Naresh Gurbuxani
2016-Nov-05 22:59 UTC
[R] lattice graph with free scales and reversed axis values
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. 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])))) })
David Winsemius
2016-Nov-05 23:39 UTC
[R] lattice graph with free scales and reversed axis values
> 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
P Tennant
2016-Nov-05 23:57 UTC
[R] lattice graph with free scales and reversed axis values
Hi Naresh, You could calculate the ranges explicitly and then supply to scales(): holdRange <- vector('list', length(unique(my.df$name))) for(i in 1:length(holdRange)){ holdRange[[i]] <- rev(range(my.df$x[my.df$name==unique(my.df$name)[i]])) } holdRange # [[1]] # [1] -5.052890 -9.967671 # [[2]] # [1] 2.648870 -2.629866 xyplot(y ~ x | name, data = my.df, type = c("p", "g"), scales = list(relation="free", limits=holdRange)) Philip On 6/11/2016 9:59 AM, Naresh Gurbuxani 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. > 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])))) > }) > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Naresh Gurbuxani
2016-Nov-06 03:27 UTC
[R] lattice graph with free scales and reversed axis values
This worked very well for me. Thanks, Naresh ________________________________ From: P Tennant <philipt900 at iinet.net.au> Sent: Saturday, November 5, 2016 7:57 PM To: Naresh Gurbuxani Cc: R-help at r-project.org Subject: Re: [R] lattice graph with free scales and reversed axis values Hi Naresh, You could calculate the ranges explicitly and then supply to scales(): holdRange <- vector('list', length(unique(my.df$name))) for(i in 1:length(holdRange)){ holdRange[[i]] <- rev(range(my.df$x[my.df$name==unique(my.df$name)[i]])) } holdRange # [[1]] # [1] -5.052890 -9.967671 # [[2]] # [1] 2.648870 -2.629866 xyplot(y ~ x | name, data = my.df, type = c("p", "g"), scales = list(relation="free", limits=holdRange)) Philip On 6/11/2016 9:59 AM, Naresh Gurbuxani 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. > 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])))) > }) > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]