-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Sort within x prior to stacking #1693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
To continue @karawoo's example, with this fix you get: library("ggplot2")
## Create data
set.seed(40)
dat <- data.frame(x = rep(c(1:10), 3),
var = rep(c("a", "b", "c"), 10),
y = round(runif(30, 1, 5)))
## Plot
ggplot(dat, aes(x = x, y = y, fill = var)) +
geom_area(position = "stack") It is still not able to match stacking order if the scale has been manually modified: ggplot(dat, aes(x = x, y = y, fill = var)) +
geom_area(position = "stack") +
scale_fill_manual(breaks = c('c', 'a', 'b'), values = c('forestgreen', 'steelblue', 'firebrick')) |
Continuing @hadley's example 00eedd1 allows you to mix positive and negative y-values when stacking so you get: library(ggplot2)
library(tibble)
df <- frame_data(
~x, ~g, ~y,
1, 1, 1,
1, 2, -1,
1, 3, 1,
2, 1, 2,
2, 2, -3
)
ggplot(df, aes(x, y, fill= factor(g))) +
geom_bar(stat = "identity") Stacking order is as close to the default legend order as possible, but can get mixed up if one category jumps between negative and positive y-values |
This looks good. I think it needs a few unit tests (so we don't accidentally break again in the future), and a section in position_stack() docs that describes the algorithm. Changing the order of the bars is such a common question that it's probably also worth mentioning in the docs for geom_bar(). |
Do we need a similar tweak for |
I can't really wrap my head around how position fill should behave with negative values... |
@thomasp85 hmmmm, I was thinking it we'd apply the same principle and fill separately for positive and negative |
that would mean that positive and negative values received different scaling at the same point on the x-axis... And what if there are only negative values for some x? what to draw? |
LGTM - just needs a news bullet that makes it clear this is a big change (and we'll need to highlight in the blog post) |
@hadley do you want stronger wording/more emphasis in the news? |
9c81f16
to
a0be60e
Compare
I've squashed it and will merge (and close issues) if you approve the news |
@@ -1,5 +1,13 @@ | |||
# ggplot2 2.1.0.9000 | |||
|
|||
* position_stack and position_fill now sorts the stacking order so it matches |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be position_stack()
Looks good (apart from the small formatting issue). We'll definitely highlight this in the final release notes, and in the blog post. |
Fixes tidyverse#1691. Positive and negative y-values are now calculated separately to allow stacking below the x-axis Fix for geom_area geom_area doesn’t sort ymin and ymax so this needs to be handled here for correct results Add docs Adding documentation to position_stack Unit tests for the bug fixes/features Add bullets to news
a0be60e
to
cf716a3
Compare
How do you handle roxygenize? once, prior to release, or for every PR? |
Every PR. |
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
This fixes #1552.
collide
now sorts the data by x and then by negated group. The negation is performed to ensure that the stacking order follows the default legend order of the fill.