xyplot from summary data

Lazar

Phineas Packard
#1
Ok so here is a data set in which I have means for males and females and their SEs (MSE and FSE respectively) along with a factor and a year in school (grade):
Code:
data<-structure(list(Females = c(0.664, -0.123, 0.448, 0.387, 0.319, 
1.092, -0.322, -0.802, -0.374, -0.279, -0.273, -0.11, 0.453, 
0.282, 0.734, 0.747, 0.574, 0.899), FSE = c(0.128, 0.092, 0.117, 
0.113, 0.123, 0.149, 0.092, 0.075, 0.087, 0.092, 0.1, 0.122, 
0.087, 0.078, 0.09, 0.092, 0.094, 0.116), Males = c(-0.001, -0.828, 
-0.463, -0.491, -0.171, 0.156, 0, -0.665, -0.332, -0.356, -0.264, 
0.017, 0, -0.368, -0.005, -0.026, 0.136, 0.282), MSE = c(0.105, 
0.072, 0.093, 0.091, 0.12, 0.146, 0.092, 0.073, 0.093, 0.083, 
0.106, 0.129, 0.071, 0.052, 0.071, 0.068, 0.091, 0.102), Grade = c(7L, 
8L, 9L, 10L, 11L, 12L, 7L, 8L, 9L, 10L, 11L, 12L, 7L, 8L, 9L, 
10L, 11L, 12L), factor = c("sadness", "sadness", "sadness", "sadness", 
"sadness", "sadness", "fear", "fear", "fear", "fear", "fear", 
"fear", "hostility", "hostility", "hostility", "hostility", "hostility", 
"hostility")), .Names = c("Females", "FSE", "Males", "MSE", "Grade", 
"factor"), row.names = c(NA, 18L), class = "data.frame")

I want to create a xyplot as follows:
Code:
library(lattice)

xyplot(Males+Females~Grade|factor, data=data, relation="free", pch=c(1,2),  lty=c(1,2), type="b",
       col="black",  main="Gender Differences in Hope and Affect", ylab="", xlab="School Grade")
My question is how can I use data$FSE and data$MSE to construct SE bars around these line graphs?
 

vinux

Dark Knight
#2
Lazar,
How do you wanted to create error bars? what is FSE (i guess MSE is mean squared error).

It would be easier to do in ggplot2 (at least for me and trinker). I guess you prefer it in xyplot.
 

Lazar

Phineas Packard
#3
Hi Vinux happy to have it done in ggplot and happy really to have the error bars done in what ever way is convienent (standard error bars would be easiest I think). FSE = Female standard error and MSE = Male Standard error
 

vinux

Dark Knight
#4
Quick and dirty solution in ggplot2. We could improve this.

First step: make the dataset in long format
Code:
library(reshape)

data1 = melt(data, id.var=c(5, 6))
data1$var="SomeVar"
data1$var[data1$variable %in% c("FSE","MSE")]="SE"
data1$group="Female"
data1$group[data1$variable %in% c("Males","MSE")]="Male"
data2 = cast(data1, factor + Grade + group ~ var, mean)
ggplot2 code here

Code:
library(ggplot2)
ggplot(data=data2) + geom_line(aes(x=Grade,y=SomeVar, linetype=group)) + 
    geom_point(aes(x=Grade,y=SomeVar, shape=group)) + 
    geom_errorbar(aes(x=Grade, y=SomeVar, ymin= SomeVar - SE, ymax=SomeVar + SE, group=group), width=0.2)+ facet_wrap(~factor, ncol=2)
You could also add different colours for Males/Females.