问题
I want to represent a tree factor dotplot with error bar combined with boxplot. I have complete almost all the graphic except that my error bar are overlapped. Concretely my idea is to jitter the dots and the error bars
set.seed(1)
data<-sample(rnorm(50,2,2),40)
sd<-sample(rnorm(50,0.2,2),40)
factor<-sample( LETTERS[1:3], 40, replace=TRUE, prob=c(0.3,.3, 0.3) )
frame<-data.frame(data,sd,factor)
frame<-as.data.frame(frame)
up=frame$data+frame$sd
lo=frame$data-frame$sd
bwplot(data ~ factor, frame,
ylim=c((min(lo)+0.5*min(lo)),(max(up)-0.5*min(lo))),
up=frame$data+frame$sd,
lo=frame$data-frame$sd,
panel=function(x,y,...){
panel.bwplot(x,y,...)
panel.stripplot(x,y,pch=16 ,alpha=0.7, jitter=TRUE, factor=0.2,
col="grey",cex=1.2,...)
panel.arrows(x0=x, y0=lo,
x1=x, y1=up,code=3,
angle=90, length=0.05)
}
)
thanks!
回答1:
do the jittering separately
set.seed(1)
data<-sample(rnorm(50,2,2),40)
sd<-sample(rnorm(50,0.2,2),40)
factor<-sample( LETTERS[1:3], 40, replace=TRUE, prob=c(0.3,.3, 0.3) )
frame<-data.frame(data,sd,factor)
frame<-as.data.frame(frame)
up=frame$data+frame$sd
lo=frame$data-frame$sd
bwplot(data ~ factor, frame,
ylim=c((min(lo)+0.5*min(lo)),(max(up)-0.5*min(lo))),
up=frame$data+frame$sd,
lo=frame$data-frame$sd,
panel=function(x,y,...){
panel.bwplot(x,y,...)
xj=jitter(as.numeric(x))
panel.stripplot(xj,y,pch=16 ,alpha=0.7, factor=0.2,
col="grey",cex=1.2,...)
panel.arrows(x0=xj, y0=lo,
x1=xj, y1=up,code=3,
angle=90, length=0.05)
}
)
Then the amount of jitter is the same for the stripplot and the error bars (arrows).
来源:https://stackoverflow.com/questions/29180567/error-bar-lattice-dotplot