Xiaokuan Wei
2011-Feb-09 21:26 UTC
[R] add mean and sd to dotplot in each panel using lattice
Hi, I have a data frame like this: ScoreDoseSex 2.81Dose1M 1.81Dose1M 1.22Dose1M 0.81Dose1M 0.49Dose1M 0.22Dose1M 0.00Dose1M -0.19Dose1M -0.17Dose1F -0.32Dose1F -0.46Dose1F -0.58Dose1F -0.70Dose1F -0.81Dose1F -0.91Dose1F -1.00Dose1F -1.77Dose2M -1.85Dose2M -1.93Dose2M -2.00Dose2M -2.07Dose2M -2.14Dose2M -2.20Dose2M -2.26Dose2M -2.32Dose2F -2.38Dose2F -2.17Dose2F -2.49Dose2F -2.54Dose2F -2.58Dose2F -2.63Dose2F -2.42Dose2F I can make the dotplot using lattice package: library(lattice) dat<-read.table("test.txt",header=T,sep="\t") dotplot(Score ~ Dose | Sex, groups=Sex, data=dat) How can I add mean line and stdev bar around mean line to this dotplot? I have searched maillists, only several post about add sth to dotplot, but they are not what I want. I know it may have sth to do with panel.function, but don't know how to implement it. I think the mean and stdev bar are popular additions to dot plot, don't know why there is no argument to control this kind of plotting. Thanks. Xiaokuan ____________________________________________________________________________________ TV dinner still cooling? [[alternative HTML version deleted]]
Phil Spector
2011-Feb-09 23:56 UTC
[R] add mean and sd to dotplot in each panel using lattice
Xiaokuan - Maybe this will get you started: mypanel = function(x,y,...){ agg<-aggregate(y,list(x),function(x)c(mean(x,na.rm=TRUE),sd(x,na.rm=TRUE))) mns = agg[,2][,1] sds = agg[,2][,2] for(i in 1:nrow(agg))llines(c(i-.1,i+.1),rep(mns[i],2),lwd=3) for(i in 1:nrow(agg)){llines(c(i-.1,i+.1),rep(mns[i] + 1.96 * sds[i],2)); llines(c(i-.1,i+.1),rep(mns[i] - 1.96 * sds[i],2))} panel.dotplot(x,y,...) } dotplot(Score ~ Dose | Sex, groups=Sex, data=dat,panel=mypanel) - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Wed, 9 Feb 2011, Xiaokuan Wei wrote:> Hi, > > I have a data frame like this: > ScoreDoseSex > 2.81Dose1M > 1.81Dose1M > 1.22Dose1M > 0.81Dose1M > 0.49Dose1M > 0.22Dose1M > 0.00Dose1M > -0.19Dose1M > -0.17Dose1F > -0.32Dose1F > -0.46Dose1F > -0.58Dose1F > -0.70Dose1F > -0.81Dose1F > -0.91Dose1F > -1.00Dose1F > -1.77Dose2M > -1.85Dose2M > -1.93Dose2M > -2.00Dose2M > -2.07Dose2M > -2.14Dose2M > -2.20Dose2M > -2.26Dose2M > -2.32Dose2F > -2.38Dose2F > -2.17Dose2F > -2.49Dose2F > -2.54Dose2F > -2.58Dose2F > -2.63Dose2F > -2.42Dose2F > > > > I can make the dotplot using lattice package: > > library(lattice) > dat<-read.table("test.txt",header=T,sep="\t") > dotplot(Score ~ Dose | Sex, groups=Sex, data=dat) > > > How can I add mean line and stdev bar around mean line to this dotplot? > I have searched maillists, only several post about add sth to dotplot, but they > are not what I want. > I know it may have sth to do with panel.function, but don't know how to > implement it. > I think the mean and stdev bar are popular additions to dot plot, don't know why > there is no argument to control this kind of plotting. > Thanks. > > Xiaokuan > > > > ____________________________________________________________________________________ > TV dinner still cooling? > > > [[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. >