Hi Phil,

This is exactly what I want, I just changed some trivial settings to make it 
plot standard error of the mean and color changing with panels.
Thank you very much.
-Xiaokuan

library(lattice)

mypanel = function(x,y,...){
   
 agg<-aggregate(y,list(x),function(x)c(mean(x,na.rm=TRUE),sd(x,na.rm=TRUE),length(x[!is.na(x)])))

      mns = agg[,2][,1]
    sds = agg[,2][,2]
     ns = agg[,2][,3]
    for(i in 1:nrow(agg))llines(c(i-.2,i+.2),rep(mns[i],2),lwd=3,col=i)
    for(i in 1:nrow(agg)){llines(c(i-.1,i+.1),rep(mns[i] + 
sds[i]/(ns[i]^0.5),2),col=i);  #standard error
                          llines(c(i-.1,i+.1),rep(mns[i] - 
sds[i]/(ns[i]^0.5),2),col=i)}
    panel.dotplot(x,y,col=1:nrow(agg),...)
    }

dotplot(Score ~ Dose | Sex , group=Dose, data=dat,panel=mypanel)






________________________________
From: Phil Spector <spector@stat.berkeley.edu>

Cc: r-help <r-help@r-project.org>
Sent: Wed, February 9, 2011 6:56:32 PM
Subject: Re: [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@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@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.
>



 
____________________________________________________________________________________

[[elided Yahoo spam]]

	[[alternative HTML version deleted]]

