Why no position_overlap() for geom_bar?

229 views
Skip to first unread message

Ryan Hope

unread,
Feb 11, 2013, 8:19:19 PM2/11/13
to ggp...@googlegroups.com
Why is there no such thing as position_overlap() for bar charts?
position_dodge() wastes too much space. I want to make a bar chart
where the bars overlap and are rendered with the tallest bar in back
and the shortest bar in front.

--
Ryan Hope, M.S.
CogWorks Lab
Cognitive Science Department
Rensselaer Polytechnic Institute

Ista Zahn

unread,
Feb 11, 2013, 8:38:24 PM2/11/13
to Ryan Hope, ggp...@googlegroups.com
It's called position_identity

-Ista
> --
> --
> 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.
>
>
>

Ryan Hope

unread,
Feb 12, 2013, 12:15:51 AM2/12/13
to Ista Zahn, ggp...@googlegroups.com
Hmm, I guess when I see "identity" I always assume its referring to
stat_identity()

Ryan Hope

unread,
Feb 12, 2013, 1:47:50 AM2/12/13
to Ista Zahn, ggp...@googlegroups.com
Actually, position_identity() does not do what you what I want it to.
The bars are overlapped but they are not layered in a way that makes
all the fill factors visible.

Ryan Hope

unread,
Feb 12, 2013, 2:02:47 AM2/12/13
to Ista Zahn, ggp...@googlegroups.com
Simple example:

ggplot(data.frame(x=rep(c("A","B"),each=3),y=rep(c("X","Y","Z"),times=2),z=c(1,2,3,2,3,1)))
+ geom_bar(aes(x=x,y=z,fill=y),position="dodge")

ggplot(data.frame(x=rep(c("A","B"),each=3),y=rep(c("X","Y","Z"),times=2),z=c(1,2,3,2,3,1)))
+ geom_bar(aes(x=x,y=z,fill=y),position="identity")

In the second example much of the data is hidden.

Brandon Hurr

unread,
Feb 12, 2013, 2:44:52 PM2/12/13
to Ryan Hope, Ista Zahn, ggp...@googlegroups.com
Give them an alpha and they'll all show up. The issue is if your factor levels are always in the same order of quantity or not. If they are, you can easily do this, if they aren't... I don't believe you can reorder a factor for each bar. You could do some fancy grid editing, but it isn't worth it unless you really need it. Consider an alternative visualization method?

Ryan Hope

unread,
Feb 12, 2013, 2:50:13 PM2/12/13
to Brandon Hurr, Ista Zahn, ggp...@googlegroups.com
Alpha is not a good enough solution because then the key does not make
sense. I know I can't reorder a factor for each bar, hence the
problem. There needs to be some built in way to make overlapping bar
charts work better. The current implementation is stupid. What
possible benefit is there to having overlapping bars if they are just
going to hide data?

Brandon Hurr

unread,
Feb 12, 2013, 2:57:31 PM2/12/13
to Ryan Hope, Ista Zahn, ggp...@googlegroups.com
Ryan, 
What is it you are trying to visualize? Is a stacked bar chart that alternates the order of the bars really going to easily show your reader what you want them to see, without issues of mis-interpretation? From my perspective, even if you could do what you want, it seems like the position="dodge" plot is much better for comparison within each x-axis value than your current ideal could. Just my opinion though.

Brandon


Ryan Hope

unread,
Feb 12, 2013, 3:08:40 PM2/12/13
to Brandon Hurr, Ista Zahn, ggp...@googlegroups.com
I have nearly 80 factors on the x-axis and with 3 fill factors, thats
a lot of individual bars in dodge mode, makes things unnecessarily
small. If you need to see for your self ->
http://glimmer.rstudio.com/ryanhope/hops

Brandon Hurr

unread,
Feb 12, 2013, 3:31:40 PM2/12/13
to Ryan Hope, Ista Zahn, ggp...@googlegroups.com
I see your problem.At 80 variables, I don't know if you could expect the reader to get much out of it anyway. 

Although, I wonder if you could do it with layers. 

If you hard code your colors and plot subsets of data (tallest first to shortest last)... mayhaps? 

I've never done that sort of thing before, but I bet you could do it that way. It's messy though. Perhaps someone else knows a better way?

Ben Bolker

unread,
Feb 12, 2013, 4:34:17 PM2/12/13
to ggp...@googlegroups.com
I wouldn't try to insist that the OP plot things in any particular
way, *but* I would have the following thoughts/suggestions, which
parallel lots of advice given in stat-graphics fora over the years:

* try a dot plot (geom_point()) rather than a bar plot: this will
naturally solve the overlapping problem you have. For a percent
composition score, it might also make sense to use stacked bars ...
although I see that these percentages don't add up to 1 ...
* use coord_flip() to turn the graph sideways so that labels can be
read more easily, without needing additional rotation (you also get more
usable room on a standard portrait-layout page)
* unless people really need to use the graph to look up the values of
_specific_ hops variety, consider sorting them in some way more
meaningful than alphabetically [oh, OK, I see you have a "sort by"
button ...]
* consider using scale_colour_brewer() to get dichromat-friendly colours
> <brando...@gmail.com <mailto:brando...@gmail.com>>
> <rmh...@gmail.com <mailto:rmh...@gmail.com>>
> >> >> > wrote:
> >> >> >> Hmm, I guess when I see "identity" I always assume its
> referring to
> >> >> >> stat_identity()
> >> >> >>
> >> >> >> On Mon, Feb 11, 2013 at 8:38 PM, Ista Zahn
> <ista...@gmail.com <mailto:ista...@gmail.com>>
> >> >> >> wrote:
> >> >> >>> It's called position_identity
> >> >> >>>
> >> >> >>> -Ista
> >> >> >>>
> >> >> >>> On Monday, February 11, 2013, Ryan Hope <rmh...@gmail.com
> <mailto:rmh...@gmail.com>> wrote:
> >> >> >>>> Why is there no such thing as position_overlap() for bar
> charts?
> >> >> >>>> position_dodge() wastes too much space. I want to make a
> bar chart
> >> >> >>>> where the bars overlap and are rendered with the tallest
> bar in
> >> >> >>>> back
> >> >> >>>> and the shortest bar in front.
> >> >> >>>>
> >> >> >>>> --
> >> >> >>>> Ryan Hope, M.S.
> >> >> >>>> CogWorks Lab
> >> >> >>>> Cognitive Science Department
> >> >> >>>> Rensselaer Polytechnic Institute
> >> >> >>>>
> >> >> >>>> --
> >> >> >>>> --
> >> >> >>>> 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
> <mailto:ggp...@googlegroups.com>
> >> >> >>>> To unsubscribe: email
> ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@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
> <mailto:ggplot2%2Bunsu...@googlegroups.com>.
> <mailto:ggp...@googlegroups.com>
> >> >> To unsubscribe: email ggplot2+u...@googlegroups.com
> <mailto:ggplot2%2Bunsu...@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
> <mailto:ggplot2%2Bunsu...@googlegroups.com>.

Winston Chang

unread,
Feb 12, 2013, 8:50:29 PM2/12/13
to Ryan Hope, Brandon Hurr, Ista Zahn, ggp...@googlegroups.com
The "stupid" current implementation isn't designed to make the bar graphs that you have in mind. All position="identity" does is tell ggplot to leave the geoms in place, without any modification. Each color-set of bars is added as a layer to the plot, and the blue ones happen to be on the top layer in your example.

The question of "what possible benefit is there to having overlapping bars" isn't the driving issue behind position="identity". The identity position adjustment can be used for any geom, not just bars, and it is indeed used as the default for many geoms.


Regarding your desired output: the kind of graph you're asking for would look just like a stacked bar plot (though possibly with an inconsistent stacking order, depending on the data) and could easily be confused for one. I don't think I've seen any examples of overlaid bars where the layering order depends on the bar height.

You could have each group with different widths, although it's potentially misleading since the areas aren't quite proportional to the value:

dat <- data.frame(
  x=rep(c("A","B"),each=3),
  y=c(1,2,3,2,3,1),
  z=rep(c("X","Y","Z"),times=2))

dat$width <- .9
dat$width[dat$z=="Y"] <- .75
dat$width[dat$z=="Z"] <- .6

ggplot(dat, aes(x=x,y=y,fill=z, width=width)) +
  geom_bar(position="identity", stat="identity", colour="black")


A good alternative is a Cleveland-style dot plot, as Ben suggested.

-Winston

LLP

unread,
Feb 17, 2016, 7:02:16 AM2/17/16
to ggplot2, brando...@gmail.com, ista...@gmail.com, rmh...@gmail.com
Ryan,

You can achieve what you want by adding the data on separate geom_bar calls:

ggplot('the data you want at the back', aes(x=x,y=z,fill=y)) #note that aes is the same for all the calls
+ geom_bar(position="identity") 
+ geom_bar(data= 'the variable you want in the front', position="identity") 

Still, a position_overlap would be much better.
I hope this helps.
LLP
Reply all
Reply to author
Forward
0 new messages