Skip to content

Stacked area charts not stacking #1552

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

Closed
karawoo opened this issue Feb 21, 2016 · 5 comments · Fixed by #1693
Closed

Stacked area charts not stacking #1552

karawoo opened this issue Feb 21, 2016 · 5 comments · Fixed by #1693
Assignees
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@karawoo
Copy link
Member

karawoo commented Feb 21, 2016

When I try to create a stacked area chart using the following code the segments don't stack, and they occlude each other instead.

library("ggplot2")
library("dplyr")

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

unstacked_area

However if I reorder the rows so all of the var values are grouped together, it does create a stacked chart:

dat %>%
  arrange(var, x) %>%
  ggplot(aes(x = x, y = y, fill = var)) +
  geom_area(position = "stack")

stacked_area

Is this a bug, or is the ordering of the rows meant to matter like this?

@hadley hadley added bug an unexpected problem or unintended behavior ready labels Jul 28, 2016
@hadley hadley added this to the v2.2.0 milestone Jul 28, 2016
@thomasp85
Copy link
Member

It sort of makes weird sense as the order at each x-value follows the order it occurs in in the data. I would suggest we add a sorting step to position_stack - In this way we can also get the stacking order to follow the legend order...

@hadley thoughts? It might change the appearance of some plots, though I would argue that the benefit of the bug fix and sorting would alleviate that...

@thomasp85 thomasp85 self-assigned this Aug 2, 2016
@hadley
Copy link
Member

hadley commented Aug 2, 2016

I like the idea of matching the stacking order to the legend order. That would also help with #1593

@thomasp85
Copy link
Member

It would not be bulletproof, i.e. it is only possible to match the stacking to the default legend order and not to any modified order defined in scale_*, that is, we can sort only by the data itself as position doesn't have access to the scale object...

@hadley
Copy link
Member

hadley commented Aug 3, 2016

Oh that's true - and it would mean that to change the stacking order you'd be forced to change the levels of a factor. Hmmm.

@thomasp85
Copy link
Member

I think this is reasonable. If well documented it is not too much to ask that they reorder factor levels to achieve their desired stacking...

@hadley hadley removed the in progress label Aug 8, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants