将抖动数据点添加到带有误差线的格子xYplot

时间:2013-12-11 15:56:17

标签: r plot lattice

我正在尝试使用xYplot from package Hmisc绘制覆盖在抖动的原始数据点上的错误条。看起来直截了当地使用xYplotpanel.stripplot内调用函数。它有效,但有一个奇怪的故障 - 我不能'抖动'用panel.stripplot绘制的数据。让我说明一下我的观点:

library(reshape2)
library(Hmisc)
data(iris)
#get error bars
d <- melt(iris, id=c("Species"), measure=c("Sepal.Length"))
X <- dcast(d, Species ~ variable, mean)
SD <- dcast(d, Species ~ variable, sd)
SE = SD[,2]/1#this is wrong on purpose, to plot larger error bars
Lo = X[,2]-SE
Hi = X[,2]+SE
fin <- data.frame(X,Lo=Lo,Hi=Hi)
#plot the error bars combined with raw data points
quartz(width=5,height=7)
xYplot(Cbind(Sepal.Length, Lo, Hi) ~ numericScale(Species), fin,  
       type=c("p"), ylim=c(4,8),lwd=3, col=1,
       scales = list(x = list(at=1:3, labels=levels(d$Species))),
       panel = function(x, y, ...) { 
         panel.xYplot(x, y, ...)
         panel.stripplot(d$Species, d$value, jitter.data = TRUE, cex=0.2, ...)
  }
)

结果是:

enter image description here

正如您所看到的,这些点与误差条垂直排列,为什么我希望它们在水平平面上略微偏移。我尝试在factor中调整amountpanel.stripplot参数,但不会更改它。有什么建议?只有格子的解决方案,最好使用xYplot

1 个答案:

答案 0 :(得分:2)

使用horizontal=FALSE

panel.stripplot(d$Species, d$value,                                         
                          jitter.data = TRUE, cex=0.2,horizontal=FALSE, ...)

内部只是致电:

panel.xyplot(d$Species, d$value, cex=0.2,jitter.x=TRUE, ...)

enter image description here

相关问题