问题
I'm quite new to R, so I hope you guys can help me out with this problem. I tried to look for other posts with similar questions, but wasn't able to find any.
This is how my representative data set is constructed:
library(lattice)
data(Theoph)
DF <- Theoph
DF$Time <- rep(c(0,0.25,0.5,1,2,4,5,7,9,12,24), times=12)
DF$group[DF$Subject==1|DF$Subject==2|DF$Subject==3] <- "A"
DF$group[DF$Subject==4|DF$Subject==5|DF$Subject==6] <- "B"
DF$group[DF$Subject==7|DF$Subject==8|DF$Subject==9] <- "C"
DF$group[DF$Subject==10|DF$Subject==11|DF$Subject==12] <- "D"
y <- DF$conc
x <- DF$Time
groups <- as.factor(DF$group)
I made an xyplot that shows the individual data points and also lines showing the mean per group (A,B,C and D) using this code:
xyplot(y ~ x, type="p", groups=groups, auto.key=T,
panel = function(x, y, groups, subscripts, ...){
panel.superpose(x, y, groups, subscripts,...,
panel.groups=function(x,y,col,col.symbol,...){
panel.xyplot(x, y, col=col.symbol,...)
panel.average(x, y, col=col.symbol, lwd = 2, horizontal = FALSE)
})
})
I would like to add error bars (e.g. +/- standard deviation) to the mean values. I know there is a function called panel.arrows, but I haven't been successful in combining it with the panel.groups function.
Is there any way of doing this? Or should I make a separate data frame first with the SD and lower and upper limits of the error bars?
回答1:
This may not be exactly what you were looking for, but it comes very close. Have a read here: R - Lattice xyplot - How do you add error bars to groups and summary lines?
来源:https://stackoverflow.com/questions/23678494/r-add-error-bars-to-different-groups-in-xyplot