【发布时间】:2021-06-17 05:06:29
【问题描述】:
#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
这是我的图表代码。
看起来就是这样。我想在我的图表中添加图例,而不必整理我的数据。因为那样我就不能按照我的意愿格式化我的图表了。
在我得到有用的评论之后。
Upper95
Lower95
Upper80
Lower80
dfb
Plot1
geom_ribbon(aes(ymin=Upper95, ymax=Lower95, fill='95% 预测水平')) +
geom_ribbon(aes(ymin=Upper80, ymax=Lower80, fill='80% 预测水平')) +
geom_line(data=dfb, aes(x=TeslaWeeklyPrices.Date, y=fcast1, color="预测值"),size=1)+
geom_point(shape = 19, aes(color = "Observed Values"), 填充 = "白色", 大小 = 1 ,)+
scale_fill_manual(values=c('95% 预测水平'='slategray2', '80% 预测水平'="bisque"), breaks=c('95% 预测水平', '80% 预测水平') ) +
scale_color_manual(values=c("Predicted Values"="red","Observed Values"= "blue"), breaks=c('Predicted Values', 'Observed Values'))+ 指南(color=guide_legend(title=NULL),fill=guide_legend(title=NULL)) +
主题(legend.margin = margin(b=0, t=-1000))+
theme_light(base_size = 12)
情节1
那么我的蓝点怎么会看起来像图例中的点而不是一条线。以及如何将我的 2 个图例之间的边距设为 0?
我可以设置它的背景颜色,使它看起来像一个独立的部分而不是图形的一部分吗?
这是我在一篇论文中看到的一个例子。
【问题讨论】: