I have a faceted ggplot with free scales. Now I want to annotate this plot at the same RELATIVE Position at each facet. This would be easy to do, if I knew how to get the range/limits of the scale of each facet. QUESTION: Is there any way to get the scales of each facet in ggplot?
In the following example, I did it manually. The question would be how to construct facet_scales
by a function which uses a2
(which would also be more exact). If i call str(a2)
, it gives me an explanation of a2$scales
. It also states that a useful function could be get_scales()
. Lamentably, i don't know how to call this function.
library(ggplot2)
a <- ggplot(data = msleep, aes(x = bodywt, y = sleep_total))+ geom_point()
a2 <- a + facet_wrap(~vore, scales="free")
a2
facet_scales = data.frame(vore=c(levels(msleep$vore), NA),
x_min = c(0,0,0,0,0),
x_max = c(800,7000,60,90,4),
y_min = c(0,0,5,7.5,5),
y_max = c(20,18,21,19,15)
)
a2 + geom_rect(data=facet_scales, mapping =aes(xmin=x_min, xmax=x_max, ymin=y_min,
ymax=(y_max+y_min)/3, x=NULL, y=NULL ), color='yellow', alpha=0.2)