Skip to content

Adding error bars to xyplot()

5 messages · Jon Zadra, David Winsemius, Deepayan Sarkar

#
Hi,

I want to add error bars to a plot generated with xyplot.  I've tried 
both errbar() and plotCI(), but in both cases the points are not in the 
same place.  It's as if the two functions are using a different frame of 
reference for the plotting area.

for example:
means <- c(92.5, 92.25, 90.9, 91.0, 94.15, 90.05) #means
time <- c(1,1,2,2,3,3) #occasion variable
group <- rep(c("n","u"),3) #grouping variable
SE <- c(2.22, 1.66, 2.10, 1.43, 2.31, 1.57) #standard errors

#Plot the graph
xyplot(means ~ time, groups=group, type="o", lwd=3, main="Change Over 
Time by Condition", scale=list(cex=2), xlab="Test Number", auto.key=T)

#first attempt
require(Hmisc)
errbar(x = time, y = means, yplus = means + SE, yminus = means - SE, 
add=T, col=c("blue", "hotpink"))

#second attempt, same result
require(gplots)
plotCI(x = time, y = means, uiw = SE, add=T)


Thanks in advance!

- Jon
#
On Apr 21, 2010, at 6:24 PM, Jon Zadra wrote:

            
Yes. plotCI uses base graphics while xyplot is a lattice function.
A base graphics function.
Another base graphics function.

Since you already have Hmisc why not check out Harrell's xYplot which  
provides CI's in a grid function:
 From the xYplot examples:

require(Hmisc)
dfr <- expand.grid(month=1:12, continent=c('Europe','USA'),
  sex=c('female','male')) set.seed(1)
dfr <- upData(dfr, y=month/10 + 1*(sex=='female') +
                           2*(continent=='Europe') +
                           runif(48,-.15,.15),
                    lower=y - runif(48,.05,.15),
                    upper=y + runif(48,.05,.15))
xYplot(Cbind(y,lower,upper) ~ month|continent,  
subset=sex=='male',data=dfr)
David Winsemius, MD
West Hartford, CT
1 day later
#
I took a look at xYplot() but it is beyond over-complex, at least for my 
level, and the documentation isn't too clear.

Is there a simple way to make functions like plotCI() and errbar() use 
the lattice reference rather than base graphics?

It seems like creating a plot of two lines across three time points 
separated into two groups should be a very basic, simple thing for a 
statistics program to do.  Is there really no way to simply pass a model 
to a plotting function and get something that includes error bars?  I 
just wonder if I"m missing how to do this "the easy way."

Thanks,

Jon
Jon Zadra
Department of Psychology
University of Virginia
P.O. Box 400400
Charlottesville VA 22904
(434) 982-4744
email: zadra at virginia.edu
<http://www.google.com/calendar/embed?src=jzadra%40gmail.com>
On 4/21/2010 6:47 PM, David Winsemius wrote:
#
On Apr 23, 2010, at 3:56 PM, Jon Zadra wrote:

            
Don't know about simple. You seem to be rejecting one by one the  
"simple" methods people have offered. Why not do a bit of searching on  
your own, since you have a different standard for "simple" than some  
of the rest of us:

RSiteSearch("lattice error bars")
RSiteSearch("lattice confidence intervals")
1 day later
#
On Fri, Apr 23, 2010 at 12:56 PM, Jon Zadra <jrz9f at virginia.edu> wrote:
Try ?segplot in package latticeExtra. E.g.,

require(latticeExtra)
segplot(interaction(group, time) ~ (means-SE) + (means+SE), centers = means,
        horizontal = FALSE, draw.bands = FALSE)

YMMV.

-Deepayan