Geek On Acid
Geek On Acid

Reputation: 6410

Add jittered data points to lattice xYplot with error bars

I'm trying to plot error bars overlaid on jittered raw data points using xYplot from package Hmisc. Seemed straightforward to just call a function within xYplot using panel.stripplot. It works, but there is a strange glitch - I can't 'jitter' the data plotted with panel.stripplot. Let me illustrate my point:

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, ...)
  }
)

Which results in:

enter image description here

As you can see, the points are lined up vertically with the error bars, why I would like them to be slightly offset in horizontal plain. I tried to tweak factor and amount parameters in the panel.stripplot but it doesn't change it. Any suggestions? Solutions with lattice-only please, preferably using xYplot.

Upvotes: 4

Views: 2534

Answers (1)

agstudy
agstudy

Reputation: 121588

Use horizontal=FALSE:

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

Internally is just a call to :

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

enter image description here

Upvotes: 3

Related Questions