[R] lattice graph with free scales and reversed axis values

Naresh Gurbuxani naresh_gurbuxani at hotmail.com
Sat Nov 5 23:59:19 CET 2016


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]))))
})



More information about the R-help mailing list