-
Notifications
You must be signed in to change notification settings - Fork 2.1k
geom_area with holes in the negative y-range #2802
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
Comments
This comment has been minimized.
This comment has been minimized.
This is because we are mixing negative values and zeros ( which are considered positive ?). The issues goes if you replace the zero y values by a small negative e.g. -0.01. |
Possibly related: #2803 |
I think we need to step back for a second and have a discussion about what stacking with negative areas is supposed to mean and do. It is not clear to me, and I'd say a priori it's not well defined. The current code seems to work (mostly) when all areas are negative, and it interprets that to mean that the stacking should go down rather than up. But when we mix positive and negative values, what should happen? Stacking implies that there should be no holes and no overlapping areas, but a mix of positive and negative numbers will possibly create one or the other or both. |
Fair question. My understanding of stacking with negative values is what the first graph of #2803 does (old behaviour), that is subtract from current height. That first chart is a reproduction of a chart made with some other software and published in a book, suggesting that it's the expected behaviour for some people at least. Also the reprex for #2803 (current behaviour) cannot possibly be the expected behaviour. |
The current code stacks positive values up and negative values down. It works fine when each series is entirely positive or entirely negative. However, when a series contains both positive and negative values, then things get wonky. I'm still not sure how the second example should be treated. library(ggplot2)
df <- data.frame(
id = rep(letters[1:4], each = 3),
x = rep(1:3, 4),
y = c(1, 2, 1, 2, 2, 3, -1, -2, -2, -3, -1, -2)
)
ggplot(df, aes(x, y, fill = id)) +
geom_area(position = "stack") df <- data.frame(
id = rep(letters[1:4], each = 3),
x = rep(1:3, 4),
y = c(1, 2, 1, 2, 2, 3, -1, 1, -1, -3, -1, -2)
)
ggplot(df, aes(x, y, fill = id)) +
geom_area(position = "stack") Created on 2018-08-04 by the reprex package (v0.2.0). |
@ptoche I'm not convinced the behavior you describe would be appropriate for For the current |
@clauswilke: a practical way to avoid the overlapping and the holes for mixed series would be to separate the positive and the negative series as showed below: library(ggplot2)
df <- data.frame(
id = rep(letters[1:4], each = 3),
x = rep(1:3, 4),
y = c(1, 2, 1, 2, 2, 3, -1, 1, -1, -3, -1, -2)
)
df$positive <- ifelse(df$y >= 0, df$y, 0)
df$negative <- ifelse(df$y < 0, df$y, -1e-36)
ggplot(df) +
geom_area(aes(x=x, y=positive, fill=id)) +
geom_area(aes(x=x, y=negative, fill=id)) Created on 2018-08-04 by the reprex package (v0.2.0). |
Given the discussion, it appears to me that the stacking behaviour is correct for simple cases (all positive and all negative) and it’s not clear what should happen for mixed cases. For that reason, I think this is out of scope for ggplot2: a fuller implementation would be more appropriate in an extension package. |
@clauswilke, yes I think your suggestion is reasonable. One could just pull the old code out and name it |
@ptoche I've thought about adding a warning, but the current behavior is completely fine for other geoms (see below). And the current behavior is properly documented. So I agree with @hadley that there's nothing to be done on the ggplot2 codebase itself. library(ggplot2)
df <- data.frame(
id = rep(letters[1:4], each = 3),
x = rep(1:3, 4),
y = c(1, 2, 1, 2, 2, 3, -1, 1, -1, -3, -1, -2)
)
ggplot(df, aes(x, y, fill = id)) +
geom_col(position = "stack") Created on 2018-08-04 by the reprex package (v0.2.0). |
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
As I already described here
https://stackoverflow.com/questions/51656490/holes-with-geom-area-ggplot2-for-negative-y-range
there is an issue with geom_area. When using geom_area with negative values, some holes arise on the negative y-range. Interestingly, this weird behavior does not occur on the positive y-range. Here is a reproducible example, that illustrates what I mean
Created on 2018-08-04 by the reprex package (v0.2.0).
In the first plot the problem is that x2 is changing from -1 to 0 and x3 from 0 to -1 at the same time, hence producing a hole. But again, this only seems to occur on the negative y-range. As you can see in the second plot, the stacking behavior of geom_area is completely satisfactory with no holes between the areas. Therefore my supposition that this may be due to a bug.
Thank you in advance for you help!
EDIT: As pointed out by @smouksassi (many thanks for this!), a quick & dirty workaround would be to set the zeros to some very small negative value as here:
Created on 2018-08-04 by the reprex package (v0.2.0).
The text was updated successfully, but these errors were encountered: