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

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

Closed
hadley opened this Issue Aug 2, 2016 · 10 comments

Comments

Projects
None yet
4 participants
@hadley
Member

hadley commented Aug 2, 2016

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

This comment has been minimized.

Member

thomasp85 commented Aug 3, 2016

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

thomasp85 added a commit to thomasp85/ggplot2 that referenced this issue Aug 3, 2016

Fixes tidyverse#1691. Positive and negative y-values are now calculat…
…ed separately to allow stacking below the x-axis
@hadley

This comment has been minimized.

Member

hadley commented Aug 3, 2016

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)

@thomasp85

This comment has been minimized.

Member

thomasp85 commented Aug 3, 2016

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

@smouksassi

This comment has been minimized.

smouksassi commented Aug 3, 2016

@thomasp85

This comment has been minimized.

Member

thomasp85 commented Aug 3, 2016

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

@smouksassi

This comment has been minimized.

smouksassi commented Aug 3, 2016

I was thinking about a kind of back to back dotplot like this: https://nzmaths.co.nz/resource/fully-stretched:

@hadley

This comment has been minimized.

Member

hadley commented Aug 3, 2016

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

@wmurphyrd

This comment has been minimized.

wmurphyrd commented Aug 4, 2016

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

@thomasp85

This comment has been minimized.

Member

thomasp85 commented Aug 5, 2016

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

@hadley

This comment has been minimized.

Member

hadley commented Aug 5, 2016

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

thomasp85 added a commit to thomasp85/ggplot2 that referenced this issue Aug 8, 2016

Reorder by group as well as x
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 thomasp85 closed this in cf716a3 Aug 8, 2016

@hadley hadley removed the ready 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.