R 总结时出错:未找到对象,用户定义函数

R 总结时出错:未找到对象,用户定义函数,r,function,R,Function,我试图编写一个函数,生成一些汇总统计数据,并为非常具体的数据绘制一些曲线图。但是,当我尝试运行该函数时,无法生成绘图,我得到以下错误: wrapup期间出错:未找到对象“ff”。从各种测试中,我可以看出错误来自函数的绘图部分。我还要求函数独立于绘图来打印这个对象,它这样做没有问题,所以我不确定问题出在哪里 这可能是我所缺少的一些非常明显的东西,但我看不见,无法找出答案。我在下面发布我的代码。任何帮助都将不胜感激! 下面是我通过函数传递的一些示例数据,相关列是FISH、FARM和MIX。该功能还包

我试图编写一个函数,生成一些汇总统计数据,并为非常具体的数据绘制一些曲线图。但是,当我尝试运行该函数时,无法生成绘图,我得到以下错误:

wrapup期间出错:未找到对象“ff”。从各种测试中,我可以看出错误来自函数的绘图部分。我还要求函数独立于绘图来打印这个对象,它这样做没有问题,所以我不确定问题出在哪里

这可能是我所缺少的一些非常明显的东西,但我看不见,无法找出答案。我在下面发布我的代码。任何帮助都将不胜感激! 下面是我通过函数传递的一些示例数据,相关列是FISH、FARM和MIX。该功能还包括以下内容:

humans_l[[2004]]
        PB.HAP.Grp             Chem Lake.Dist..m. Wsp..ms.1. Precip..mm.
1 Mercury (methyl) Mercury (methyl)            40        2.8        1500
2 Mercury (methyl) Mercury (methyl)            40        2.8        1500
3 Mercury (methyl) Mercury (methyl)            40        2.8        1500
4 Mercury (methyl) Mercury (methyl)            40        2.8        1500
  CHRIS.Lake_WSP_PRECIP  MIX Ratio..ThreshT2.ThreshT1. Ratio..EEFT1.EEFT2. Farm.Fraction
1           40_2.8_1500  710                  10.53022                   1   0.001876256
2           40_2.8_1500  865                  11.12440                   1   0.001644249
3           40_2.8_1500 1079                  11.98686                   1   0.001434138
4           40_2.8_1500 1537                  13.91867                   1   0.001182803
  Fish.Fraction        FARM       FISH Soil.Fraction Exposed.Vegetable.Fraction
1     0.9981237 0.000178178 0.09478657   0.000307563                0.000190690
2     0.9983558 0.000147806 0.08974468   0.000269257                0.000167158
3     0.9985659 0.000119642 0.08330503   0.000234640                0.000145833
4     0.9988172 0.000085000 0.07176095   0.000193304                0.000120313
  Protected.Vegetable.Fraction Exposed.Fruit.Fraction Protected.Fruit.Fraction
1                  0.000180819            0.000258604              0.000826058
2                  0.000158505            0.000226691              0.000724117
3                  0.000138284            0.000197772              0.000631742
4                  0.000114085            0.000163162              0.000521189
  Root.Vegetable.Fraction Beef.Fraction Total.Dairy.Fraction Pork.Fraction
1                9.10e-06      6.38e-05             2.58e-05      8.85e-08
2                7.98e-06      5.58e-05             2.26e-05      7.75e-08
3                6.96e-06      4.87e-05             1.97e-05      6.75e-08
4                5.74e-06      4.01e-05             1.62e-05      5.56e-08
  Poultry.Fraction Eggs.Fraction
1         5.65e-06      8.05e-06
2         4.94e-06      7.05e-06
3         4.31e-06      6.14e-06
4         3.55e-06      5.06e-0

names(humans_l[2004])
"Mercury (methyl)40_2.8_1500"

    regr<-function(dataframe, name){
  require(ggplot2)
  require(ggthemes)
  require(reshape2)

  pr_farm<-lm(log(dataframe$FARM)~log(dataframe$MIX))
  farm_intercept<-as.numeric(summary(pr_farm)$coefficients[1,1])
  farm_slope<-as.numeric(summary(pr_farm)$coefficients[2,1])
  b<-confint(pr_farm, 'log(dataframe$MIX)', level=0.95)
  CI_farm<-summary(pr_farm)$coefficients[2,1]-b[1]
  r.square_farm<-summary(pr_farm)$r.squared

  if(dataframe$FISH[1] != 0) {
    pr_fish<-lm(log(dataframe$FISH)~log(dataframe$MIX))
    fish_intercept<-as.numeric(summary(pr_fish)$coefficients[1,1])
    fish_slope<-as.numeric(summary(pr_fish)$coefficients[2,1])
    a<-confint(pr_fish, 'log(dataframe$MIX)', level=0.95)
    CI_fish<-summary(pr_fish)$coefficients[2,1]-a[1]
    r.square_fish<-summary(pr_fish)$r.squared
  } else {
    fish_intercept<-NA
    fish_slope<-NA
    CI_fish<-NA
    r.square_fish<-NA
  }



  vector<-c(farm_intercept, farm_slope, CI_farm, r.square_farm, fish_intercept, fish_slope, CI_fish, r.square_fish)
  names(vector)<-c("farm_intercept", "farm_slope", "CI_farm", "r.squared_farm", "fish_intercept", "fish_slope", "CI_fish", "r.squared_fish")

  print(formatC(vector, format="f", digits=5))

  Farm<-data.frame(x=dataframe$MIX, y=dataframe$FARM)
  Fish<-data.frame(x=dataframe$MIX, y=dataframe$FISH)
  ff <- melt(list(Farm=Farm, Fish=Fish), id.vars = "x")
  plot1<-ggplot(ff, aes(x=x, y=value, color=L1, fill=L1))+geom_point(aes(x=ff$x, y=Farm$value, color=ff$L1), shape=19, size=7)+geom_point(size=6, shape=21, lwd=4)+scale_fill_manual("Risk Type", values=c("Farm"="#677719", "Fish"="#0067AB"))+scale_colour_manual("Risk Type", values=c("Farm"="darkgreen", "Fish"="darkblue"))
  plot2<-plot1+theme_classic()
  plot3<-plot2+scale_x_continuous(breaks=c(710, 865, 1079, 1537))+xlab("Mixing Height")+ylab("Tier 2 Risk")
  plot4<-plot3+geom_line(aes(y=exp(farm_intercept)*x^farm_slope), color="darkgreen", lwd=2, lty=1)
  plot6<-plot4+theme(axis.title=element_text())+theme(axis.title.x = element_text(face='bold', size=20),axis.title.y = element_text(face='bold', size=20, angle=90) )
  plot7<-plot6+theme(axis.text.x = element_text(face='bold', size=18),axis.text.y = element_text(face='bold', size=18) )
  plot8<-plot7+theme(axis.title.y=element_text(vjust=0.25))
  plot9<-plot8+ggtitle(name)
  ggsave(paste(name, '.pdf', sep=""), plot1)
  postscript(file = paste(name, '.eps', sep=""))
  dev.off()

  print(plot9)
}
由于错误,打印的图像为空白。我看不出我做错了什么!任何帮助都将不胜感激

非常感谢

regr(humans_l[[2004]], name=names(humans_l[2004]))
    farm_intercept     farm_slope        CI_farm r.squared_farm fish_intercept     fish_slope 
        "-2.33663"     "-0.95878"      "0.01370"      "0.99998"      "0.03835"     "-0.36312" 
           CI_fish r.squared_fish 
         "0.10279"      "0.99142" 
    Saving 11.2 x 8.28 in image
    Error during wrapup: object 'Farm' not found