library(tidyverse)
compute_group_xmean <- function(data, scales){
data %>%
summarise(x = mean(x)) |>
mutate(xend = x, y = I(0), yend = I(1) )
}
StatXmeanline <- ggproto("StatXmeanline",
Stat,
compute_group = compute_group_xmean)
ggplot(cars, aes(speed)) +
geom_rug() +
geom_segment(stat = StatXmeanline)