I'm trying to understand how to correctly pass the output from a stat_summary fun(ction) to a geom. By trial and error I have a working example for geom_point and geom_hline, but the correct incantation for geom_linerange (the one I actually want to use) eludes me.
The error message I get is:
ggplot( testData, aes(x=a, y=b) ) +
...
stat_summary( aes(y = after_stat(y), xmin = a-1, xmax=a+1), fun = mean, geom = "linerange", color="green" )
! `stat_summary()` requires the following missing aesthetics: y.
I can't figure out from the documentation, nor from my own experiments, quite how this is meant to work and I'm wondering from the lack of examples that google can find for me if it can be used like this at all. Maybe there's a symbol clash between the expected parameters of linerange (y) and something else?
Full code:
ggplot( testData, aes(x=a, y=b) ) +
geom_point( ) +
geom_linerange( aes(xmin = a-1, xmax = a+1) ) +
stat_summary( fun = "mean", geom = "point", color="red" ) +
stat_summary( aes(yintercept = after_stat(y)), fun = mean, geom = "hline", color="blue") +
stat_summary( aes(y = after_stat(y), xmin = a-1, xmax=a+1), fun = mean, geom = "linerange", color="green" )
Sample data:
testData =
tribble(~a, ~b, ~g,
1, 1, 1,
1, 2, 1,
1, 3, 1,
1, 4, 1,
5, 5, 2,
5, 6, 2,
5, 7, 2,
5, 8, 2,
-1, -1, 3
)
Update:
Fwiw, I've discovered that I can break geom_point in the same way by coding an after_stat such as:
stat_summary( aes(y = after_stat(y)), fun = "mean", geom = "point", color="red" )