ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Width argument to geom_errorbar not passed on when using stat_summary_bin

Open hummuscience opened this issue 4 years ago • 8 comments

When using stat_summary_bin and geom = "errorbar" the width argument used to set the width of the errorbars in geom_errorbar does not work.

The below code produces the same plots with different setting for width

Related to follwing StackOverflow issue: https://stackoverflow.com/questions/42418746/how-to-adjust-width-of-errorbar-created-with-stat-summary-bin

library(tidyverse)

iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  stat_summary_bin(geom = "errorbar")
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`

iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  stat_summary_bin(geom = "errorbar", width = 0.1)
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`

iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  stat_summary_bin(geom = "errorbar", width = 2)
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`

Created on 2021-10-20 by the reprex package (v2.0.1)

hummuscience avatar Oct 20 '21 14:10 hummuscience

Yeah, this is an unfortunate consequence when stats and geoms share an aesthetic or parameter name. Fixing this will be surprisingly difficult I'm afraid

thomasp85 avatar Oct 28 '21 06:10 thomasp85

Is it because width can be used to set binwidth in stat_summary_bin? Or does width affect something else?

hummuscience avatar Oct 28 '21 11:10 hummuscience

hum... that was my thinking writing this, but now I'm not so sure 😬

I'll have to dig in a bit more when I sit down to fix the issue

thomasp85 avatar Oct 28 '21 12:10 thomasp85

If it is any help, I could try to dig for the reason. I don't think that I am at the level of actually writing a fix, but maybe I could find out what is causing it...

hummuscience avatar Oct 28 '21 12:10 hummuscience

That is very welcome 🙏

thomasp85 avatar Oct 28 '21 12:10 thomasp85

Ah, I can see where things run into each other. stat_summary_bin does not take a width paramater or aesthetic, but it does calculate one based on the given bins/binwidth argument (or its default). Since that is now present in the data given to geom_errorbar it takes precedence over the provided width parameter...

So, my initial hunch was correct, it was just a bit more complicated. I'm afraid we don't have a great solution for this at the moment. I'll keep this issue open to remind us to think about it

thomasp85 avatar Nov 11 '21 11:11 thomasp85

You can use after_stat() to work around this limitation.

library(ggplot2)

iris |>
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  stat_summary_bin(
    aes(width = after_stat(1)),
    geom = "errorbar"
  )
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`
#> No summary function supplied, defaulting to `mean_se()`

Created on 2024-03-28 with reprex v2.1.0

teunbrand avatar Mar 28 '24 14:03 teunbrand

Should a parameter width override the width column in the data? For basically any other aesthetic given as a parameter, say colour = "red", we override any colour variable passed through aes() or computed by the stat.

teunbrand avatar Mar 28 '24 14:03 teunbrand

It is also a bit verbose the throw the warning about the summary function 3 times

teunbrand avatar Jul 03 '24 08:07 teunbrand