ggpot2-水平轴上的多行时间

问题描述 投票:0回答:1

我正在尝试使用 R 中的 ggplot2 绘制图表。我想要绘制的变量是时间序列,并且超过一个(3 个变量)。我面临的问题是,在横轴,即时间轴上,只出现年份,而不是日期(每月),具体来说,只出现几年,即2000年、2010年、2020年。

我一直在使用的代码如下,我得到的结果附在照片中。

require(ggplot2)

library(tidyr)
library(dplyr)

#date<-seq(as.Date("1997/1/1"), as.Date("2023/12/1"),by="months")
df <-data_for_use_all_spills %>%
select(date, `TSI (TVP-VAR)`, 'GPR to inflation (TVP-VAR)','CPU to inflation (TVP-VAR)') %>%
  gather(key = "variables", value="value", -date)
head(df, 3)

ggplot(df, aes(x = date, y = value)) + 
  geom_line(aes(color = variables), linewidth = 1) +
  scale_color_manual(values = c("cornflowerblue","orange","red2")) +
  theme_light(base_size=15)+
  labs(y= "Spillover effects", x = "time")
  

我的问题是如何使更多日期出现在水平轴上,而不是只有三年(2000年、2010年、2020年)以及如何更改格式(每月,而不仅仅是年份)。 预先感谢您!

date ggplot2
1个回答
0
投票

你应该看看

scale_x_date()
。特别是
date_breaks
date_labels
参数。

请参阅此处的相关文档:https://ggplot2.tidyverse.org/reference/scale_date.html

© www.soinside.com 2019 - 2024. All rights reserved.