问题
I want to plot a time series with missing dates and values. Here is an example:
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")
I have no dates and data from November to April.
My plot looks like this:
How can I remove those connection lines between the years? I read about transforming the dates into factors, but I am not sure about this. Is there another solution?
回答1:
One solution would be to specify the group aesthetics to match the groups you want to have connected by lines.
In your case this is the year:
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")
Building on Gregors comment we can also change implicit missing values to explicit missing values, e.g. using 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")
来源:https://stackoverflow.com/questions/56425915/removing-connetion-lines-between-missing-dates-in-ggplot