Choc_waffles
Choc_waffles

Reputation: 537

r: How to connect line breaks in ggplot

Reusing the example in this question, but for a different question; Plot time series and forecast simultaneously using ggplot2

As you can see, there is a gap between 'my observation' and 'my forecast' (between 350, and 351).

Why is there a gap? I have a 1 day forecast, and the forecast line itself is completely missing from the chart. Please help!

Upvotes: 1

Views: 199

Answers (1)

BJK
BJK

Reputation: 153

It's because your last 'observation' was made when time=350.

df[df$time > 349 & df$time <= 351, ]
##   time          M         isin
## 26  350 -0.2180864 observations
## 27  351  1.2246175  my_forecast
## 51  351  3.7502526  upper_bound
## 75  351 -1.3010176  lower_bound

You can add a data point at time=351 and isin=observations, if you want to connect them.

df <- rbind(df, data.frame(
  time = c(351), M = c(1.2246175), isin = c("observations")
))
ggplot(df, aes(x = time, y = M, color = isin)) +
  geom_line()

Upvotes: 1

Related Questions