Warning about binwidth given with geom_bar and stat = bin

Based on the documentation, I can see that binwidth is deprecated as an argument for geom_bar with the default stat of count. However, my understanding is that geom_bar with stat = bin is essentially equivalent to geom_histogram. If so, then why is there a warning about using binwidth with geom_bar and stat = bin?

Histogram using geom_bar and stat = bin:

library(ggplot2)
ggplot(mpg, aes(x = displ)) + geom_bar(stat = "bin", binwidth = 1)

This gives the warning

Warning message: geom_bar() no longer has a binwidth parameter. Please use geom_histogram() instead.

Histogram using geom_histogram (no warning):

library(ggplot2)
ggplot(mpg, aes(x = displ)) + geom_histogram(binwidth = 1)
library(ggplot2)
# Both geoms USE the binwidth argument
ggplot(mpg, aes(x = displ)) + geom_bar(stat = "bin", binwidth = 0.5)
#> Warning: `geom_bar()` no longer has a `binwidth` parameter. Please use
#> `geom_histogram()` instead.

ggplot(mpg, aes(x = displ)) + geom_histogram(binwidth = 1)

Created on 2020-01-29 by the reprex package (v0.3.0)

It's not yet an error to provide a bindwidth argument to geom_bar(). The warning is a heads up that future releases will not necessarily have ... that will continue the same behavior; ∴ future proof your code now.

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)

I'm now out-of-pay-grade. You might consider posting this as an issue

When you do post an issue (as I see you did), could you please also link back to it here? It makes it easier for those who see this in the future to follow along. Thanks!

1 Like

Ah yes, definitely will do in the future. Thank you!!

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.