![](/img/trans.png)
[英]How to add a regression line with linear and quadratic components in ggplot?
[英]Add quadratic regression line to existing ggplot R
I have fit a linear model and a polynomial model onto to same dataframe and would like to plot both lines on the same scattergraph.
我有以下代码来显示我的线性模型的线,如何将多项式 model (pm1) 添加到此?
如果需要,我愿意放弃 ggplot
ggplot(A1, aes(x = PopMns, y = Freq)) +
geom_point() +
stat_smooth(method = "lm", col = "red") +
geom_line()
这是我正在使用的 dataframe。 我正在尝试 plot 线性和非线性 model 的线到基于此的散点图。
year Freq PopTotal PopMns
1 1970 611 3700437 3700.437
2 1971 436 3775760 3775.760
3 1972 515 3851651 3851.651
4 1973 436 3927781 3927.781
5 1974 531 4003794 4003.794
6 1975 685 4079480 4079.480
试试这个:
#Code
ggplot(A1, aes(x = PopMns, y = Freq)) +
geom_point() +
stat_smooth(method = "lm", col = "red",se=F) +
stat_smooth(method = "lm", col = "blue",formula = y~poly(x,2),se=F) +
geom_line()
有没有办法让散点代替黑线?
更新:试试这个,使用你的数据:
library(ggplot2)
#Code
ggplot(A1, aes(x = PopMns, y = Freq)) +
geom_point() +
stat_smooth(method = "lm", col = "red",se=F) +
stat_smooth(method = "lm", col = "blue",se=F,formula = y~poly(x,2))
Output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.