If I try to manually compose some elements of a ggplot2
plot, it works just fine:
> p <- ggplot(aes(x = mpg, y = hp), data = mtcars)
> p + geom_vline(xintercept = 20) + geom_point(data = mtcars)
But if I try to bundle some of the composition into a function, I get an error:
> myFunction <- function() {
+ return(
+ geom_vline(xintercept = 20) + geom_point(data = mtcars)
+ )
+ }
> p <- ggplot(aes(x = mpg, y = hp), data = mtcars)
> p + myFunction()
Error in geom_vline(xintercept = 20) + geom_point(data = mtcars) :
non-numeric argument to binary operator
Am I missing something in ggplot2
notation for properly combining ggplot2
elements within a function body?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…