0

I'm able to convert the following df to tsibble object and plot using gg_season():

library(tsibble)
library(feasts)
library(tidyr)
library(dplyr)

df <- structure(list(date = structure(c(18292, 18321, 18352, 18382, 
18413, 18443, 18474, 18505, 18535, 18566, 18596, 18627, 18658, 
18686, 18717, 18747, 18778, 18808, 18839, 18870, 18900, 18931, 
18961, 18992), class = "Date"), value1 = c(-2.94, -40.61, -6.89, 
3.04, -3.5, 0.18, 6.79, 9.08, 9.35, 10.92, 20.53, 18.04, 24.6, 
154.6, 30.4, 32.1, 27.7, 32.1, 19.2, 25.4, 28, 26.9, 21.7, 20.9
), value2 = c(-12.66, 7.56, -1.36, -14.39, -16.18, 3.29, -0.69, 
-1.6, 13.47, 4.83, 4.56, 7.58, 28.7, 18.9, 39.1, 44, 52, 37.1, 
28.2, 32.7, 17.2, 20.4, 31.4, 19.5)), class = "data.frame", row.names = c(NA, 
-24L))

# Convert to tsibble object and plot using gg_season()
df %>%
  pivot_longer(value1:value2) %>%
  mutate(date = yearmonth(date)) %>%
  mutate(year = year(date)) %>% 
  as_tsibble(index = date, key = name) %>%
  gg_season(value) +
  geom_point() # +
  # scale_color_manual(values = c('2020' = 'blue', '2021' = 'red'))

Now I try to customize colors based on different years, ie., setting blue for 2020, and red for 2021. I've added scale_color_manual(values = c('2020' = 'blue', '2021' = 'red')), but I didn't succeed yet, how could I do that correctly? Thanks.

enter image description here

Reference:

how to change the color in geom_point or lines in ggplot

ah bon
  • 9,293
  • 12
  • 65
  • 148

1 Answers1

1
...
gg_season(value, pal = c("#3333FF", "#FF3333")) +
  geom_point() 

The year scale here is a continuous one (explaining why the scale_color_manual line produces "Error: Continuous value supplied to discrete scale"). But we can give gg_season a vector of color codes to use in its pal parameter.

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53