[R] lattice: add vertical lines in xyplot

Deepayan Sarkar deepayan.sarkar at gmail.com
Thu Aug 7 21:04:41 CEST 2008


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



More information about the R-help mailing list