In the plot below of the data in foo
, how can put the text "50%" on the right margin at the median value of each facet? I imagine it's a a geom_text
with coord_cartesian(clip = "off")
but I don't know how to incorporate the facets.
foo <- data.frame(x=rep(1:50,times=2),y=c(rnorm(50,mean=2),rnorm(50,mean=4)),
z=rep(c("A","B"),each=50))
bar <- foo %>% group_by(z) %>% summarise(med=median(y))
ggplot(foo,aes(x,y,color=z)) + geom_point() +
geom_hline(data=bar,aes(yintercept = med,col=z)) +
facet_wrap(z~.,ncol = 1) +
theme(legend.position = "none",
plot.margin = unit(c(1,3,1,1), "lines"))
# place "50%" in right margin at bar$med for each facet