我知道如何使用lm
和abline
函数添加线性趋势线,但是如何添加其他趋势线,如对数,指数和电力趋势线?如何在R中添加不同的趋势线?
17
A
回答
38
这里有一个我准备早:
# set the margins
tmpmar <- par("mar")
tmpmar[3] <- 0.5
par(mar=tmpmar)
# get underlying plot
x <- 1:10
y <- jitter(x^2)
plot(x, y, pch=20)
# basic straight line of fit
fit <- glm(y~x)
co <- coef(fit)
abline(fit, col="blue", lwd=2)
# exponential
f <- function(x,a,b) {a * exp(b * x)}
fit <- nls(y ~ f(x,a,b), start = c(a=1, b=1))
co <- coef(fit)
curve(f(x, a=co[1], b=co[2]), add = TRUE, col="green", lwd=2)
# logarithmic
f <- function(x,a,b) {a * log(x) + b}
fit <- nls(y ~ f(x,a,b), start = c(a=1, b=1))
co <- coef(fit)
curve(f(x, a=co[1], b=co[2]), add = TRUE, col="orange", lwd=2)
# polynomial
f <- function(x,a,b,d) {(a*x^2) + (b*x) + d}
fit <- nls(y ~ f(x,a,b,d), start = c(a=1, b=1, d=1))
co <- coef(fit)
curve(f(x, a=co[1], b=co[2], d=co[3]), add = TRUE, col="pink", lwd=2)
添加描述性的传说:
# legend
legend("topleft",
legend=c("linear","exponential","logarithmic","polynomial"),
col=c("blue","green","orange","pink"),
lwd=2,
)
结果:
通用和更少的长手绘图的方式曲线只是通过x
和th
curve(do.call(f,c(list(x),coef(fit))),add=TRUE)
17
一个ggplot2
方式使用stat_smooth
,使用相同的数据thelatemail
DF <- data.frame(x, y)
ggplot(DF, aes(x = x, y = y)) + geom_point() +
stat_smooth(method = 'lm', aes(colour = 'linear'), se = FALSE) +
stat_smooth(method = 'lm', formula = y ~ poly(x,2), aes(colour = 'polynomial'), se= FALSE) +
stat_smooth(method = 'nls', formula = y ~ a * log(x) +b, aes(colour = 'logarithmic'), se = FALSE, start = list(a=1,b=1)) +
stat_smooth(method = 'nls', formula = y ~ a*exp(b *x), aes(colour = 'Exponential'), se = FALSE, start = list(a=1,b=1)) +
theme_bw() +
scale_colour_brewer(name = 'Trendline', palette = 'Set2')
你也可以适应指数趋势线:系数为curve
功能,如电子表如使用带有日志链接功能的glm
glm(y~x, data = DF, family = gaussian(link = 'log'))
为了一点乐趣,你可以使用theme_excel
从ggthemes
library(ggthemes)
ggplot(DF, aes(x = x, y = y)) + geom_point() +
stat_smooth(method = 'lm', aes(colour = 'linear'), se = FALSE) +
stat_smooth(method = 'lm', formula = y ~ poly(x,2), aes(colour = 'polynomial'), se= FALSE) +
stat_smooth(method = 'nls', formula = y ~ a * log(x) +b, aes(colour = 'logarithmic'), se = FALSE, start = list(a=1,b=1)) +
stat_smooth(method = 'nls', formula = y ~ a*exp(b *x), aes(colour = 'Exponential'), se = FALSE, start = list(a=1,b=1)) +
theme_excel() +
scale_colour_excel(name = 'Trendline', palette = 'Set2')
相关问题
- 1. 如何在R中添加趋势线(指数)?
- 2. 在ggplot2中添加趋势线/盒子图(按组)添加趋势线/箱图
- 3. 在R中设置趋势线长度
- 4. 加权趋势线
- 5. 如何在python matplotlib点(散点图)中添加趋势线?
- 6. 叠加趋势,使用R
- 7. QlikView - 为平均趋势线添加值
- 8. 将exp/power趋势线添加到ggplot
- 9. 添加趋势线,部分地块
- 10. 添加多个趋势线 - SSRS 2008R2
- 11. 添加趋势线大熊猫
- 12. 将趋势线添加到ASP.Net图表
- 13. Google Charts手动添加趋势线
- 14. 如何计算趋势线
- 15. 谷歌趋势中的R
- 16. PowerBI如何将趋势曲线添加到图表中?
- 17. 在ggplot2中添加加权最小平方趋势线
- 18. 如何找到R中的线性趋势?
- 19. 如何将趋势线添加到r两个变量,两个y轴图?
- 20. 在趋势表中添加分类
- 21. 使用ggplot绘制趋势线R
- 22. openpyxl趋势线和R平方值
- 23. 类型y〜x +趋势或y〜x +“趋势”的R公式,其中趋势/“趋势”用作指标?
- 24. 如何添加多个趋势线大熊猫
- 25. 如何在R图上建立一个趋势线
- 26. VBA:趋势线添加或删除(如果存在)
- 27. 在Reporting Services中向条形图添加趋势线
- 28. 报告趋势线
- 29. D3趋势线域
- 30. 如何为每个人添加趋势线,以便在Base R中不使用循环?
这是非常有用的。我如何使用Date x轴使用此答案? – pomarc 2015-09-03 10:58:51