我知道如何使用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