【发布时间】:2011-07-03 16:03:16
【问题描述】:
我正在尝试使用 ggplot2 库编写一个简单的绘图函数。但是对 ggplot 的调用没有找到函数参数。
考虑一个名为means 的data.frame,它存储了我要绘制的两个条件和两个平均值(条件将出现在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
看来ggplot调用的是eval,却找不到参数meansdf。有谁知道我如何成功地将函数参数传递给 ggplot?
(注意:是的,我可以直接调用 ggplot 函数,但最后我希望让我的 plot 函数做更复杂的事情!:) )
【问题讨论】: