This is probably very simple, but I haven't been using my R muscles in quite some time, and I may be missing something obvious. I trained two forecasting models, and for each model I got fitted values, lower and upper prediction bounds, from day 1 to today, and then extrapolated to the future. I can easily plot the forecast from one model:
library(ggplot2)
day <- c(1:7,7:30)
dframe <- data.frame(day = day,
future = seq_along(day) > 7,
m1_lwr = day^2, m1_fit = 1.2*day^2, m1_upr = 1.4*day^2,
m2_lwr = day^1.8, m2_fit = 1.2*day^1.8, m2_upr = 1.4*day^1.8)
ggplot(dframe, aes(x = day, color = future)) +
geom_line(aes(y = m1_fit)) +
geom_line(aes(y = m1_lwr), linetype = "dashed") +
geom_line(aes(y = m1_upr), linetype = "dashed") +
labs(title=paste("model 1 forecast"))
Created on 2020-03-06 by the reprex package (v0.3.0)
Now, I would like to make a plot with two facets, same y axis, where one facet contains the results of model_1
, the other one contains the results of model_2
, and such that data are still colored by the future
variable. I recall this could be done with facet_wrap
, but IIRC I should first reshape my data so that:
- the last three columns are appended to the three columns before them
- all the other columns are duplicated
- a new column is added, containing something like
model 1
for the first 31 rows andmodel 2
for the other 31.
How do I do that?