Skip to content

position_stack() should stack +ive & -ve values separately #1691

Closed

Description

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

Activity

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

    @thomasp85
    Member

    There are a lot of case where, even if supported, negative values doesn't make sense when stacking. As I see it it only makes sense for stacked bar charts - can you come with other examples...

    I'm asking because I need to know which types of checks to make and warnings to throw, and also where it should be solved. It could be handled directly by pos_stack() but this function is also called by pos_fill() where it doesn't make sense to have negative values. Alternatively (and easier) the data could be split into two calls to collide in PositionStack$compute_panel() which would make the functionality specific to PositionStack (though it would still be a mess if coupled with geom_area

  4. added a commit that references this issue on Aug 3, 2016
    00eedd1
  5. hadley commented on Aug 3, 2016

    @hadley
    MemberAuthor

    Can you come with an example where stacking makes sense apart from bars/areas? I think that's the only case we need to worry about, and it's reasonable for positive values to stack upwards from the y-axis, and negative values to stack downwards.

    Your fix seems reasonable to me - I don't think there's any way to have an elegant solution without completely rewriting the stacking (which we don't want to do)

  6. thomasp85 commented on Aug 3, 2016

    @thomasp85
    Member

    I cannot - which was why I asked whether you knew of any other use cases. I'll move forward with #1693

  7. thomasp85 commented on Aug 3, 2016

    @thomasp85
    Member

    @smouksassi true. Dot plots should mainly be used for count data though, which makes them unsuitable to show negative values.

  8. smouksassi commented on Aug 3, 2016

    @smouksassi
  9. hadley commented on Aug 3, 2016

    @hadley
    MemberAuthor

    Dot plots have to use a completely different algorithm so aren't relevant to this discussion.

  10. wmurphyrd commented on Aug 4, 2016

    @wmurphyrd

    Here's an example where the current behavior of position_stack for a mixture of positive and negative values produced the desired result: the total extent of the bar aligns with the sum of the values (please ignore the politics, just examples that I happened to have handy).
    image. However, I did zoom-hide the negative side because it's not clear what's going on. (From https://github.com/wmurphyrd/BernieTax )

    In another example, I wanted to show what was happening on the negative side with a dodge:
    image. Although, in the end I used geom_tile with position_dodge after calculating the offsets manually

  11. thomasp85 commented on Aug 5, 2016

    @thomasp85
    Member

    I do think that these examples, while potentially useful to you, violates the general expectations of stacking, namely that stacks are build from the ground and up (or down), that is, they extend from zero

  12. hadley commented on Aug 5, 2016

    @hadley
    MemberAuthor

    Cool plots, and it is a nice use, but I think it also violates the principle that you expect position_stack() to avoid overlaps (i.e. even in this case you want to dodge as well to show the direction of each change).

  13. added 2 commits that reference this issue on Aug 8, 2016
    a0be60e
    cf716a3
  14. 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

feature

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions