geom_errorbar not dodging or producing horizontal line

232 views
Skip to first unread message

James Doonan

unread,
Mar 3, 2023, 7:20:55 AM3/3/23
to ggplot2
Dear all,
I would like my plot to have horizontal lines at the top and bottom of error bars. Is there a way to make sure they appear?
Also the position dodge does not seem to work. The vertical error bar lines are not dodging.


    library(dplyr)
    library(stringr)
    library(ggplot2)

    ggplot(data2, aes(x=Cohort, y=Mean_R2, color = Trait)) +
      geom_errorbar(aes(ymin=Mean_R2+SD, ymax=Mean_R2-SD), position=position_dodge(0.05))+
        xlim(0, 10000)

Data

        Trait Cohort Mean_R2 SD Trait_color
        Color 10 0.02947673 0.02082434 Color
        Color 50 0.2259004 0.04675703 Color
        Color 100 0.3245573 0.04779442 Color
        Color 500 0.4892648 0.04745342 Color
        Color 1000 0.5447884 0.04178 Color
        Color 5000 0.5954482 0.0355258 Color
        Color 10000 0.632652 0.03706049 Color
        Color 25000 0.6181629 0.03825963 Color
        Color 50000 0.6079681 0.04134136 Color
        Color 100000 0.5880006 0.0465956 Color
        Color 1000000 0.4801181 0.05596124 Color
        Health 10 0.1669139 0.03696639 Health
        Health 50 0.448158 0.04392942 Health
        Health 100 0.4819306 0.04525248 Health
        Health 500 0.4788394 0.05993038 Health
        Health 1000 0.5137877 0.05924604 Health
        Health 5000 0.5665759 0.05810662 Health
        Health 10000 0.5921626 0.05924604 Health
        Health 25000 0.6366738 0.06368359 Health
        Health 50000 0.6277492 0.0692886 Health
        Health 100000 0.6023589 0.07283448 Health
        Health 1000000 0.4107126 0.1088985 Health


enter image description here


Apologies for cross posting from biostars but I thought I'm more likely to get an answer from this group. Especially as I posted this question and did not get an answer almost one month ago.


Thanks,

James

Kaori Ito

unread,
Mar 3, 2023, 1:22:37 PM3/3/23
to ggplot2
You can use "width" to add horizontal lines on top and bottom of error bars, but it looks you need to assign large value since x-range is large (~ 10000). The same reason, your dodge (0.05) is too small to appear in the plot.  
I tried this and produced the attached plot.  hope this helps.

ggplot(data2, aes(x=Cohort, y=Mean_R2, color = Trait)) +
  geom_line(aes(group=Trait))+
  geom_errorbar(aes(ymin=Mean_R2+SD, ymax=Mean_R2-SD),
                width = 500, position=position_dodge(200)) +
  coord_cartesian(xlim=c(0,10000))

testplot.PNG

James Doonan

unread,
Mar 3, 2023, 3:49:26 PM3/3/23
to ggplot2
Perfect!

Thanks Kaori!

Reply all
Reply to author
Forward
0 new messages