19
votes
$\begingroup$

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")
$\endgroup$
4
  • 1
    $\begingroup$ Have you tried different values of "dodge"? For example something like that: position = position_dodge(width = 0.90). See also this post. $\endgroup$ Jun 14, 2014 at 15:21
  • $\begingroup$ Thanks for your example. However, I have just tried different values of width and it still does't work. $\endgroup$
    – ycc
    Jun 14, 2014 at 15:40
  • 2
    $\begingroup$ It is difficult to come up with solutions without the original dataset. But one question I have: Why are your $x$-values and the "groups" the same? What is ntrunc? Shouldn't group be INDEX? $\endgroup$ Jun 14, 2014 at 16:27
  • $\begingroup$ It works now after I changed group="INDEX". I think I have misunderstood the meaning of 'group'. Thanks @COOLSerdash $\endgroup$
    – ycc
    Jun 15, 2014 at 1:01

1 Answer 1

12
votes
$\begingroup$

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

$\endgroup$
3
  • 7
    $\begingroup$ This didn't work for me until I used geom_point(aes(shape=detectable),na.rm=TRUE, position=position_dodge(width=0.3)) $\endgroup$
    – Nova
    Apr 13, 2016 at 16:01
  • $\begingroup$ It would be great to get an example data here because I cannot reproduce your result. $\endgroup$ Nov 11, 2016 at 23:15
  • 1
    $\begingroup$ @Nova my guess is the answer was broken when ggplot2 went from version 0.9.X to 1.X or 2.X $\endgroup$ Jun 9, 2017 at 16:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.