Robin Jeffries
2010-Aug-13 21:58 UTC
[R] Lattice xyplots plots with multiple lines per cell
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]]
Juliet Hannah
2010-Aug-17 01:24 UTC
[R] Lattice xyplots plots with multiple lines per cell
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. >
Dennis Murphy
2010-Aug-17 12:04 UTC
[R] Lattice xyplots plots with multiple lines per cell
Here's a lattice solution using some faked data. library(lattice) testdat <- data.frame(time = rep(rep(1:10, each = 4), 8), y = rnorm(320), gender = factor(rep(rep(c('F', 'M'), each = 80), 2)), grade = factor(rep(c('1-3', '4-6'), each = 160)), gp = factor(rep(rep(c('Control', 'Intervention'), each = 40), 4))) testmn <- aggregate(y ~ time + gp + gender + grade, data = testdat, FUN mean) xyplot(y ~ time | gender * grade, groups = gp, data = testmn, type = c('p', 'l'), auto.key = list(space = 'top', lines = TRUE, points = FALSE, columns = 2)) I have four replicate observations at each of ten times for eight combinations of gender, grade level and treatment group. aggregate() is used to average the four observations at each time point, and then point/line graphs of average response vs. time in each treatment is plotted for each gender and grade level combination. I hope I caught the essential characteristics of what you wanted. Dennis On Fri, Aug 13, 2010 at 2:58 PM, Robin Jeffries <rjeffries@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@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. >[[alternative HTML version deleted]]