使用 ggplot 和以下代碼,我創建了下面的圖:
df_have %>%
ggplot(aes(date, num_visits))
geom_line(colour = "blue")
ggtitle("Number of Customers Visiting Store by Month")
geom_smooth(method = "lm", fit = num_visits ~ date, se=F, color="red")
xlab("Date")
ylab("Number of Visits")

但是,我想知道如何:
添加區分藍線(訪問次數)和紅線(最佳擬合線)的圖例和
用紅線的方程注釋這個圖表,形式為
y = mx c
謝謝!
編輯:示例資料
df_have
num_visits date
1 19.825 2021.000
2 25.025 2021.083
3 27.950 2021.167
4 25.425 2021.250
5 29.575 2021.333
uj5u.com熱心網友回復:
更新:一種方法可能是使用ggpmisc包:
library(tidyverse)
library(ggpmisc)
my.formula <- y ~ x
df_have %>%
ggplot(aes(date, num_visits))
geom_line(aes(colour = "blue"))
ggtitle("Number of Customers Visiting Store by Month")
geom_smooth(method = "lm", fit = num_visits ~ date, se=F, aes(color="red"))
stat_poly_eq(formula = my.formula,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE)
xlab("Date")
ylab("Number of Visits")
scale_color_manual(values = c('blue', 'red'), labels=c('num_visits', 'y = mx c'))

First_answer: You mean this kind of output?
df_have %>%
ggplot(aes(date, num_visits))
geom_line(aes(colour = "blue"))
ggtitle("Number of Customers Visiting Store by Month")
geom_smooth(method = "lm", fit = num_visits ~ date, se=F, aes(color="red"))
xlab("Date")
ylab("Number of Visits")
scale_color_manual(values = c('blue', 'red'), labels=c('num_visits', 'y = mx c'))

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427669.html
