为什么nls功能在ggplot2中不起作用

时间:2016-04-20 13:20:40

标签: r ggplot2

我认为nls方法在之前版本的ggplot2中一直有效:

df22 <- data.frame(Date = as.Date(c("1997-04-23", "2003-04-01", "2004-10-01", "2007-04-12", "2009-10-04",
                                  "2011-05-12", "2012-08-23", "2013-11-08", "2014-10-29", "2015-08-12")),
                 Packages = c(12, 250, 500, 1000, 2000, 3000, 4000, 5000, 6000, 7000))

library(ggplot2)
ggplot(data = df22, aes(x = Date, y = Packages)) + 
  geom_point() + 
  geom_smooth(method = 'nls', formula = y ~ exp(a * x + b), 
              start = c(a = 0.001, b = 3), se = FALSE)

现在我收到了错误

Error: Unknown parameters: start

我的会议

R version 3.2.4 Revised (2016-03-16 r70336)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=Swedish_Sweden.1252  LC_CTYPE=Swedish_Sweden.1252    LC_MONETARY=Swedish_Sweden.1252
[4] LC_NUMERIC=C                    LC_TIME=Swedish_Sweden.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_2.1.0   broom_0.4.0     asreml_3.0      lattice_0.20-33 dplyr_0.4.3    

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.4      magrittr_1.5     mnormt_1.5-4     munsell_0.4.3    colorspace_1.2-6 R6_2.1.2        
 [7] stringr_1.0.0    plyr_1.8.3       tools_3.2.4      parallel_3.2.4   grid_3.2.4       nlme_3.1-125    
[13] gtable_0.2.0     psych_1.5.8      DBI_0.3.1        lazyeval_0.1.10  assertthat_0.1   digest_0.6.9    
[19] reshape2_1.4.1   tidyr_0.4.1      labeling_0.3     stringi_1.0-1    scales_0.4.0 

为什么不起作用?????

1 个答案:

答案 0 :(得分:3)

ggplot2版本2.0.0及更高版本中,您需要使用method.args将参数传递给geom_smooth(),例如:

library(ggplot2)
ggplot(data = df22, aes(x = Date, y = Packages)) + 
   geom_point() + 
   geom_smooth(method = 'nls', formula = y ~ exp(a * x + b), 
          method.args=list(start = c(a = 0.001, b = 3)), se = FALSE)

ggplot2 NEWS file(强调添加):

  

图层现在对他们的论点更为严格 - 如果您提供的论证不是美学或参数,您将会收到错误。这可能会导致一些短期的痛苦,但从长远来看,它会更容易发现拼写错误和其他错误(#1293)。

     

此更改确实打破了一些使用...将其他参数传递给基础计算的geoms / stats。 现在geom_smooth()/ stat_smooth()和geom_quantile()/ stat_quantile()使用method.args (#1245,#1289);和stat_summary()(#1242),stat_summary_hex()和stat_summary2d()使用fun.args。