Home > Back-end >  How to vary line types when using the autoplot() function to help the colour-blind among us?
How to vary line types when using the autoplot() function to help the colour-blind among us?

Time:01-25

Running the code below plots actual data (black line) against 4-month forecasts for that data. However, the forecast lines are indistinguishable to me since I don't see colours. How can the lines be distinguished from each other (with the actual (non-forecast) data line, the black line, made thicker than the others), either via dashed lines or the use of markers? In XLS I used dashed lines/markers to distinguish.

I have fooled around with ggplot(...scale_linetype_manual(values = c("TRUE" = "solid", "FALSE" = "dotted"))...) with no luck.

Code:

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 %>%
  autoplot(tmp, level = NULL)  
  autolayer(tmpNext4, StateX, colour = "black")  
  labs(y = "Units",
       title = "Units reaching dead state X",
       subtitle = "(Months 11 - 15)")  
  guides(colour = guide_legend(title = "Forecast"))

CodePudding user response:

fcTmp %>%
  ggplot(aes(Month, .mean))  
  geom_line(aes(linetype = .model, color = .model))  
  geom_line(aes(y = StateX, linetype = "Next4", color = "Next4"), data = tmpNext4)  
  geom_line(aes(y = StateX), data = tmp)

enter image description here

  • Related