How to avoid overlapping error bars in ggplot‏

1,446 views
Skip to first unread message

Abdi Gudda

unread,
Jan 9, 2014, 3:51:20 PM1/9/14
to ggp...@googlegroups.com

I want to plot the data provided in dput using ggplot. I have tried using the code below but the error bars have overlapped. How could I plot the data with out overlap with firm or cat on x-axis and anyone of them dodged?

 ggplot(df, aes(x = Firm, y = percent, ymin = lcl, ymax = ucl)) +   geom_linerange(position=position_dodge(width=0.60)) + geom_point( size =3.5)  +    ylab("Percent Gains") +   geom_hline(aes(yintercept = 0)) + xlab("Centers") +  theme(axis.title=element_text(face="bold",size="12", color="blue"))

Reproducible data 

structure(list(percent = c(0.487949245610619, 3.27880172100063, 1.89374877194499, 1.08252115508014, -0.692103184183268, 0.198063556460548, 1.09332951525736, 0.496686952250625, -0.105000620319717, 0.217630052057594, 0.492752135462715, 2.06742921702474, 2.19736131810619, -0.461142997846875, -0.511557523071016), lcl = c(-0.982462000903261, 0.394631092930098, -0.639589178155431, -1.80976900249697, -4.09482382314923, -0.677573402424136, -0.73224708845242, -1.00644816951001, -1.68416496187449, -2.1287716444072, -0.440412399860468, -0.00113831891449534, 0.443323691248554, -1.96785109024255, -2.95893935853256), ucl = c(1.98019611112259, 6.24582977004349, 4.49167785161435, 4.06000657363825, 2.8313461600352, 1.08142022286779, 2.95247925665045, 2.02264593629817, 1.49952850623303, 2.62028526667761, 1.43466315187299, 4.1787869565604, 3.98202963183645, 1.06872249037189, 1.99754744081631), cat = c("UPS", "UPS", "UPS", "UPS", "UPS", "DP", "DP", "DP", "DP", "DP", "RM", "RM", "RM", "RM", "RM"), Firm = c("BA", "RM", "BM", "MU", "MC", "BA", "RM", "BM", "MU", "MC", "BA", "RM", "BM", "MU", "MC")), .Names = c("percent", "lcl", "ucl", "cat", "Firm"), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"), class =

"data.frame") 

Ben Bond-Lamberty

unread,
Jan 9, 2014, 9:00:00 PM1/9/14
to ggplot2
Thanks for the reproducible example. So you want to plot the data
without overlapping error bars, but also without dodging...so what
should happen for overlapping cases? You'll have to decide, as there
isn't any magic way to make them not overlap.

One suggestion off the top of my head: y u could set alpha<<1 (say
0.25) for the geom_linerange, which would give you a visual indication
of the overlap.

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Abdi Gudda

unread,
Jan 9, 2014, 9:59:45 PM1/9/14
to ggp...@googlegroups.com
Thanks Ben. I don't want overlap, but dodging is fine.

Thanks

Ben Bond-Lamberty

unread,
Jan 10, 2014, 1:36:12 AM1/10/14
to Abdi Gudda, ggplot2
Ah, OK. Then you simply need to let ggplot know what your groups are
so it can dodge correctly:

dodge <- position_dodge(width=0.6)
ggplot(df, aes(x = Firm, y = percent, group=cat,ymin = lcl, ymax =
ucl)) + geom_point(position=dodge) + geom_linerange(position=dodge)

Ben

On Thu, Jan 9, 2014 at 9:59 PM, Abdi Gudda <wal...@gmail.com> wrote:
> Thanks Ben. I don't want overlap, but dodging is fine.
>
> Thanks
>

Simbo

unread,
Jan 10, 2014, 3:26:11 AM1/10/14
to Ben Bond-Lamberty, ggplot2

Dear Ben, 


Thousand hanks for your input. I have produced non-overlapping error bars. But I need some help on the following. I am absolute beginner for ggplot and doing my best to learn , so bear with me.

1.       In the present stand the three error bars are not identifiable, it is impossible to know which bar belongs to which cat. How could I overcome that?

2.       The font for axes are small, especially the fonts for firm. How can I modify that ?

3.       Finally how can I modify the background? It is a bit dark.

Many thanks

Ista Zahn

unread,
Jan 10, 2014, 6:32:27 AM1/10/14
to Simbo, Ben Bond-Lamberty, ggplot2
Hi,

It sounds like you need to work through a basic ggplot2 tutorial. You
can use mine (http://projects.iq.harvard.edu/rtc/event/r-graphics ) or
just google for one.

Best,
Ista

Ben Bond-Lamberty

unread,
Jan 10, 2014, 7:44:07 AM1/10/14
to ggplot2
I absolutely agree with Ista: time spent doing a tutorial will be well
spent. That said, briefly:

1. ggplot(df, aes(x = Firm, y = percent, color=cat, ...
2. http://www.cookbook-r.com/Graphs/Axes_(ggplot2)/#axis-labels-and-text-formatting
3. +theme_bw()

Ben

Abdi Gudda

unread,
Jan 11, 2014, 7:06:27 AM1/11/14
to ggp...@googlegroups.com

Thanks Ben for your help and Ista for the link to your useful tutorial. 
Reply all
Reply to author
Forward
0 new messages