如何使用 ggplot2 为散点图绘制多条趋势线?

Posted

技术标签:

【中文标题】如何使用 ggplot2 为散点图绘制多条趋势线?【英文标题】:How can I draw multiple trendlines for scatterplots with ggplot2? 【发布时间】:2021-08-10 22:03:08 【问题描述】:

我正在尝试绘制一个基本的差异实现图(对于那些学习过经济学的人来说,您可能很熟悉)。所以我有两个独立的时间序列,涵盖 2015-2019 年。我想为两个时间序列绘制一条趋势线。问题是我为每个时间序列绘制了两个趋势线。第一个涵盖 1/2015-7/2017,第二个应涵盖 8/2017-12/2019。我已经设法通过以下使用 geom_smooth 来绘制线条:

    ggplot()+
      geom_line(data=timeseries2,aes(x=month,y=price_mean),color="red", size=1)+
      geom_line(data=timeseries1,aes(x=month,y=price_mean), color="dodgerblue4", size=1)+
      geom_smooth(data=timeseries2,aes(x=month,y=price_mean,color=time),method=lm,se=FALSE)+
      geom_smooth(data=timeseries1,aes(x=month,y=price_mean,color=time),method=lm,se=FALSE)+
      theme_classic()+
      labs(x="Month",y="Price")+
      geom_vline(xintercept =data1$month[32],linetype="dashed")

这是上面代码的结果:

这里的第一个问题是我不需要之前/之后的图例,我想用 Timeseries1/Timeseries2 图例替换它。第二个问题是 geom_smooth 趋势线的颜色。我希望 timeseries1 散点图具有相同的颜色,但之前和之后的条件都有单独的趋势线。这同样适用于其他时间序列。现在颜色是相同的,取决于时间而不是使用的时间序列。

【问题讨论】:

您的问题解决了吗? 【参考方案1】:

我创造了三种情节(我认为第二种是你的意思)

第一个情节:

library(dplyr)
library(ggplot2)

# Timeseries simulation
timeseries1 <- data.frame(
  month = seq(as.Date("2015/1/1"), as.Date("2019/12/1"), by = "month"),
  price_mean = sample(100:300, 60, replace = FALSE)
)

timeseries2 <- data.frame(
  month = seq(as.Date("2015/1/1"), as.Date("2019/12/1"), by = "month"),
  price_mean = sample(300:600, 60, replace = FALSE)
)

data1 <- rbind(timeseries1, timeseries2)

# dividing timeseries into two parts
timeseries1_B <- timeseries1 %>%
  filter(month >= as.Date(data1$month[32]))

timeseries1_A <- timeseries1 %>%
  filter(month <= as.Date(data1$month[32]))

timeseries2_B <- timeseries2 %>%
  filter(month >= as.Date(data1$month[32]))

timeseries2_A <- timeseries2 %>%
  filter(month <= as.Date(data1$month[32]))

#First plot
ggplot(data = timeseries1, aes(x = month, y = price_mean))+
  geom_line(data=timeseries1_A, color="dodgerblue4", size=1) + 
  geom_line(data = timeseries2_A, color = "dodgerblue4", size = 1) + 
  geom_line(data=timeseries1_B, color="red", size=1) + 
  geom_line(data = timeseries2_B, color = "red", size = 1) +
  geom_smooth(data=timeseries1_A,aes(x=month,y=price_mean, color = "dodgerblue4"),method=lm,se=FALSE)+
  geom_smooth(data=timeseries2_A,aes(x=month,y=price_mean,color="dodgerblue4"),method=lm,se=FALSE)+
  geom_smooth(data=timeseries1_B,aes(x=month,y=price_mean, color = "red"),method=lm,se=FALSE)+
  geom_smooth(data=timeseries2_B,aes(x=month,y=price_mean,color="red"),method=lm,se=FALSE)+
  theme_classic()+
  labs(x="Month",y="Price")+
  geom_vline(xintercept =data1$month[32],linetype="dashed") +
  scale_color_manual(name = "Time", labels = c("Timeseries1", "timeseries2"), values = c("dodgerblue4","red"))

第二个情节:

#the same code as above then ...

#second plot:  
  
ggplot(data = timeseries1, aes(x = month, y = price_mean))+
  geom_line(data=timeseries1_A, color="dodgerblue4", size=1) + 
  geom_line(data = timeseries2_A, color = "red", size = 1) + 
  geom_line(data=timeseries1_B, color="dodgerblue4", size=1) + 
  geom_line(data = timeseries2_B, color = "red", size = 1) +
  geom_smooth(data=timeseries1_A,aes(x=month,y=price_mean, color = "dodgerblue4"),method=lm,se=FALSE)+
  geom_smooth(data=timeseries2_A,aes(x=month,y=price_mean,color="red"),method=lm,se=FALSE)+
  geom_smooth(data=timeseries1_B,aes(x=month,y=price_mean, color = "dodgerblue4"),method=lm,se=FALSE)+
  geom_smooth(data=timeseries2_B,aes(x=month,y=price_mean,color="red"),method=lm,se=FALSE)+
  theme_classic()+
  labs(x="Month",y="Price")+
  geom_vline(xintercept =data1$month[32],linetype="dashed") +
  scale_color_manual(name = "Time", labels = c("Timeseries1", "Timeseries2"), values = c("dodgerblue4","red"))
    

第三个情节


# Timeseires simulation
timeseries1 <- data.frame(
  month = seq(as.Date("2015/1/1"), as.Date("2019/12/1"), by = "month"),
  price_mean = sample(100:300, 60, replace = FALSE)
)

timeseries2 <- data.frame(
  month = seq(as.Date("2015/1/1"), as.Date("2019/12/1"), by = "month"),
  price_mean = sample(300:600, 60, replace = FALSE)
)


data1 <- rbind(timeseries1, timeseries2)


ggplot()+
  geom_line(data=timeseries2,aes(x=month,y=price_mean),color="red", size=1)+
  geom_line(data=timeseries1,aes(x=month,y=price_mean), color="dodgerblue4", size=1)+
  geom_smooth(data=timeseries2,aes(x=month,y=price_mean,color="red"),method=lm,se=FALSE)+
  geom_smooth(data=timeseries1,aes(x=month,y=price_mean,color="dodgerblue4"),method=lm,se=FALSE)+
  theme_classic()+
  labs(x="Month",y="Price")+
  geom_vline(xintercept =data1$month[32],linetype="dashed") +
  scale_color_manual(name = "Time", labels = c("Timeseries1", "timeseries2"), values = c("dodgerblue4","red"))

它会创建如下内容:

【讨论】:

以上是关于如何使用 ggplot2 为散点图绘制多条趋势线?的主要内容,如果未能解决你的问题,请参考以下文章

如何将数据椭圆叠加在 ggplot2 散点图上?

Fig4-a ggplot2绘制箱线图叠加散点图2020-12-14

matplotlib:通过用于为散点图着色的对数颜色条值对 2D 线进行着色

R语言为散点图添加凸包(convex hull):数据预处理(创建一个包含每组数据凸包边界的数据集)ggplot2使用geom_polygon函数为可视化图像添加凸包(convex hull)

R语言散点图可视化:自定义标题和标签拟合回归线lowess为散点图添加平滑拟合线修改散点图中点颜色和点符号分组散点图添加图例pairs可视化散点图矩阵ggplt2可视化lattice

散点图怎么添加标准差和斜率(散点图怎么添加趋势线)