为什么nls函数在ggplot2中不起作用 [英] Why does nls function not work in 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,
pre>
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 ='公式= y_exp(a * x + b),
start = c(a = 0.001,b = 3),se = FALSE)
现在我得到了错误:
错误:未知参数:开始
和我的会话
R版本3.2.4修订版(2016-03-16 r70336)
平台:x86_64-w64-mingw32 / x64(64位)
在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
附加软件包:
[1]统计图形grDevices utils数据集方法库
其他附加软件包:
[1] ggplot2_2。 1.0 broom_0.4.0 asreml_3.0 lattice_0.20-33 dplyr_0.4.3
通过命名空间加载(并未附加):
[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
为什么它不起作用?????
解决方案在ggplot2
2.0.0版本中,您需要使用method.args
将参数传递给geom_smooth()
,例如:
method.args = list(start = c(a = 0.001,b = 3)),se = FALSE)
从 ggplot2 NEWS文件(强调增加):
图层现在更加严格关于他们的论点 - 如果你提供的参数不是美学或参数,你会得到一个错误。这可能会导致一些短期的痛苦,但从长远来看,它会使发现拼写错误和其他错误变得更容易(#1293)。 更改确实会破坏一些geoms / stats,这些geoms / stats使用...将附加参数传递给底层计算。 现在geom_smooth()/ stat_smooth()和geom_quantile()/ stat_quantile()使用method.args来代替(#1245,#1289);和stat_summary()(#1242),stat_summary_hex()和stat_summary2d()使用fun.args。
I thought the
nls
method had been working in the previous versions ofggplot2
: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?????
解决方案In
ggplot2
version 2.0.0 and up you need to usemethod.args
to pass arguments togeom_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.
这篇关于为什么nls函数在ggplot2中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文