[英]How to plot trend line with regression equation in R?
我的數據是這樣的
日期 | 速度 |
---|---|
1/2019 | 4500 |
2/2019 | 3400 |
2019 年 3 月 | 5300 |
2019 年 4 月 | 2000 |
日期是我的自變量,速度是我的因變量。
我正在嘗試使用回歸方程對趨勢線進行 plot 以了解是否存在上升趨勢或下降趨勢。
我嘗試使用此代碼,但它沒有在圖中顯示方程式。
ggscatter(a, x = "Date", y = "Speed", add = "reg.line") +
stat_cor(label.x = 03/2019, label.y = 3700) +
stat_regline_equation(label.x = 03/2019, label.y = 3600)
#> `geom_smooth()` using formula 'y ~ x'
這是我通常使用的方法:
library(tidyverse)
library(lubridate)
library(ggpmisc)
df <- tibble::tribble(
~Date, ~Speed,
"1/2019", 4500L,
"2/2019", 3400L,
"3/2019", 5300L,
"4/2019", 2000L
)
df$Date <- lubridate::my(df$Date)
ggplot(df, aes(x = Date, y = Speed)) +
geom_point() +
geom_smooth(method = "lm", formula = y ~ x, se = FALSE) +
stat_poly_eq(formula = x ~ y,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE)
使用 p 值:
geom_point() +
geom_smooth(method = "lm", formula = y ~ x, se = FALSE) +
stat_poly_eq(formula = x ~ y,
aes(label = paste(..eq.label.., ..rr.label.., ..p.value.label.., sep = "~~~")),
parse = TRUE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.