函数内的ggplot调用:eval(expr,envir,enclos)中的错误:找不到对象'...' [英] ggplot call inside a function: Error in eval(expr, envir, enclos) : object '...' not found

查看:2812
本文介绍了函数内的ggplot调用:eval(expr,envir,enclos)中的错误:找不到对象'...'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  compare<  -  function(list.a,list.b,selection)
$ b dev.new()
df < - melt(as.data.frame(t(matrix(c(list.a [selection],list.b [selection]),nrow = 2,byrow = TRUE))))
xaxis < - rep(c(1:length(selection)),2)
ggplot(df,aes(x = xaxis,y = value, group = variable))+ geom_line()
}



我刚才意识到这段代码需要两次运行:

  require(ggplot2) 
require(reshape)

它可以归纳为单行问题,如下所示:

  compare < -  function(list.a,list.b,selection)ggplot()+ geom_line(data = melt( as.data.frame(t(matrix(c(list.a [selection],list.b [selection]),nrow = 2,byrow = TRUE)))),aes_string(x = rep(1:length(selection ),2),y =value,color =variable))

这个函数是一个linechar t两个值列表(a和b),但只绘制这两个列表中的某个范围(选择);请参阅下面的示例函数调用。

顺便说一句,函数的单行版本可以工作,但输出是错误的:它会生成一行代码的期望线图。但是,如果我取消变量变量(由 melt() code> call - 实际上是 melt.data.frame(),然后我得到如下的错误(object not found),但现在它找不到找不到对象'值'



以下是该函数的默认,纯净版本;但是同样的问题:

  compare<  -  function(list.a,list.b,selection)
{
df < - 熔化(as.data.frame(t(matrix(c(list.a [selection],list.b [selection]),nrow = 2,byrow = TRUE)))
xaxis < - rep(c(1:length(selection)),2)
ggplot(df,aes(x = xaxis,y = value,color = variable))+ geom_line()
}

使用以下方式调用这些函数的任何版本:

 比较(runif(100),runif(100),30:80)

应该在y轴上的[0,1]范围内生成两行随机值的线图,在这两个列表中,从索引30到80取得51个值。



[/编辑]



但是我收到以下错误:

< pre $ eval中的错误(expr,envir,enclos):找不到对象'xaxis'

我不知道为什么我得到这个错误或者如何防止它。任何人都可以为我解决这个问题,请告诉我我做错了什么?

解决方案

我有同样的问题,发现答案在这里:在R中的另一个函数中使用ggplot() a>



如上所述,像下面这样添加 environment 参数对我有用:

  library(reshape2)
library(ggplot2)

比较< - function(list.a,list.b ,选择)
{
df < - melt(as.data.frame(t(matrix(c(list.a [selection],list.b [selection]),nrow = 2,byrow = b)(b)(b)(b)(b)(b)(b) ))+ geom_line()#failed
ggplot(df,aes(x = xaxis,y = value,color = variable),environment = environment())+ geom_line()#works
}

比较(runif(100,0,1),runif(100,0,1),c(30:80))


After defining this function:

compare <- function(list.a, list.b, selection)
{
  dev.new()
  df <- melt(as.data.frame(t(matrix(c( list.a[selection], list.b[selection]), nrow=2, byrow=TRUE))))
  xaxis <- rep(c(1:length(selection)), 2)
  ggplot(df, aes(x=xaxis, y=value, group=variable)) + geom_line()
}

[EDIT]

I just realized this code needs two requires to run:

require(ggplot2)
require(reshape)

And it can be summarized into a single line problem like this:

compare <- function(list.a, list.b, selection) ggplot() + geom_line(data=melt(as.data.frame(t(matrix(c( list.a[selection], list.b[selection]), nrow=2, byrow=TRUE)))), aes_string(x=rep(1:length(selection), 2), y="value", colour="variable"))

The expected output of this function is a linechart of two lists of values (a and b), but only plotting a certain range (selection) from those two lists; see an example function call below.

The one-line version of the function, by the way, does work, but the output is wrong: it produces a single line instead of the desired linechart. But if I unquote the value and variable variables (generated by the melt() call - that is melt.data.frame(), actually), then I get the same error as below ("object not found"), but now it does not find value: object 'value' not found.

Here is the "default", clean version of the function; same problem, though:

compare <- function(list.a, list.b, selection)
{
  df <- melt(as.data.frame(t(matrix(c( list.a[selection], list.b[selection]), nrow=2, byrow=TRUE))))
  xaxis <- rep(c(1:length(selection)), 2)
  ggplot(df, aes(x=xaxis, y=value, colour=variable)) + geom_line()
}

Calling any version of these functions with:

compare(runif(100), runif(100), 30:80)

Should have produced a linechart of two lines with random values in the [0,1] range on the y-axis, over 51 values taken from index 30 to 80 in both lists.

[/EDIT]

But I get the following error:

Error in eval(expr, envir, enclos) : object 'xaxis' not found

I have no clue why I am getting this error or how to prevent it. Anybody can solve this for me, please, and tell me what I am doing wrong?

解决方案

I had the same problem and found the answer here: Use of ggplot() within another function in R

As suggested there, adding environment parameter like below worked for me:

library(reshape2)
library(ggplot2)

compare <- function(list.a, list.b, selection)
{
  df <- melt(as.data.frame(t(matrix(c( list.a[selection], list.b[selection]), nrow=2, byrow=TRUE))))
  xaxis <- rep(c(1:length(selection)), 2)
  # ggplot(df, aes(x=xaxis, y=value, colour=variable)) + geom_line() # fails
  ggplot(df, aes(x=xaxis, y=value, colour=variable),environment=environment()) + geom_line() # works
}

compare(runif(100, 0, 1), runif(100, 0, 1), c(30:80))

这篇关于函数内的ggplot调用:eval(expr,envir,enclos)中的错误:找不到对象'...'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆