# Re: [R] lattice: add vertical lines in xyplot

From: baptiste auguie <ba208_at_exeter.ac.uk>
Date: Thu, 07 Aug 2008 21:42:12 +0100

Many thanks Deepayan, it works nicely (i always need simple examples to understand the help page for some reason).

Also, Hadley, I can't get this example from ggplot2's website to work:

# Slopes and intercepts as data
p <- ggplot(mtcars, aes(x = wt, y=mpg), . ~ cyl) + geom_point()

df <- data.frame(a=rnorm(10, 25), b=rnorm(10, 0))

p + geom_abline(aes(intercept=a, slope=b), data=df)

i get an error:

Error in `[.data.frame`(df, , var) : undefined columns selected

Thanks again,

baptiste

On 7 Aug 2008, at 20:04, Deepayan Sarkar wrote:

```> 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)
>>>
>>>
>>> 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

_____________________________

```

Baptiste Auguié

School of Physics
University of Exeter
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

R-help_at_r-project.org mailing list
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. Received on Thu 07 Aug 2008 - 20:44:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 07 Aug 2008 - 21:33:51 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.