我正在尝试使用 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年)以及如何更改格式(每月,而不仅仅是年份)。 预先感谢您!
你应该看看
scale_x_date()
。特别是 date_breaks
和 date_labels
参数。
请参阅此处的相关文档:https://ggplot2.tidyverse.org/reference/scale_date.html