Why the nls function does not work in ggplot2

I thought the nls method worked in 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 an 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 this not work ?????

+2
source share
1 answer

In ggplot2 version 2.0.0 and later, you need to use method.args to pass the geom_smooth() arguments, for example:

 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 ggplot2 NEWS file (highlighted by me):

Layers are now much stricter with respect to their arguments - you will receive an error message if you provide an argument that is not aesthetic or parameter. This is likely to cause some short-term pain, but in the long run it will greatly facilitate the identification of spelling errors and other errors (# 1293).

This change breaks up several geometers / statistics that they used ... to pass additional arguments to the underlying calculation. Now geom_smooth () / stat_smooth () and geom_quantile () / stat_quantile () instead use the .args method (# 1245, # 1289); and stat_summary () (# 1242), stat_summary_hex () and stat_summary2d () use fun.args.

+3
source

All Articles