How can I ‘dodge’ the position of geom_point in ggplot2?

I am using ggplot2 in R to make plots like the following ones:

enter image description here

The errorbars overlap with each other which look really messy.
How can I separate the errorbars for different indices?
I have used position=”dodge” but it seems to be not working.
Here is the main part of my code:

plot =  ggplot(data,aes(x=ntrunc,y=beta_best,group=ntrunc,colour=INDEX))
       +geom_point(aes(shape=detectable),na.rm=TRUE,position="dodge") 
        +geom_errorbar(aes(x=ntrunc,ymax=beta_high,ymin=beta_low),na.rm=TRUE,position="dodge")

Answer

The group should = INDEX instead of ntrunc in the aes.

plot =  ggplot(data, aes(x=ntrunc, y=beta_best, group=INDEX, colour=INDEX)) +
   geom_point(aes(shape=detectable), na.rm=TRUE, position="dodge") +
   geom_errorbar(aes(x=ntrunc, ymax=beta_high, ymin=beta_low), na.rm=TRUE, position="dodge")

The plot looks better now.

enter image description here

Attribution
Source : Link , Question Author : ycc , Answer Author : Richard Telford

Leave a Comment