Changed stacking behaviour in geom_bar as of ggplot2 version 2.2.0

144 views
Skip to first unread message

Claude Vividsky

unread,
Dec 19, 2016, 10:31:19 AM12/19/16
to ggplot2

I have some stacked bar charts with stacked text labels produced by geom_bar and geom_text. According to the blog https://blog.rstudio.org/2016/11/14/ggplot2-2-2-0/ the stacking behaviour of geom_bar has been reversed such that the order of items is the same as in the legend. Because geom_text seems not to be changed accordingly, the charts are wrongly formatted now.


Is there a chance to change this? I suggest one of the following:

  1. revert to the old behaviour of geom_bar
  2. add an option to geom_bar to reverse the order (default is behaviour from 2.1.0: reverse = false)
  3. the new geom_col function may use the new ordering (default: reverse = true)
  4. Alternatively a less invasive change would be to reverse the order of the legend.
  5. If 1 - 4 are not an option please change geom_text according to geom_bar

Thank you for your support (and sorry if the post is posted twice)!

Claude

Hadley Wickham

unread,
Dec 30, 2016, 4:58:52 PM12/30/16
to Claude Vividsky, ggplot2
Please provide a reproducible example.

Hadley
> --
> --
> 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/d/optout.



--
http://hadley.nz

Claude Vividsky

unread,
Jan 3, 2017, 3:28:14 AM1/3/17
to Hadley Wickham, ggp...@googlegroups.com
I added an example here: https://github.com/tidyverse/ggplot2/issues/1965

Here is it too; please compare the plots generated by 2.1.0 to those
generated by 2.2.0:


library(ggplot2)

x <- data.frame(
a=c("A", "A", "B", "B"),
b=c("x", "y", "x", "y"),
m=c(4, 8, 7, 3)
)


# Example 1: label at the top of the bar segment

ggplot(x, aes(x=a, y = m)) +
geom_bar(stat="identity", aes(fill=a, alpha=b)) +
geom_text(position = "stack", aes(label=m), vjust=-0.3, size = 3) +
scale_alpha_discrete(range=c(1, 0.5))


# Example 2: label at the center of the bar segment

x = x[order(x$a, x$b),]
x$p = ave(x$m, x$a, FUN = cumsum)
x$p = x$p - x$m * 0.5

# p is a calculated label pos for the second example such that within each x$a
# category the labels are centered for the stacked x$b;
# e.g x$p[2] = x$m[1] + x$m[2] / 2

ggplot(x, aes(x=a, y = m)) +
geom_bar(stat="identity", aes(fill=a, alpha=b)) +
geom_text(position = "identity", aes(y=p, label=m), size = 3) +
scale_alpha_discrete(range=c(1, 0.5))


At least the second example goes wrong with ggplot2 2.2.0 (can't test the
first one with 2.2.0 at the moment; will do it this evening).

Of course it is possible to calculate x$p according to the new scheme. However,
I do not think that this is intuitively, because in 2.1.0 all x$m from the first
to the current must be summed up while in 2.2.0 all x$m from the last to the
current must be regarded. This is not as easy as here, because x$b must at first
be ordered descending to calculate the label pos and after that it must be
reordered ascending to confirm to the new display behaviour of ggplot2 2.2.0.

Thank you
Michael
Reply all
Reply to author
Forward
0 new messages