類型aes
無任何括號或參數,看看它在做什麼:
function (x, y, ...)
{
aes <- structure(as.list(match.call()[-1]), class = "uneval")
rename_aes(aes)
}
它需要它的參數的名稱不加評估了。它基本上保存了以後的名稱,以便它可以在你試圖繪製的數據框的上下文中評估它們(這就是爲什麼你的錯誤信息是抱怨eval
)。因此,當您在您的ggplot
建設中包含my.aes(x=a, y=b)
時,它正在尋找x
在data
- 因爲x
沒有在aes(x=x, y=y)
評估。
考慮在aes
發生了什麼事情的另一種方法是一樣的東西
my.aes <- function(x, y) {
ans <- list(x = substitute(x), y = substitute(y))
class(ans) <- "uneval"
ans
}
應在例如工作之上,但看到plyr::.
的說明(使用相同match.call()[-1]
範式爲aes
):
Similar tricks can be performed with substitute, but when functions can be called in multiple ways it becomes increasingly tricky to ensure that the values are extracted from the correct frame. Substitute tricks also make it difficult to program against the functions that use them, while the quoted class provides as.quoted.character to convert strings to the appropriate data structure.
如果你想my.aes
調用aes
本身,或許是這樣的:
與
aes_string
功能
my.aes <- function(x,y) {
do.call(aes, as.list(match.call()[-1]))
}
例子指出了羅馬Luštrik:
my.aes <- function(x,y) {
aes_string(x = x, y = y)
}
,但你需要你的電話改變my.aes("a", "b")
在這種情況下。
不知道你在這裏之後,但是http://docs.ggplot2.org/current/aes_string.html? –
@RomanLuštrik:謝謝你,沒有意識到這一點。小心將其轉換爲答案? – krlmlr