#Plot the in sample forecasts against the actual values
#Build the confidence interval
Upper95 <- fcast1 + 1.96*sqrt(Var1)
Lower95 <- fcast1 - 1.96*sqrt(Var1)
Upper80 <- fcast1 + 1.28*sqrt(Var1)
Lower80 <- fcast1 - 1.28*sqrt(Var1)
#Create a data frame
dfb <- data.frame(TeslaWeeklyPrices$Date,fcast1,TeslaWeeklyPrices$TeslaPrices,Upper95,Lower95,Upper80,Lower80)
#Make the Plot
Plot1 <- ggplot(dfb, aes(x=TeslaWeeklyPrices.Date, y=TeslaWeeklyPrices.TeslaPrices))+
geom_ribbon(data=dfb,aes(ymin=Upper95,ymax=Lower95),fill = "slategray2")+
geom_ribbon(data=dfb,aes(ymin=Upper80,ymax=Lower80),fill = "bisque")+
geom_line(data=dfb, aes(x=TeslaWeeklyPrices.Date, y=fcast1),size=1, color="red1")+
geom_point(shape = 19, fill = "white", colour = "blue" ,size = 1)+
theme_light(base_size = 11) +
ylab("Tesla Stock price ($)") + xlab("Date (weeks)")
Plot1
That is my code for my graph.
That is how it looks. I want to add legends in my graph without having to tidy my data. Because then I can not format my graph as I want.
After the useful comment I got.
Upper95 <- fcast1 + 1.96*sqrt(Var1)
Lower95 <- fcast1 - 1.96*sqrt(Var1)
Upper80 <- fcast1 + 1.28*sqrt(Var1)
Lower80 <- fcast1 - 1.28*sqrt(Var1)
dfb <- data.frame(TeslaWeeklyPrices$Date,fcast1,TeslaWeeklyPrices$TeslaPrices,Upper95,Lower95,Upper80,Lower80)
Plot1 <- ggplot(dfb, aes(x=TeslaWeeklyPrices.Date, y=TeslaWeeklyPrices.TeslaPrices))+
geom_ribbon(aes(ymin=Upper95, ymax=Lower95, fill='95% prediction level')) +
geom_ribbon(aes(ymin=Upper80, ymax=Lower80, fill='80% prediction level')) +
geom_line(data=dfb, aes(x=TeslaWeeklyPrices.Date, y=fcast1, color="Predicted Values"),size=1)+
geom_point(shape = 19, aes(color = "Observed Values"), fill = "white", size = 1 ,)+
scale_fill_manual(values=c('95% prediction level'='slategray2', '80% prediction level'="bisque"), breaks=c('95% prediction level', '80% prediction level')) +
scale_color_manual(values=c("Predicted Values"="red","Observed Values"= "blue"), breaks=c('Predicted Values', 'Observed Values'))+ guides(color=guide_legend(title=NULL),fill=guide_legend(title=NULL) ) +
theme(legend.margin = margin(b=0, t=-1000))+
theme_light(base_size = 12)
Plot1
So how can my blue points look as points in the Legend and not as a line. And how can i det the margin to 0 between my 2 legends?
Can I format the background color of this so it looks like an independent part and not as part of the graph?
That is an example I saw in one paper.