Error in eval(expr, envir, enclos) - contradiction?

12,443

Solution 1

I am not sure whether this is what you want, but it might help. I modified agstudy's code:

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)

Here is code without using 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)

Solution 2

The error occur because you use df[, 7] in gglpot2, use column name Adj.Close will fix the problem.

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

In fact the error , it is a scoping error. aes can't find the df environnement. It tries to look for it the global scope .

if you you want to use use indexing calls , you can use aes_string for example , and manipulate strings not expressions

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
}

enter image description here

Share:
12,443
alexwhitworth
Author by

alexwhitworth

By day: Data Scientist working primarily in R and flavors of SQL EDU: MS in Statistics from UCLA. My thesis can be found here

Updated on July 25, 2022

Comments

  • alexwhitworth
    alexwhitworth almost 2 years

    Edited to give a fuller example of code and specific issue

    I'm writing a function to produce time series plots of stock prices. However, I'm getting the following error

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

    Here's an example of the function:

    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
    }
    

    And example data:

    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
    

    As you can see, the code is identical. I get an error if the call to ggplot() is INSIDE the function but not if the call to ggplot() is OUTSIDE the function.

    Anyone have any idea why the seeming contradiction?

  • alexwhitworth
    alexwhitworth over 11 years
    This would fix the problem in the example function. However, the full code includes adding layers for multiple columns.... I didn't put the full detail because it would make the example data difficult. <\n> Bottom Line I need to be able to use indexing calls for the aes(y= ...) call.
  • agstudy
    agstudy over 11 years
    @Alex I append my solution for indexing
  • alexwhitworth
    alexwhitworth over 11 years
    I edited my problem to give a fuller example of the error. Perhaps the issue is the addition of colour= brewer.pal(...)
  • Mark Miller
    Mark Miller over 11 years
    Where did you get brewer.pal? Is it in the 'RColorBrewer' package?
  • agstudy
    agstudy over 11 years
    @Alex before trying to do complex plots with ggplot2 try to understand the basics : aesthetic mapping and the lexical scoping.
  • alexwhitworth
    alexwhitworth over 11 years
    Looks like I had a syntax error. I was using "... aes_string(x,y, colour)" not "... aes_string(x,y ), colour)"
  • smci
    smci almost 11 years
    For parameterizing col names in ggplot functions, see also Making plot functions with ggplot and aes_string. If you stored the column as.Date(Date, format= "%Y-%m-%d") then you could use aes_string to reference both x,y series.