在运行下面的代码时,我试图更好地描绘和可视化情节线,以帮助视觉受损的人(像我一样,色盲和视野盲点)。即使是目前呈现的虚线看起来也太相似了,我几乎无法区分它们。如何添加标记和其他区别特征,如下图所示,使其更容易阅读?
代码:
library(feasts)
library(fable)
library(ggplot2)
library(tsibble)
tmp <- data.frame(
Month = c(1,2,3,4,5,6,7,8,9,10),
StateX = c(1527,1297,933,832,701,488,424,353,302,280)
) %>%
as_tsibble(index = Month)
tmpNext4 <- data.frame(
Month = c(11,12,13,14),
StateX = c(211,182,153,125)
) %>%
as_tsibble(index = Month)
# Fit the models to tmp dataframe:
fit <- tmp %>%
model(
Mean = MEAN(StateX),
`Naïve` = NAIVE(StateX),
Drift = NAIVE(StateX ~ drift())
)
# Produce forecasts for the next 4 months:
fcTmp <- fit %>%
forecast(new_data = tmpNext4)
# Plot the forecasts:
fcTmp %>%
ggplot(aes(Month, .mean)) +
geom_line(aes(linetype = .model, color = .model)) +
geom_line(aes(y = StateX, linetype = "Actual", color = "Actual"), data = tmpNext4) +
geom_line(aes(y = StateX), data = tmp)
1条答案
按热度按时间m1m5dgzv1#
困难的部分是将图例一分为二,我们可以使用
ggnewscale::new_scale_color()
为geom_line
创建一个新的图层,其中包含实际数据。您可以在answers中找到其他方法来完成此操作。geom_point(aes(shape = .model, color = .model))
geom_line(..., linewidth = 1.2)
scales::hue_pal()
被使用,因此我们可以保持ggplot2原始颜色