This is a peculiar behaviour and is semi-alluded to in Create geom_vline for mean value in a density plot, for a new variable in the dataframe, without create new tables.
Plot 1: When using a computed after_stat(y) as yintercept in stat_summary with a hline geom, and one doesn't explicitly pass an x aesthetic, then this returns multiple lines that do not have any obvious relation to the data (not obvious to me, that is). The respective happens with an xintercept with a vline geom when not specifying y.
Plot 2: When hard coding a x (or y) aesthetic, this is miraculously resolved. But it defeats the point of the entire exercise, because we want to define the intercept programmatically and not depend on any hard coded values.
My question is: Why does this happen?
I don't care too much for "how to solve this". (My solution would be simply to make my own stat, but if you've got something that can fix this using stat_summary, I'd be still keen to hear).
library(ggplot2)
library(patchwork) ## just for reprex
## Multiple lines
p1 <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
stat_summary(aes(yintercept = after_stat(y)), fun = mean, geom = "hline") +
labs(title = "Plot 1", caption = "Multiple lines -\ncorrespond to what exactly?")
## It works with a hard coded x
p2 <- ggplot(mpg, aes(displ, hwy)) +
geom_point() +
stat_summary(aes(x = 1, yintercept = after_stat(y)), fun = mean, geom = "hline") +
labs(title = "Plot 2", caption = "A hard coded x removes those")
p1 + p2
Created on 2023-04-13 with reprex v2.0.2
P.S. I came across this problem when trying to answer How can I pass variables between ggplot personal functions?