You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've encountered behaviour with geom_bar() which may be a bug. I noticed the behaviour in a bar chart, where the x-axes contained POSIXct dates, while trying to highlight a single year by adding a new layer with a subsetted data.frame with nrow == 1. The expected chart is generated when using a data.frame with length > 1 (see reprex). The behaviour does not ocurr with numeric x values and axis.
The issue may be related to #2047.
Thanks,
Alex
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.3# with posixct axis --------------------------------------------------------start<- as.POSIXct("2020-01-01")
end<- as.POSIXct("2030-01-01")
dates<- seq(start, end, length.out=11)
values<- (1:11)
df<-data.frame(date=dates, value=values)
filter_date_single_row<-"2029-01-01"# assert that we will try to add a df with a single rowif(nrow(df[df$date> as.POSIXct(filter_date_single_row), ]) ==1){
ggplot(df) +
geom_bar(aes(x=date, y=value, fill="base"), stat="identity") +# add single row
geom_bar(
inherit.aes=FALSE,
data=df[df$date> as.POSIXct(filter_date_single_row), ],
aes(x=date, y=value, fill="add"),
stat="identity")
}
filter_date_multiple<-"2025-01-01"# assert that we will try to add a df with more than one row rowif(nrow(df[df$date> as.POSIXct(filter_date_multiple), ]) >1){
ggplot(df) +
geom_bar(aes(x=date, y=value, fill="base"), stat="identity") +# add single row
geom_bar(
inherit.aes=FALSE,
data=df[df$date> as.POSIXct(filter_date_multiple), ],
aes(x=date, y=value, fill="add"),
stat="identity")
}
str(df[df$date> as.POSIXct(filter_date_single_row), ])
#> 'data.frame': 1 obs. of 2 variables:#> $ date : POSIXct, format: "2030-01-01"#> $ value: int 11
str(df[df$date> as.POSIXct(filter_date_multiple), ])
#> 'data.frame': 5 obs. of 2 variables:#> $ date : POSIXct, format: "2025-12-31 19:12:00" "2027-01-01 02:24:00" ...#> $ value: int 7 8 9 10 11# with numeric X-Axis -----------------------------------------------------x<-1:11values<- (1:11)
df<-data.frame(x=x, value=values)
# this worksfilter_x_single_row<-10# assert that we will try to add a df with a single rowif(nrow(df[df$x>filter_x_single_row, ]) ==1){
ggplot(df) +
geom_bar(aes(x=x, y=value, fill="base"), stat="identity") +# add single row
geom_bar(
inherit.aes=FALSE,
data=df[df$x>filter_x_single_row, ],
aes(x=x, y=value, fill="add"),
stat="identity")
}
You are adding the data without any problems, it is just that there is no reasonable way to determine a good width from the data (and hence the solo bar becomes very thin). If you set that manually, it should work as intended. However, I don't think there is an easy way to set a date-width, so in the example below I've opted for the awkward 28405728.
Thanks for the hint - I was wondering if this was the issue and had been tinkering with width, also using resolution(). There I was only passing the original dates, which resulted in a width of one. But using resolution(as.numeric(df$date)) gives what appears to be the correct value in a similiar order of magnitude as in your example. I will close the issue - thank you! Do you reckon a warning message or hint for geom_bar/col when adding a layer with length-1 POSIXct data would be useful? Something along the lines of "The width of your bar is possibly off. Consider setting it manually, for example, with resolution()?
Dear devs,
I've encountered behaviour with
geom_bar()
which may be a bug. I noticed the behaviour in a bar chart, where the x-axes contained POSIXct dates, while trying to highlight a single year by adding a new layer with a subsetteddata.frame
withnrow == 1
. The expected chart is generated when using adata.frame
with length > 1 (see reprex). The behaviour does not ocurr with numeric x values and axis.The issue may be related to #2047.
Thanks,
Alex
Created on 2024-09-26 with reprex v2.1.1
ggplot2 Version: 3.5.1
R: 4.2.2
The text was updated successfully, but these errors were encountered: