1

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)
Village.Idyot
  • 1,359
  • 2
  • 8
  • Does this answer your question? [ggplot2: Divide Legend into Two Columns, Each with Its Own Title](https://stackoverflow.com/questions/27803710/ggplot2-divide-legend-into-two-columns-each-with-its-own-title) – M Aurélio Jan 24 '23 at 13:43
  • I'm digging through that post to see how it applies to my situation – Village.Idyot Jan 24 '23 at 14:29
  • 1
    The simplest thing would be to use the `size` parameter for your line layers to make them as thick as you want. – Jon Spring Jan 24 '23 at 16:10

1 Answers1

1

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 answers.

  • To make the different points we use geom_point(aes(shape = .model, color = .model))
  • To make the line thicker we use geom_line(..., linewidth = 1.2)
  • scales::hue_pal() is used so we can mantain ggplot2 original colors
# Produce forecasts for the next 4 months:
fcTmp <- fit %>%
  forecast(new_data = tmpNext4)

legend_title = "Forecast"
# Plot the forecasts:
fcTmp %>%
  ggplot(aes(Month, .mean)) +
  geom_line(aes(linetype = .model, color = .model)) +
  geom_point(aes(shape = .model, color = .model)) + 
  scale_color_manual(aesthetics = "colour",values = scales::hue_pal()(4)[-1]) + 
  labs(linetype =legend_title ,shape = legend_title, colour = legend_title) +
  ggnewscale::new_scale_color() +
  geom_line(aes(y = StateX, color = "Actual"), data = tmpNext4,  linewidth = 1.2) +
  geom_line(aes(y = StateX), data = tmp, linewidth = 1.2)  +
  scale_color_manual(aesthetics = "colour",values = scales::hue_pal()(1), name = "")

Created on 2023-01-24 with reprex v2.0.2 

enter image description here

M Aurélio
  • 830
  • 5
  • 13