Center error bars (geom_errorbar) horizontally on bars (geom_bar)

前端 未结 1 1084
Happy的楠姐
Happy的楠姐 2020-12-18 10:19

How would I position the error bars in the centre of the appropriately coloured bars?

df1 <- data.frame(
  supp = c(\"OJ\",\"OJ\",\"OJ\",\"VC\",\"VC\",\"V         


        
相关标签:
1条回答
  • 2020-12-18 10:41

    As data for error bars are located in the same data frame where data for the bars you don't need to provide argument data= in geom_errorbar() and also there is no need to call geom_errorbar() twice.

    You should provide in geom_errorbar() ymin and ymax values in aes(), also color=supp in aes() will ensure that error bars are colored according to supp values. To get the same colors as for bars, add scale_color_manual() with the same color names. With argument position=position_dodge(0.9) you can get errorbars in center of bars.

    ggplot(df1, aes(x=dose, y=len, fill=supp)) +      
      geom_bar(stat="identity", position=position_dodge()) +  
      scale_fill_manual(name = "", values = c("deepskyblue1", "green")) +
      geom_errorbar(aes(ymin=len-se, ymax=len+se,color=supp), width=.4,
           position=position_dodge(.9))+
      scale_color_manual(name = "", values = c("deepskyblue1", "green"))
    

    enter image description here

    0 讨论(0)
提交回复
热议问题