Skip to content

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

Merged
merged 1 commit into from
Aug 8, 2016

Conversation

thomasp85
Copy link
Member

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.

@thomasp85
Copy link
Member Author

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")

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'))

stack2

@thomasp85
Copy link
Member Author

This is very related to #1691 so I'll try to solve it within the same PR with @hadley's blessing.

@thomasp85
Copy link
Member Author

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")

neg-stack

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

@hadley
Copy link
Member

hadley commented Aug 3, 2016

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().

@hadley
Copy link
Member

hadley commented Aug 5, 2016

Do we need a similar tweak for position_fill()?

@thomasp85
Copy link
Member Author

I can't really wrap my head around how position fill should behave with negative values...

@hadley
Copy link
Member

hadley commented Aug 5, 2016

@thomasp85 hmmmm, I was thinking it we'd apply the same principle and fill separately for positive and negative

@thomasp85
Copy link
Member Author

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?

@hadley
Copy link
Member

hadley commented Aug 5, 2016

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)

@thomasp85
Copy link
Member Author

@hadley do you want stronger wording/more emphasis in the news?

@thomasp85
Copy link
Member Author

thomasp85 commented Aug 8, 2016

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be position_stack()

@hadley
Copy link
Member

hadley commented Aug 8, 2016

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
@thomasp85
Copy link
Member Author

How do you handle roxygenize? once, prior to release, or for every PR?

@hadley
Copy link
Member

hadley commented Aug 8, 2016

Every PR.

@thomasp85 thomasp85 merged commit c2d91dc into tidyverse:master Aug 8, 2016
@hadley hadley removed the in progress label Aug 8, 2016
@thomasp85 thomasp85 deleted the bug-area-stack branch August 8, 2016 15:03
@lock
Copy link

lock bot commented Jan 18, 2019

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/

@lock lock bot locked and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stacked area charts not stacking
2 participants