Skip to content

Stacked area charts not stacking #1552

Closed

Description

@karawoo

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?

Activity

  1. added
    bug
    on Jul 28, 2016
  2. added this to the v2.2.0 milestone on Jul 28, 2016
  3. thomasp85 commented on Aug 2, 2016

    @thomasp85
    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...

  4. self-assigned this
    on Aug 2, 2016
  5. hadley commented on Aug 2, 2016

    @hadley
    Member

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

  6. thomasp85 commented on Aug 3, 2016

    @thomasp85
    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...

  7. hadley commented on Aug 3, 2016

    @hadley
    Member

    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.

  8. thomasp85 commented on Aug 3, 2016

    @thomasp85
    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...

  9. locked as resolved and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions