Question

I have a line-plot produced with lattice's xyplot. It contains two types of temperatures measured, one of these are mean values. Therefore, I would like to add standard errors to these points (t.tort). Unfortunately, doing this separately does not work using (Hmisc)errbar... here is what I have come up with so far:

Temperature Plot

xyplot(mean.tort+t.ws~DateTime, pre, type=c("a", "p"), col=c("red", "blue"),
   main="Pre-Translocation",
   xlab=list(label="Date and Time", cex=1), 
   ylab=list(label="Temperature (°C)", cex=1),
   scales = list(tck = c(1, 0),
                 x=list(cex=0.8, rot=45, tick.number=40),
                 y=list(cex=0.8, tick.number=8, limits=c(29,43))),
   key=list(text=list(c("Tortoise","Ambient")), lines=list(col=c("red", "blue"),type="l"), corner=c(0.5,0.92)))
errbar(x=pre$DateTime, y=pre$mean.tort, yplus=pre$mean.tort+pre$se.tort,yminus=pre$mean.tort-pre$se.tort,
   add=T, col="red")

And the important bits of my data frame are as follows:

pre$DateTime<-c(as.POSIXct("2013-01-27 09:00:00" "2013-01-27 10:00:00" "2013-01-27 11:00:00" "2013-01-27 12:00:00" "2013-01-27 13:00:00")
pre$t.ws<-c(32.7, 35.5, 37.1, 37.6, 38.7)
pre$mean.tort<-c(32.4, 34.9, 35.1, 36.8, 37.7)
pre$se.tort<-c(0.825, 0.84, 0.21, 0.228, 0.28)

I got a bit frustrated with this, so any suggestions will be greatly appreaciated. Thank you very much for your efforts in advance!

Was it helpful?

Solution

Try this

Your data:

pre = data.frame(DateTime = as.POSIXct(c("2013-01-27 09:00:00", "2013-01-27 10:00:00", 
                                     "2013-01-27 11:00:00", "2013-01-27 12:00:00", 
                                     "2013-01-27 13:00:00")),
            t.ws = c(32.7, 35.5, 37.1, 37.6, 38.7),
            mean.tort = c(32.4, 34.9, 35.1, 36.8, 37.7),
            se.tort = c(0.825, 0.84, 0.21, 0.228, 0.28))

The plot:

require(lattice)

xyplot(mean.tort+t.ws~DateTime, pre,
   main="Pre-Translocation",
   xlab=list(label="Date and Time"), 
   ylab=list(label="Temperature (°C)"),
   scales = list(tck = c(1, 0),
                 x=list(cex=0.8, rot=45, tick.number=40),
                 y=list(cex=0.8, tick.number=8, limits=c(29,43))),
   key=list(text=list(c("Tortoise","Ambient")), 
            lines=list(col=c("red", "blue"), 
                       type="l"), corner=c(0.5,0.92)),
   lx = pre$mean.tort - pre$se.tort, ux = pre$mean.tort + pre$se.tort,
   panel = function (x,y, lx, ux,  ...){
     panel.xyplot(x,y, type = "b", col=c("red", "blue"), ...)
     panel.segments(x0 = x, x1 = x, y0 = lx, y1 = ux, col = "red", ...)
   }
)

Here you go: enter image description here

You can use panel.arrows() instead of the segments if you really want to put those little 'hats' on your error bars.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top