Skip to content

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

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
hadley opened this issue Aug 2, 2016 · 10 comments
Closed

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

hadley opened this issue Aug 2, 2016 · 10 comments
Assignees
Labels
feature a feature request or enhancement
Milestone

Comments

@hadley
Copy link
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")
@hadley hadley added feature a feature request or enhancement ready labels Aug 2, 2016
@hadley hadley added this to the v2.2.0 milestone Aug 2, 2016
@thomasp85
Copy link
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

thomasp85 added a commit to thomasp85/ggplot2 that referenced this issue Aug 3, 2016
…ed separately to allow stacking below the x-axis
@hadley
Copy link
Member Author

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

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

@smouksassi
Copy link

I think we can also stack dots which can be useful in some cases. http://www.lispworks.com/success-stories/inspiredata.html

http://docs.ggplot2.org/current/geom_dotplot.html

@thomasp85
Copy link
Member

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

@smouksassi
Copy link

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

@hadley
Copy link
Member Author

hadley commented Aug 3, 2016

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

@wmurphyrd
Copy link

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

@hadley
Copy link
Member Author

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
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
@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.
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants