tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.48k stars 2.02k forks source link

Could the NA default colour be set for a discrete scale where it is wanted to set colours for multiple levels? #5849

Closed davidhodge931 closed 5 months ago

davidhodge931 commented 5 months ago

Hi,

It'd be great if ggplot2 had functionality to set the default NA colour for a discrete colour/fill scale where one wants to use different colours for different numbers of levels.

So for example, in the below code, I'd like to be able to also set the default NA colour to "red".

library(tidyverse)
library(palmerpenguins)

options(
    ggplot2.discrete.colour =
      list(
        c('red', "blue"),
        c("green", "yellow", "navy")
      )
  )

penguins |> 
  ggplot() +
  geom_point(aes(x = flipper_length_mm, 
                 y = body_mass_g, 
                 colour = sex))
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).


penguins |> 
  mutate(species = as.character(species)) |> 
  mutate(species = ifelse(flipper_length_mm > 220, NA, species)) |> 
  ggplot() +
  geom_point(aes(x = flipper_length_mm, 
                 y = body_mass_g, 
                 colour = species))
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

Created on 2024-04-19 with reprex v2.1.0

teunbrand commented 5 months ago

The infrastructure to pass na.value via the palette isn't present, so this would be pretty invasive to solve. I'm not saying it can't be done, but it wouldn't be easy.

davidhodge931 commented 5 months ago

Ah okay, feel free to close if you think @teunbrand