我很难将变量的标签(由expss包提供)组合到一个ggplot2图中,这个函数我已经写了好几遍了。换句话说,下面的代码按预期工作。
data(mtcars)
library(expss)
library(ggplot2)
mtcars <- apply_labels(mtcars,
mpg = "MPG",
cyl = "CYL",
wt = "WEIGHT")
use_labels(mtcars, {
# from the example of the package's vignette
ggplot(..data) +
geom_point(aes(y = mpg, x = wt))
}) 如果我想写一个像
myplot <- function(x,y) {
ggplot(data=mtcars) +
geom_point(aes(y = {{y}}, x = {{x}}))
}
myplot(mpg, cyl)
myplot(mpg, wt)这也是适当的。
但如果我用
myplot <- function(x,y) {
use_labels(data=mtcars, {
ggplot(..data) +
geom_point(aes(y = y, x = x))
})
}
myplot("mpg", "cyl")这不再起作用了,即情节不正确,标签也不显示。
我试过了
myplot <- function(x,y) {
use_labels(data=mtcars, {
ggplot(data=mtcars) +
geom_point(aes(y = mtcars[[y]], x = mtcars[[x]]))
})
}
myplot("mpg", "cyl")那么情节是正确的,但标签没有显示.
发布于 2021-12-21 18:17:31
更简单的解决方案:ggeasy包(https://rdrr.io/cran/ggeasy/man/easy_labs.html)
以下内容非常有效:
myplot <- function(x,y) {
ggplot(data=mtcars) +
geom_point(aes(y = {{y}}, x = {{x}}))+
ggeasy::easy_labs(teach=TRUE)
}
myplot(mpg, cyl)https://stackoverflow.com/questions/70428492
复制相似问题