删除ggplot中缺少日期之间的连接线

时间:2019-06-03 10:56:57

标签: r date ggplot2 plot

我想绘制一个缺少日期和值的时间序列。这是一个示例:

library(lubridate)
date_list = seq(ymd('1990-05-01'),ymd('2000-09-30'),by='day')
date_list = date_list[which(month(date_list) %in% c(5:9))]

value_list1 = sample(1:40, 1683, replace=TRUE)
value_list2 = sample(1:40, 1683, replace=TRUE)

testsample = data.frame(Date = date_list, Value1 = value_list1, Value2 = value_list2)

library(ggplot2)
ggplot(data = testsample, aes(x = Date)) +
  geom_line(aes(y = Value1), color = "black", size = 1, alpha=0.5) +
  geom_line(aes(y = Value2), color = "red", size = 1, alpha=0.5) +
  labs(subtitle="testplot", 
       x = "year", 
       y = "values") +
  scale_x_date(date_labels="%y",date_breaks  ="1 year")

我没有从11月到4月的日期和数据。

我的情节看起来像这样:

enter image description here

如何删除年份之间的那些连接线?我读过有关将日期转换为因子的信息,但是对此我不确定。还有其他解决方案吗?

1 个答案:

答案 0 :(得分:3)

一种解决方案是指定组的外观,以匹配要通过线连接的组。

在您的情况下,这是年份:

ggplot(data = testsample, aes(x = Date, group = year(Date))) +
  geom_line(aes(y = Value1), color = "black", size = 1, alpha=0.5) +
  geom_line(aes(y = Value2), color = "red", size = 1, alpha=0.5) +
  labs(subtitle="testplot", 
       x = "year", 
       y = "values") +
  scale_x_date(date_labels = "%y", date_breaks  ="1 year")

enter image description here

基于Gregors注释,我们还可以将隐式缺失值更改为显式缺失值,例如使用tidyr::complete

testsample2 <- tidyr::complete(testsample, Date = seq(min(Date), max(Date), by = "day"))
ggplot(data = testsample2, aes(x = Date)) +
  geom_line(aes(y = Value1), color = "black", size = 1, alpha=0.5) +
  geom_line(aes(y = Value2), color = "red", size = 1, alpha=0.5) +
  labs(subtitle="testplot", 
       x = "year", 
       y = "values") +
  scale_x_date(date_labels = "%y", date_breaks  ="1 year")