r- Add error bars to different groups in xyplot

旧城冷巷雨未停 提交于 2019-12-11 12:17:57

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!