我正在努力將變數標簽(由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")
那么情節是正確的,但沒有顯示標簽......
uj5u.com熱心網友回復:
更簡單的解決方案: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://www.uj5u.com/net/395364.html
