我正在尝试使用ggplot2库编写一个简单的绘图函数.但是对ggplot的调用没有找到函数参数.
考虑一个存储两个条件和两个我想要绘制的平均值的data.frame调用means(条件将出现在X轴上,表示在Y上).
library(ggplot2)
m <- c(13.8, 14.8)
cond <- c(1, 2)
means <- data.frame(means=m, condition=cond)
means
# The output should be:
# means condition
# 1 13.8 1
# 2 14.8 2
testplot <- function(meansdf)
{
p <- ggplot(meansdf, aes(fill=meansdf$condition, y=meansdf$means, x = meansdf$condition))
p + geom_bar(position="dodge", stat="identity")
}
testplot(means)
# This will output the following error:
# Error in eval(expr, envir, enclos) : object 'meansdf' not found
Run Code Online (Sandbox Code Playgroud)
所以似乎ggplot正在调用eval,它无法找到参数meansdf.有谁知道如何成功将函数参数传递给ggplot?
(注意:是的我可以直接调用ggplot函数,但最后我希望让我的绘图函数做更复杂的东西!:))