Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw vertical ending of error bar line in dotplot

Tags:

plot

r

lattice

I am drawing dotplot() using lattice or Dotplot() using Hmisc. When I use default parameters, I can plot error bars without small vertical endings

--o--

but I would like to get

|--o--|

I know I can get

|--o--|

when I use centipede.plot() from plotrix or segplot() from latticeExtra, but those solutions don't give me such nice conditioning options as Dotplot(). I was trying to play with par.settings of plot.line, which works well for changing error bar line color, width, etc., but so far I've been unsuccessful in adding the vertical endings:

require(Hmisc)
mean = c(1:5)
lo = mean-0.2
up = mean+0.2
d = data.frame (name = c("a","b","c","d","e"), mean, lo, up)
Dotplot(name ~ Cbind(mean,lo,up),data=d,ylab="",xlab="",col=1,cex=1,
        par.settings = list(plot.line=list(col=1),
                       layout.heights=list(bottom.padding=20,top.padding=20)))

enter image description here

Please, don't give me solutions that use ggplot2...

like image 972
Geek On Acid Avatar asked Feb 27 '12 16:02

Geek On Acid


1 Answers

I've had this same need in the past, with barchart() instead of with Dotplot().

My solution then was to create a customized panel function that: (1) first executes the original panel function ; and (2) then uses panel.arrows() to add the error bar (using a two-headed arrow, in which the edges of the head form a 90 degree angle with the shaft).

Here's what that might look like with Dotplot():

# Create the customized panel function
mypanel.Dotplot <- function(x, y, ...) {
    panel.Dotplot(x,y,...)
        tips <- attr(x, "other")
        panel.arrows(x0 = tips[,1], y0 = y, 
                     x1 = tips[,2], y1 = y, 
                     length = 0.15, unit = "native",
                     angle = 90, code = 3)
}

# Use almost the same call as before, replacing the default panel function 
# with your customized function.
Dotplot(name ~ Cbind(mean,lo,up),data=d,ylab="",xlab="",col=1,cex=1,
        panel = mypanel.Dotplot,
        par.settings = list(plot.line=list(col=1),
                       layout.heights=list(bottom.padding=20,top.padding=20)))

enter image description here

like image 163
Josh O'Brien Avatar answered Nov 20 '22 14:11

Josh O'Brien