-
Notifications
You must be signed in to change notification settings - Fork 2.1k
geom_bar
inconsistently handling date values
#2047
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.
I came across a case which touches upon the first plot's inconsistent gaps you pointed out. library(ggplot2)
library(lubridate)
d6 <- data.frame(
dates = rep(make_date(year = 2017, month = 1:3), each = 2),
highlight = c(TRUE, FALSE, TRUE, FALSE, FALSE, FALSE)
)
ggplot(d6, aes(x = dates)) +
geom_bar(aes(fill = highlight)) I attempted to calculate the width ahead of time using # using d6 data frame from above
ggplot(d6, aes(x = dates)) +
geom_bar(aes(fill = highlight), width = resolution(as.numeric(d6$dates))) |
This comment has been minimized.
This comment has been minimized.
Minimal reprex: library(ggplot2)
df <- data.frame(
x = c(0, 0, 2, 1),
fill = c(TRUE, TRUE, TRUE, FALSE)
)
ggplot(df, aes(x, fill = fill)) + geom_bar()
#> Warning: position_stack requires non-overlapping x intervals ggplot(df, aes(x, fill = fill)) + geom_bar(width = 0.9) I think the root cause is that |
For what it is worth, this bugs results in issues with the RECON package incidence: library(incidence)
set.seed(1)
dates <- as.Date("2018-01-01") + sample(1:20, 100, replace = TRUE)
dates_posix <- as.POSIXct(dates)
plot(incidence(dates)) plot(incidence(dates_posix)) |
To be clear, are you stating that this is not a problem with the released version of ggplot2? |
This problem @hadley, as per you example in #2047 (comment) and per my own difficulty in plotting similar data, does still exist in ggplot2 3.2.1 |
This issue have return in the new released version of ggplot 3.3.0 available in CRAN
|
@avallecam that's a different problem. Please open a new issue with a reprex. |
I think it is indeed another issue. I have posted a reprex already in this issue. |
The report above, which Kara closed, is a duplicate. So, moving forward: how could the Line 76 in ac2b5a7
|
The basic issue here is that the group aesthetic is derived from fill meaning that the resolution of the second group is computed to be much higher... Setting library(ggplot2)
df <- data.frame(
x = c(0, 0, 2, 1),
fill = c(TRUE, TRUE, TRUE, FALSE)
)
ggplot(df, aes(x, fill = fill, group = x)) + geom_bar() Created on 2021-04-13 by the reprex package (v2.0.0) I think we can safely move the |
hmm, actually... a question then arrises on whether the width should be calculated per-panel or globally. |
Uh oh!
There was an error while loading. Please reload this page.
Description
There are unexpected plot results when specifying the fill aesthetic in
geom_bar
when thex
aesthetic is a Date or POSIXct value. Below I have listed examples using Date and POSIXct objects, respectively. Similar data represented with these two classes results in rather different plots, see below.Date Examples
The following examples use Date objects produced with
make_date()
.1 month with 2 fill values
In this example January fill values are TRUE and FALSE, February and March fill values are only FALSE.
1 month with distinct fill value
In this example January fill values are only TRUE, both February and March are only FALSE.
3+ fill values
This example introduces a third fill value to the highlight column.
POSIXct Examples
The following examples use POSIXct objects produced with
make_datetime()
.1 month with distinct fill value
In this example, when January fill is only FALSE, February and March are only TRUE, the January bar does not show. The bar may be too thin to see.
3+ fill values
Similar the 3+ fill value example above, however in this example the bars are not equally thin, they are all missing.
Conclusion
The
geom_bar
fill aesthetic is not properly handling Date and POSIXct objects. I am not weighing in yet on whether bars ought to be thin or wide, rather I am hoping to iron outgeom_bar
so Date and POSIXct values are handled consistently. I hope I have not misunderstood howgeom_bar
is intended to handle date values.I will look under the hood to try and identify the problem. For now I am not sure of a work around.
The text was updated successfully, but these errors were encountered: