我正在尝试绘制一些预测与实际数据的图表,类似于以下内容:
# Some random data
x <- seq(1: 10)
y_pred <- runif(10, min = -10, max = 10)
y_obs <- y_pred + rnorm(10)
# Faking a CI
Lo.95 <- y_pred - 1.96
Hi.95 <- y_pred + 1.96
my_df <- data.frame(x, y_pred, y_obs, Lo.95, Hi.95)
ggplot(my_df, aes(x = x, y = y_pred)) +
geom_line(aes(colour = "Forecasted Data"), size = 1.2) +
geom_point(aes(x = x, y = y_obs, colour = "Actual Data"), size = 3) +
geom_ribbon(aes(ymin=Lo.95, ymax=Hi.95, x=x, linetype = NA, colour = "Confidence Interval"), alpha=0.2) +
theme_grey() +
scale_colour_manual(
values = c("gray30", "blue", "red"),
guide = guide_legend(override.aes = list(
border=c(NA, NA, NA),
fill=c("gray30", "white", "white"),
linetype = c("blank", "blank", "solid"),
shape = c(NA, 19, NA))))
这个图像看起来是这样的:
我的唯一问题是图例中线条(即预测数据)的符号周围有红色边框。有没有办法在不破坏图像的其他部分的情况下将其去除呢?
aes(color = "Actual Data")
并不是正确的用法。通常,我们使用aes()
来映射变量(列名未引用)。我会收集数据以创建一个指示是否观察到或预测y的列,然后使用带有aes()
的geom_point()
将点按该列着色。 - neilfws