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
{{ message }}
Repository navigation
Width argument to geom_errorbar not passed on when using stat_summary_bin #4647
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()`
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
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...
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
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()`
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.
Activity
thomasp85 commentedon Oct 28, 2021
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
hummuscience commentedon Oct 28, 2021
Is it because
widthcan be used to setbinwidthinstat_summary_bin? Or doeswidthaffect something else?thomasp85 commentedon Oct 28, 2021
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
hummuscience commentedon Oct 28, 2021
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...
thomasp85 commentedon Oct 28, 2021
That is very welcome 🙏
thomasp85 commentedon Nov 11, 2021
Ah, I can see where things run into each other.
stat_summary_bindoes not take awidthparamater or aesthetic, but it does calculate one based on the givenbins/binwidthargument (or its default). Since that is now present in the data given togeom_errorbarit takes precedence over the providedwidthparameter...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
teunbrand commentedon Mar 28, 2024
You can use
after_stat()to work around this limitation.Created on 2024-03-28 with reprex v2.1.0
teunbrand commentedon Mar 28, 2024
Should a parameter
widthoverride thewidthcolumn in the data? For basically any other aesthetic given as a parameter, saycolour = "red", we override anycolourvariable passed throughaes()or computed by the stat.teunbrand commentedon Jul 3, 2024
It is also a bit verbose the throw the warning about the summary function 3 times