Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

r - Why does nls function not work in ggplot2

I thought the nls method had been working in the previous versions of 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)

Now I got the error

Error: Unknown parameters: start

And my session

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 

Why does it not work?????

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In ggplot2 version 2.0.0 and up you need to use method.args to pass arguments to geom_smooth(), e.g.:

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)

From the ggplot2 NEWS file (emphasis added):

Layers are now much stricter about their arguments - you will get an error if you've supplied an argument that isn't an aesthetic or a parameter. This is likely to cause some short-term pain but in the long-term it will make it much easier to spot spelling mistakes and other errors (#1293).

This change does break a handful of geoms/stats that used ... to pass additional arguments on to the underlying computation. Now geom_smooth()/stat_smooth() and geom_quantile()/stat_quantile() use method.args instead (#1245, #1289); and stat_summary() (#1242), stat_summary_hex(), and stat_summary2d() use fun.args.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...