在R中的另一个函数中使用ggplot() [英] Use of ggplot() within another function in R
问题描述
我试图用ggplot2库编写一个简单的绘图函数。考虑一个 data.frame
称为表示存储两个条件和两个我想要绘制的平均值(条件将出现在X轴上,表示在Y上)。 $ b $>
b(pre> library(ggplot2)
m <-c(13.8,14.8)
cond <-c(1,2)
表示< ; - data.frame(means = m,condition = cond)
表示
#输出应该是:
#表示条件
#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)
#这将输出以下错误:
#eval中的错误(expr,envir,enclos):object'meansdf'not found
所以看起来ggplot正在调用 eval
,它找不到argum ent meansdf
。有没有人知道我可以如何成功地将函数参数传递给ggplot?
(注意:我可以直接调用ggplot函数,但最终我希望因为Joris和Chase已经正确地回答了,标准的最佳实践就是让我们的绘图函数做更复杂的东西!:) 只需简单地省略 meansdf $
部分并直接引用数据框列即可。
testplot < - function(meansdf)
{
p < - ggplot(meansdf,
aes(fill = condition,
y = means,
x = condition) )
p + geom_bar(position =dodge,stat =identity)
}
$ b $这是可行的,因为在 aes
中引用的变量既可以在全局环境中查找,也可以在传递给 ggplot
。这也是为什么你的示例代码(使用 meansdf $ condition
等)不起作用的原因: meansdf
既不是在全球环境中可用,在传递给 ggplot
,也就是 meansdf
本身的数据框内也不可用。 / p>
事实上,在全局环境而不是在调用环境中查找变量实际上是 ggplot2中的一个已知错误,目前Hadley认为不可修复。
如果希望使用局部变量(例如 scale
)来影响用于该图的数据,则会导致问题:
testplot< - function(meansdf)
{
scale < - 0.5
p < - ggplot(meansdf ,
aes(fill = condition,
y = means * scale,#不起作用,因为找不到比例
x = condition))
p + geom_bar(position =dodge ,stat =identity)
}
为这种情况提供了一个非常好的解决方法由Winston Chang引用的GitHub问题:在调用 ggplot
时,显式地将环境
参数设置为当前环境。
下面是上面例子的样子:
testplot< - function(meansdf)
scale <-0.5
p < - ggplot(meansdf,
aes(fill = condition,
y = means * scale,
x = condition),
environment = environment())#这是唯一更改/添加的行
p + geom_bar(position =dodge,stat =identity)
}
##现在,下面的工作
testplot(表示)
I'm trying to write a simple plot function, using the ggplot2 library. But the call to ggplot doesn't find the function argument.
Consider a data.frame
called means
that stores two conditions and two mean values that I want to plot (condition will appear on the X axis, means on the 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
So it seems that ggplot is calling eval
, which can't find the argument meansdf
. Does anyone know how I can successfully pass the function argument to ggplot?
(Note: Yes I could just call the ggplot function directly, but in the end I hope to make my plot function do more complicated stuff! :) )
As Joris and Chase have already correctly answered, standard best practice is to simply omit the meansdf$
part and directly refer to the data frame columns.
testplot <- function(meansdf)
{
p <- ggplot(meansdf,
aes(fill = condition,
y = means,
x = condition))
p + geom_bar(position = "dodge", stat = "identity")
}
This works, because the variables referred to in aes
are looked for either in the global environment or in the data frame passed to ggplot
. That is also the reason why your example code - using meansdf$condition
etc. - did not work: meansdf
is neither available in the global environment, nor is it available inside the data frame passed to ggplot
, which is meansdf
itself.
The fact that the variables are looked for in the global environment instead of in the calling environment is actually a known bug in ggplot2 that Hadley does not consider fixable at the moment.
This leads to problems, if one wishes to use a local variable, say, scale
, to influence the data used for the plot:
testplot <- function(meansdf)
{
scale <- 0.5
p <- ggplot(meansdf,
aes(fill = condition,
y = means * scale, # does not work, since scale is not found
x = condition))
p + geom_bar(position = "dodge", stat = "identity")
}
A very nice workaround for this case is provided by Winston Chang in the referenced GitHub issue: Explicitly setting the environment
parameter to the current environment during the call to ggplot
.
Here's what that would look like for the above example:
testplot <- function(meansdf)
{
scale <- 0.5
p <- ggplot(meansdf,
aes(fill = condition,
y = means * scale,
x = condition),
environment = environment()) # This is the only line changed / added
p + geom_bar(position = "dodge", stat = "identity")
}
## Now, the following works
testplot(means)
这篇关于在R中的另一个函数中使用ggplot()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!