What is the correct way to assign a ggplot2 grob to a variable if the object includes other variabels that I would like to be resolved at the time of assigning.
For example:
xpos and ypos are values that I would like resolved.
I am assigning geom_text to ptext, which I then want to add to some plots, say p1 and p2.
xpos <- .2
ypos <- .7
ptext <- geom_text(aes(x = xpos, y = ypos, label = "Some Text"))
I can add ptext to another plot and everything works as expected
## adding ptext to a plot
p1 <- qplot(rnorm(5), rnorm(5))
p1 + ptext
However, removing (or altering) xpos & ypos gives undesired results.
rm(xpos, ypos)
p2 <- qplot(rnorm(5), rnorm(5))
p2 + ptext
# Error in eval(expr, envir, enclos) : object 'xpos' not found
What is the correct way to go about this?
You should put
xposandyposin a data frame. In your example: