eval(expr,envir,enclos)中的错误 - 矛盾?

Ale*_*x W 9 r ggplot2

编辑以提供更完整的代码和特定问题示例

我正在编写一个函数来生成股票价格的时间序列图.但是,我收到以下错误

eval中的错误(expr,envir,enclos):找不到对象'df1234'

这是函数的一个例子:

plot.prices <- function(df1234) {
  require(ggplot2)
  g <- ggplot(df1234, aes(x= as.Date(Date, format= "%Y-%m-%d"), y= df1234[, 3], 
              colour= brewer.pal(12,"Set3")[1])) + geom_point(size=1)
  g + geom_point(aes(x= date, y = df1234[, 4], 
                 colour= brewer.pal(12,"Set3")[2]), size=1)

  # ... code not shown...
  g
}
Run Code Online (Sandbox Code Playgroud)

示例数据:

spy <- read.csv(file= 'http://ichart.finance.yahoo.com/table.csv?s=SPY&d=11&e=1&f=2012&g=d&a=0&b=29&c=1993&ignore=.csv', header= T)

plot.prices(spy) # produces error
g <- ggplot(spy, aes(x= as.Date(Date, format= "%Y-%m-%d"), y= spy[, 3], 
              colour= brewer.pal(12,"Set3")[1])) + geom_point(size=1)
  g + geom_point(aes(x= as.Date(Date), y = spy[, 4], 
                 colour= brewer.pal(12,"Set3")[2]), size=1)
## does not produce error
Run Code Online (Sandbox Code Playgroud)

如您所见,代码完全相同.如果对ggplot()的调用是INSIDE函数,但是如果对ggplot()的调用是OUTSIDE函数,则会出现错误.

任何人都知道为什么看似矛盾?

ags*_*udy 10

发生错误是因为您在gglpot2中使用df [,7],使用列名Adj.Close将解决问题.

 g <- ggplot(df, aes(x= as.Date(Date, format= "%Y-%m-%d"),
                  y= Adj.Close)) + geom_point(size=1)
Run Code Online (Sandbox Code Playgroud)

实际上是错误,这是一个范围错误.aes无法找到df环境.它试图在全球范围内寻找它.

如果要使用索引调用,可以使用aes_string作为示例,并操作字符串而不是表达式

plot.prices <- function(df) {
  require(ggplot2)

  df$Date <- as.Date(df$Date, format= "%Y-%m-%d")

  g <- ggplot(df, aes_string(x= 'Date',
                      y= colnames(df)[7])) + geom_point(size=1)

  # ... code not shown...
  g
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @Alex尝试用ggplot2做复杂的图之前尝试理解基础:美学映射和词法范围. (2认同)

Mar*_*ler 1

我不确定这是否是您想要的,但它可能会有所帮助。我修改了agstudy的代码:

spy <- read.csv(file= 'http://ichart.finance.yahoo.com/table.csv?s=SPY&d=11&e=1&f=2012&g=d&a=0&b=29&c=1993&ignore=.csv', header= T)

library(ggplot2)
library(RColorBrewer)

 plot.prices <- function(df) {

   df$Date <- as.Date(df$Date, format= "%Y-%m-%d")

   g <- ggplot(df, aes_string(x='Date', y= colnames(df)[3])) + 
                   geom_point(colour= brewer.pal(12,"Set3")[1], size=1)

   gg <- g + geom_point(aes_string(x='Date', y= colnames(df)[4]),
                   colour= brewer.pal(12,"Set3")[2], size=1)
   gg
 }

 plot.prices(spy)
Run Code Online (Sandbox Code Playgroud)

这是不使用的代码brewer.pal

library(ggplot2)

spy <- read.csv(file= 'http://ichart.finance.yahoo.com/table.csv?s=SPY&d=11&e=1&f=2012&g=d&a=0&b=29&c=1993&ignore=.csv', header= T)

 plot.prices <- function(df) {

   df$Date <- as.Date(df$Date, format= "%Y-%m-%d")

   g <- ggplot(df, aes_string(x='Date', y= colnames(df)[3])) + 
                   geom_point(colour= 'green', fill='green', size=1)

   gg <- g + geom_point(aes_string(x='Date', y= colnames(df)[4]),
                   colour= 'black', fill='black', size=1)
   gg
 }

 plot.prices(spy)
Run Code Online (Sandbox Code Playgroud)