Solved – How to ‘dodge’ the position of geom_point in ggplot2

data visualizationggplot2

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")

Best 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