[R] lattice graph with free scales and reversed axis values
David Winsemius
dwinsemius at comcast.net
Sun Nov 6 00:39:12 CET 2016
> On Nov 5, 2016, at 3:59 PM, Naresh Gurbuxani <naresh_gurbuxani at hotmail.com> wrote:
>
> I want to draw a lattice graph where different panels have different ranges AND the order of values is reversed. I am struggling to achieve both at the same time. Can you help?
>
> Thanks,
> Naresh
>
> # Create dummy data for illustration
> my.df <- data.frame(x = runif(100, min = -10, max = -5), name = "A")
> my.df <- rbind(my.df, data.frame(x = rnorm(100), name = "B"))
> my.df <- within(my.df, {y <- x + 0.2 * rnorm(200)})
>
> # This works. Both x and y axes show values in reverse order.
> xyplot(y ~ x, groups = name, data = my.df, xlim = rev(range(my.df$x)), ylim = rev(range(my.df$y)), type = c("p", "g"))
>
> # This works. Both x and y axes show values in reverse order.
> xyplot(y ~ x | name, data = my.df, xlim = rev(range(my.df$x)), ylim = rev(range(my.df$y)), type = c("p", "g"))
>
> # This does not work as intended. x and y values are in reverse order. But both panels have the same and x and y ranges. scales argument does not seem to have any effect.
> xyplot(y ~ x | name, data = my.df, xlim = rev(range(my.df$x)), ylim = rev(range(my.df$y)), scales = list(x = "free", y = "free"), type = c("p", "g"))
>
> # This does not work at all. panel.xyplot does not see to take xlim or ylim arguments.
But prepanel does ....
xyplot(y ~ x | name, data = my.df, scales = list(x = "free", y = "free"), panel = function(x,y,subscripts, ...) {
> panel.xyplot(x,y, ylim = rev(range(y[subscripts]), xlim = rev(range(x[subscripts]))))
> })
I used this posting to r-help from 2006 by xyplot's author, Sarkar, to solve the problem:
https://stat.ethz.ch/pipermail/r-help/2006-March/101248.html
xyplot(y ~ x | name, data = my.df,
scales = list(relation="free"),
prepanel= function(x,y, ...) { list(xlim= rev(range(my.df$x)) ,
ylim = rev(range(my.df$y)) )},
type = c("p", "g"))
--
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list