尽管在外面工作,ggplot在函数内部仍然无法正常工作 - R.

M. *_*azy 4 debugging r function ggplot2

我正在尝试创建一个函数,它接收2个参数并为它们输出适当的ggplot.代码可以完美地手动完成,但不知怎的,我无法在函数包装器中使它工作.

返回的错误是

Error in eval(expr, envir, enclos) : object 'TimeVector' not found
Run Code Online (Sandbox Code Playgroud)

我试图通过强制将未找到的对象(作为字符串添加到ggplot中)来纠正它.这反过来又形成了不同的麻烦

Error: Discrete value supplied to continuous scale
Run Code Online (Sandbox Code Playgroud)

删除scale_x_continuous(breaks = 0:24)修复了第二个错误,但输出了一个空图,表明ggplot根本没有任何数据.

数据是按时间分组的交通密度观测的大数据框架.它看起来像这样:

ID                   Road Status                Time Statusint Day  Months Year Weekday

1 Me7war To Sheikh Zayid Orange 2012-10-01 00:03:00         3   1 October   12  Monday
1 Me7war To Sheikh Zayid  Green 2012-10-01 05:00:00         2   1 October   12  Monday
1 Me7war To Sheikh Zayid Yellow 2012-10-01 05:24:00         5   1 October   12  Monday
Run Code Online (Sandbox Code Playgroud)

我试图绘制"Statusint"变量,简称状态整数,范围从1(良好流量)到5(可怕流量)对"时间"变量."时间"被格式化为Posix,因此我创建了一个名为"TimeVector"的数字向量,其唯一目的是在ggplot中进行绘图.

功能如下:

Plotroad <- function( roadID , Day ) {

  *** Working Code ***

  else { 

  ### THE PROBLEM CODE: Everything below works manually, but returns an error in the function

    Roadsubset <- October[October$ID == as.numeric(roadID), ] 
    Timesubset <- subset(Roadsubset, format(Roadsubset$Time,'%d') == "Day" )
    TimeVector <- as.numeric(gsub(":" , "." , strftime(Timesubset$Time, format="%H:%M")))

    ggplot(Timesubset, aes( x = "TimeVector", y = "Timesubset$Statusint")) + geom_point() +  
        stat_smooth() + scale_x_continuous(breaks=0:24) 


    ### The working code: 
    Roadsubset <- October[October$ID == as.numeric(roadID), ] 
    Timesubset <- subset(Roadsubset, subset = Roadsubset$Day == as.integer(Date) )
    TimeVector <- as.numeric(gsub(":" , "." , strftime(Timesubset$Time, format="%H:%M")))
    Timesubset$timevector <- TimeVector

    print(ggplot( data = Timesubset, aes_string( x = "timevector" , y = "Statusint" )) + geom_point() + stat_smooth()  + scale_x_continuous(breaks=0:24) + labs(list(title = as.character(Timesubset$Road[1]) , x = "Time of Day", y = "Status")))  

  }
}
Run Code Online (Sandbox Code Playgroud)

我已经看到了一些建议使用打印提示,如ggplot在命令行中不被调用.然而,这并不能解决上述错误.

这是我的第一篇关于堆栈溢出的帖子,所以请指出如果需要的话,我可以更好地格式化未来的问题.谢谢.

Bro*_*ieG 6

除了使用变量名称外,还存在范围问题.GGPlot在全局环境中执行非标准评估,因此您的函数中定义的任何内容都不能直接访问,除了"数据"参数,因为它是明确传递而不是通过非标准评估.因此,解决此问题的方法是将您的变量添加到data参数中.我创建了一个我认为模仿你的问题的例子,但由于我没有你的数据,所以它不相同:

gg_fun <- function() {
  data <- data.frame(a=1:10, b=1:10)
  clr <- rep(c("a", "b"), 5)
  ggplot(data, aes(x=a, y=b, color=clr)) + geom_point()
}
gg_fun()
# Error in eval(expr, envir, enclos) : object 'clr' not found
gg_fun <- function() {
  data <- data.frame(a=1:10, b=1:10)
  clr <- rep(c("a", "b"), 5)
  data$clr <- clr
  ggplot(data, aes(x=a, y=b, color=clr)) + geom_point()
}
gg_fun() # works
Run Code Online (Sandbox Code Playgroud)

在您的情况下,您需要添加TimeVectorTimesubset(普通),然后使用不带引号的aes语法:

ggplot(Timesubset, aes(x=TimeVector, y=Statusint)) ...
Run Code Online (Sandbox Code Playgroud)