Home > database >  How to better differentiate lines in ggplot for the visually impaired?
How to better differentiate lines in ggplot for the visually impaired?

Time:01-25

In running the below code, I'm trying to better delineate and visualize the plot lines in order to help the visually impaired (like me, colour blind and blind spots in my visual field). Even the dashed lines as currently presented look too much alike and I can hardly tell them apart. How can one add markers, and other distinguishing features as illustrated below, so this is easier to read?

enter image description here

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 %>%
  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)

CodePudding user response:

The harder part is split the legend in two, we can use ggnewscale::new_scale_color() to create a new layer for our geom_line with the actual data. You can find others way of doing this in this enter image description here

  • Related