我有一个函数,使用表达式!! enquo()可以无错误地返回ggplot图:
library(dplyr)
library(tidyr)
library(ggplot2)
f <- function (df, x, y) {
df %>%
ggplot(aes(!!enquo(x), !!enquo(y)))+
geom_point()
}
f (mtcars, cyl, disp)
我想通过添加下一个几何图形来选择图形上的最后一点:
annotate('point', x=dplyr::last(!!enquo(x)),
y=dplyr::last(!!enquo(y)), color='#FF0000', size = 1.5)
我得到一个错误:
Error: Quosures can only be unquoted within a quasiquotation
context.
答案 0 :(得分:0)
尝试:
f <- function (df, x, y) {
x <- enquo(x)
y <- enquo(y)
df %>%
ggplot(aes(!!x, !!y)) + geom_point() +
annotate("point",
x = summarise(df, last(!!x)) %>% unlist(),
y = summarise(df, last(!!y)) %>% unlist(),
color = "#FF0000", size = 1.5)
}
f(mtcars, cyl, disp)