[R] lattice: add vertical lines in xyplot
baptiste auguie
ba208 at exeter.ac.uk
Thu Aug 7 22:42:12 CEST 2008
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)
>>>
>>> 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
_____________________________
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
More information about the R-help
mailing list