Thanks for answering! I see what you're saying, i.e. that particular warning is usually used to let users know that at some point the use of binwidth
will be deprecated and cause an error in future releases. I guess what I'm feeling is that this might be a bug (albeit a somewhat harmless one). In particular, the binwidth
argument is being displayed in the documentation when the default stat is count
.
geom_bar(mapping = NULL, data = NULL, stat = "count",
position = "stack", ..., width = NULL, binwidth = NULL,
na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)
Also note the documentation for stat_bin
showing binwidth
as a legit argument:
stat_bin(mapping = NULL, data = NULL, geom = "bar",
position = "stack", ..., binwidth = NULL, bins = NULL,
center = NULL, boundary = NULL, breaks = NULL,
closed = c("right", "left"), pad = FALSE, na.rm = FALSE,
show.legend = NA, inherit.aes = TRUE)
I did some testing and noted that even if you explicitly request stat = "count"
for geom_bar
and include a binwidth
argument, ggplot2 will override your stat request and use stat = "bin"
. To me, this seems like the reason to want to deprecate the binwidth
argument when stat is count. Here's a reprex showing what I'm trying (and hopefully not failing!) to communicate (also, thanks for reminding me to reprex!):
library(ggplot2)
# First, plot geom_bar specifying explicitly the default stat of count
(ggobj <- ggplot(iris, aes(x = Sepal.Length)) +
geom_bar(stat = "count"))

# ggplot2 object shows that stat is count, as specified
ggobj$layers[[1]]
#> geom_bar: width = NULL, na.rm = FALSE
#> stat_count: width = NULL, na.rm = FALSE
#> position_stack
# Now explicitly request stat count but include binwidth argument
(ggobj <- ggplot(iris, aes(x = Sepal.Length)) +
geom_bar(stat = "count", binwidth = 0.1))
#> Warning: `geom_bar()` no longer has a `binwidth` parameter. Please use
#> `geom_histogram()` instead.

# We get the warning, and it appears the binwidth argument overrides the stat argument to bin.
ggobj$layers[[1]]
#> geom_bar: na.rm = FALSE, width = NULL
#> stat_bin: binwidth = 0.1, bins = NULL, na.rm = FALSE, pad = FALSE, width = NULL
#> position_stack
#
# In this case, if this was expected behavior, I can understand the desire to deprecate this functionality.
# Now again let's request stat bin and include binwidth, which is a legitimate argument for stat bin
(ggobj <- ggplot(iris, aes(x = Sepal.Length)) +
geom_bar(stat = "bin", binwidth = 0.1))
#> Warning: `geom_bar()` no longer has a `binwidth` parameter. Please use
#> `geom_histogram()` instead.

# We get the warning even though, again, this seems to be a legit request to stat_bin. Here's the layer info showing stat to be bin, as requested.
ggobj$layers[[1]]
#> geom_bar: na.rm = FALSE, width = NULL
#> stat_bin: binwidth = 0.1, bins = NULL, na.rm = FALSE, pad = FALSE, width = NULL
#> position_stack
Created on 2020-01-30 by the reprex package (v0.3.0)