[R] Lattice xyplots plots with multiple lines per cell

Juliet Hannah juliet.hannah at gmail.com
Tue Aug 17 03:24:26 CEST 2010


You may want to check out examples in lattice and ggplot2. Both of
these make plotting subsets much easier. I can't remember the lattice
syntax off the top of my head, but if you post some example data –
either by creating it or using dput – people will be able to help out
easier.  Here is some example data below that has the features you
were looking for (using ggplot2). One note. Learning both of the
packages takes some time, but if you make graphs often,  it will save
you a lot of time in the future.


time = c(1:100)
mymeans = 2*time + rnorm(n=100,mean=10,sd=20)
gender <- sample(c("m","f"),100,replace=TRUE)
grade <- sample(c("old","young"),100,replace=TRUE)
groups <- sample(c("control","intervention"),100,replace=TRUE)

mydata <- data.frame(y=mymeans,time=time,gender=gender,grade=grade)

library(ggplot2)
p <- ggplot(mydata,aes(x=time,y=mymeans))
p <- p + geom_point(aes(colour=groups)) +facet_grid(gender ~ grade)
p


On Fri, Aug 13, 2010 at 5:58 PM, Robin Jeffries <rjeffries at ucla.edu> wrote:
> Hello,
>
> I need to plot the means of some outcome for two groups (control vs
> intervention) over time (discrete) on the same plot, for various subsets
> such as gender and grade level. What I have been doing is creating all
> possible subsets first, using the aggregate function to create the means
>  over time, then plotting the means over time (as a simple line plot with
> both control & intervention on one plot) for one subset. I then use par()
> and repeat this plot for each gender x grade level subset so they all appear
> on one page.
>
>
> This appears to me to be very similar to an xyplot, something like
>  mean(outcome) ~ gender + gradelevel. However, I can't figure out how I
> could get both control and intervention lines in the same plot.
>
> Any suggestions? What i'm doing now -works-, but just seems to be the long
> way around.
>
> -Robin
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list