Hi list, This is a very basic question about lattice: I wish to add some vertical lines in each panel of a xyplot as demonstrated in this example:> library(lattice) > > xx <- seq(1, 10, length=100) > x <- rep(xx, 4) > y <- c(cos(xx), sin(xx), xx, xx^2/10) > fact <- factor(rep(c("cos", "sin", "id", "square"), each=100)) > fact2 <- factor(rep(c("periodic", "not periodic"), each=100)) > > my.df <- data.frame(x=x, y=y, fact = fact, fact2 = fact2) > > head(my.df) > > xyplot(y~x | fact, data = my.df, groups= fact2) # this plots as > expected > > my.lines <- c(5, 6) # i want to draw these vertical lines, according > to the level of fact2 > > xyplot(y~x | fact, data = my.df, groups= fact2, > panel=function(x, y,subscripts, ...) { > panel.xyplot(x, y,subscripts, ...) > panel.abline(v=my.lines[subscripts])} > )this gives me an error, but I don't understand the help for the panel function. Alternatively, I tried in ggplot but here again, I get an error:> p <- ggplot(my.df, aes(x, y)) + > geom_path(aes(colour = fact)) + > facet_grid(fact2 ~ fact) + > geom_vline(intercept = my.lines) # how do I select the relevant > one ? > pMany thanks, baptiste _____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag
On Thu, Aug 7, 2008 at 11:54 AM, baptiste auguie <ba208 at exeter.ac.uk> wrote:> Hi list, > > This is a very basic question about lattice: I wish to add some vertical > lines in each panel of a xyplot as demonstrated in this example: > >> library(lattice) >> >> xx <- seq(1, 10, length=100) >> x <- rep(xx, 4) >> y <- c(cos(xx), sin(xx), xx, xx^2/10) >> fact <- factor(rep(c("cos", "sin", "id", "square"), each=100)) >> fact2 <- factor(rep(c("periodic", "not periodic"), each=100)) >> >> my.df <- data.frame(x=x, y=y, fact = fact, fact2 = fact2) >> >> head(my.df) >> >> xyplot(y~x | fact, data = my.df, groups= fact2) # this plots as expected >> >> my.lines <- c(5, 6) # i want to draw these vertical lines, according to >> the level of fact2 >> >> xyplot(y~x | fact, data = my.df, groups= fact2, >> panel=function(x, y,subscripts, ...) { >> panel.xyplot(x, y,subscripts, ...) >> panel.abline(v=my.lines[subscripts])} >> ) > > this gives me an error, but I don't understand the help for the panel > function.I think this is what you meant to do: xyplot(y~x | fact, data = my.df, groups= fact2, panel = function(x, y, subscripts, groups, ...) { panel.xyplot(x, y, subscripts = subscripts, groups = groups, ...) g <- unique(as.numeric(groups)[subscripts]) panel.abline(v = my.lines[g]) }) Another approach is xyplot(y ~ x | fact, data = my.df, groups= fact2, panel = panel.superpose, panel.groups = function(..., group.number) { panel.abline(v = my.lines[group.number]) panel.xyplot(...) }) -Deepayan
> Alternatively, I tried in ggplot but here again, I get an error: > >> p <- ggplot(my.df, aes(x, y)) + >> geom_path(aes(colour = fact)) + >> facet_grid(fact2 ~ fact) + >> geom_vline(intercept = my.lines) # how do I select the >> relevant one ?If you want a different line in each panel, you'll need to construct a data frame - see the example at http://had.co.nz/ggplot2/geom_abline.html for the basic idea. Hadley -- http://had.co.nz/