Error bars show through open symbol

2019-06-20 07:25发布

问题:

I have a plot with pairs of points that are slightly offset. Each pair of points has associated error bars. I have specified that the symbol of the first point in the pair is different from that of the second (closed circle vs open circle). I would like it so that the error bars do not show through the open symbol.

Here is a mock data set:

x = runif(4,-2,2)
x_1 = runif(4,-1,3)
dfr <- data.frame(
 x = c(x, x_1),
 y = rep(c("A","B","C","D"), 2),
 upper = c(x+2, x_1+1),
 lower = c(x-2, x_1-2),
 type = rep(c("alpha", "beta"), each = 4))

And here is the plot:

dodge=position_dodge(width=0.5)  
ggplot(dfr,aes(x=y,y=x,colour=type)) + 
    geom_point(size=8,aes(shape=type),position=dodge) +
    geom_errorbar(aes(ymax=upper,ymin=lower),position = dodge) + 
    scale_colour_manual(values = c('gray','black')) +
    scale_shape_manual(values = c(19,21)) +
    coord_flip() + 
    opts(legend.position="none")

Thanks for any help you can provide!

回答1:

I can't think of a way to make an 'open' point and not let the errorbar show trough. The only way of doing this would be to fill the points with the same colour as the background, but then your gridlines won't be visible through the point.

To do this, map the fill aesthetic to type, and specify scale_fill_manual with the fill colour grey90 which is the theme_grey setting:

ggplot(dfr,aes(x=y,y=x,colour=type, fill=type)) + 
    geom_errorbar(aes(ymax=upper,ymin=lower),position = dodge) + 
    geom_point(size=8,aes(shape=type),position=dodge) +
    scale_colour_manual(values = c('gray','black')) +
    scale_fill_manual(values=c('grey', 'grey90')) +
    scale_shape_manual(values = c(19,21)) +
    coord_flip() + 
    opts(legend.position="none")



回答2:

Why don't you just use color as shown in the modified code below. It will fill the black circles too. Not sure if that is acceptable.

ggplot(dfr,aes(x=y,y=x,colour=type)) + 
    geom_point(size=8,position=dodge) +
    geom_errorbar(aes(ymax=upper,ymin=lower),position = dodge) + 
    scale_colour_manual(values = c('gray','black')) +
    coord_flip() + 
    opts(legend.position="none")