ggplot中出现“找不到对象”错误

时间:2015-01-19 22:24:12

标签: r ggplot2

我的R代码如下所示:

Region_TS <- function(index, scale = 1e+09, region){
  # Creates line graph of a variable of a Region over time
  # Input:
  #  index = index number of series
  #  scale = scaling number, 1, 1e+03, 1e+06, 1e+09 or 1e+12
  #  Region = string of region
  # Output: line graph "plot"
  if (scale == 1){
    labelstring <- ""
  }else if (scale == 1e+03){
    labelstring <- "in thousands"
  }else if (scale == 1e+06){
    labelstring <- "in millions"
  }else if (scale == 1e+09){
    labelstring <- "in billions"
  }else if (scale == 1e+12){
    labelstring <- "in trillions"
  }else{
    stop("Error! Enter correct scaling number!")
  }
  temp <- subset(Panel, Region == region)
  temp <- temp[,c(2,index)]
  temp[,2] <- temp[,2]/scale
  plot <- ggplot(data = temp, aes(x = temp[[1]], y = temp[[2]], group = 1))
  plot <- plot + geom_line()
  plot <- plot + ggtitle(paste(gsub("_", " ", names(temp)[2]), "of", region, sep = " "))
  plot <- plot + xlab("Year")
  plot <- plot + ylab(labelstring)
  plot
}

Region_TS(index = 5, scale = 1e+12, region = "Texas") 

当我运行最后一行时,它给出了一个错误说

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

然而,data.frame“temp”是正确定义的,因为我得到类似

的内容
    Year      GSP
474 2000 0.746433
475 2001 0.784956
476 2002 0.799937
477 2003 0.843715
478 2004 0.920387
479 2005 0.998092
480 2006 1.093794
481 2007 1.176966
482 2008 1.243331
483 2009 1.167233
484 2010 1.248511

当我在“plot&lt; - ggplot ...”之前插入“return(temp)”时。

我意识到这个问题与其他一些问题类似,但它们似乎并没有为我提供上述问题的解决方案。

感谢您的帮助!

0 个答案:

没有答案