Continuing the discussion from Accessing ggplot's inherited data object from a layer:
I wrote a function to annotate my plots manually. This draws a crosshatch at a target point and writes out the target value:
annopt <- function(x, y) {
list(annotate("point", x=x, y=y, size=5, shape=13),
annotate("text", x=-Inf, y=Inf, label=paste0(x,",",y), hjust=-0.2, vjust=2))
}
To annotate an plot I can tack this onto the end of a ggplot object:
p <- ggplot(iris, aes(x=Sepal.Length, y= Petal.Length, color=Species)) +
geom_point()
p + annopt(7,5.5)
I have a table of target values and they get used in numerous plots. I wish to write a function that inherits the x
and y
aesthetics from the ggplot object, looks up target values in a specification table, and returns a list of annotations like in the example above: gs_annopt(spectable, specname)
.
p + gs_annopt(myspecs, target)
Do I have to dig into ggplot_build
to extract the x
and y
strings or is there a more elegant solution?