Home > front end >  Unexpected color changes after upgrading to ggplot2 3.3.4
Unexpected color changes after upgrading to ggplot2 3.3.4

Time:12-03

The picture shows the difference before and after upgrading ggplot2 from ver.3.3.3 to ver. 3.3.4 enter image description here

library(RColorBrewer)
library(ggplot2)

df <- structure(list(name = structure(c(11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L), .Label = c("5303 B", "5303 A", 
"5302 B", "5302 A", "5301 B", "5301 A", "5202 B", "5202 A", "5201 B", 
"5201 A", "5101 B", "5101 A"), class = "factor"), stadio = c(2, 
2, 4, 4, 6, 6, 8, 8, 10, 10, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 
1, 1, 3, 3, 7, 7, 9, 9, 11, 11, 1, 1, 3, 3, 7, 7, 9, 9, 11, 11
), variable = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("start_date", 
"end_date"), class = "factor"), value = c("15/10/2021", "03/01/2022", 
"03/01/2022", "30/03/2022", "30/03/2022", "30/03/2023", "30/03/2023", 
"15/09/2023", "15/09/2023", "29/12/2023", "15/10/2021", "03/01/2022", 
"03/01/2022", "30/03/2022", "30/03/2022", "30/03/2023", "30/03/2023", 
"15/09/2023", "15/09/2023", "29/12/2023", "26/11/2021", "14/01/2022", 
"14/01/2022", "30/06/2022", "30/06/2022", "30/03/2023", "30/03/2023", 
"15/09/2023", "15/09/2023", "29/12/2023", "26/11/2021", "14/01/2022", 
"14/01/2022", "30/06/2022", "30/06/2022", "30/03/2023", "30/03/2023", 
"15/09/2023", "15/09/2023", "29/12/2023"), rating = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("2", "3"), class = "factor")), row.names = c(NA, 
-40L), class = c("tbl_df", "tbl", "data.frame"))

ggplot(df, aes(as.POSIXct(as.Date(value, "%d/%m/%Y")), name, colour = factor(stadio,levels=1:11)))  
  guides(alpha = "none",
         colour = guide_legend(
           override.aes = list(size = 4)))  
  geom_line(aes(size=rating, alpha=rating))  
  scale_alpha_manual(values=c(.5,1))  
  labs(colour="(Actual/Baseline)", x = "", y = "")  
  scale_colour_manual(breaks = c("1", "3", "7","9","11"), values = brewer.pal(11,"PiYG"))  
  scale_size_manual(breaks = levels(df$rating),  values = as.integer(levels(df$rating)), guide = "none") 

CodePudding user response:

From the help:

"values: a set of aesthetic values to map data values to. The values will be matched in order (usually alphabetical) with the limits of the scale, or with breaks if provided. If this is a named vector, then the values will be matched based on the names instead. Data values that don't match will be given na.value."

So in this line:

scale_colour_manual(breaks = c("1", "3", "7","9","11"), values = brewer.pal(11,"PiYG"))

You have 5 breaks defined so ggplot is only using the first 5 colors of the palette. If you remove "breaks = c("1", "3", "7","9","11")" the plot is correct but not the legend is displaying all 11 values.

To plot all 11 colors and have the legend only display half of the values in the legend, you need to define a function to select which values to display.

oddones <- function(x) {
   ifelse(as.integer(x)%%2, x, "")
}

ggplot(df, aes(as.POSIXct(as.Date(value, "%d/%m/%Y")), name, colour = factor(stadio,levels=1:11)))  
   guides(alpha = "none",  colour = guide_legend(override.aes = list(size = 4)))  
   geom_line(aes(size=rating, alpha=rating))  
   scale_alpha_manual(values=c(.5,1))  
   labs(colour="(Actual/Baseline)", x = "", y = "")  
   scale_colour_manual(breaks = oddones, values = brewer.pal(11,"PiYG"))  
   scale_size_manual(breaks = levels(df$rating),  values = as.integer(levels(df$rating)), guide = "none") 
  • Related