library("dplyr")
library("ggplot2")
Let's start with this simple plot:
faithful %>%
    ggplot(aes(waiting, eruptions, color = eruptions > 3)) +
    geom_point()

Now, let's introduce some missing values in the class variable:
faithful_with_missings <-
    faithful %>%
    mutate(eruptions_class = ifelse(eruptions > 3, '>3', '<=3'),
           eruptions_class = ifelse(rbinom(n(), 1, 0.9), eruptions_class, NA_character_))
           ##eruptions_class = as.factor(eruptions_class))
faithful_with_missings$eruptions_class %>% table(exclude = NULL)
#> .
#>  <=3   >3 <NA> 
#>   89  151   32
Redo the plot with missings:
ggplot(faithful_with_missings, aes(waiting, eruptions, color = eruptions_class)) +
    geom_point()

Here is my question: Is there a way to plot the NA points, but to omit NA in the legend?
My desired output is:

You could set the categories to show up in the legend via the breaks argument of the scale. Hence, to drop the NAs you could use a lambda function in scale_color_discrete to drop the NAs:
library(ggplot2)
ggplot(faithful_with_missings, aes(waiting, eruptions, color = eruptions_class)) +
  geom_point() +
  scale_color_discrete(breaks = ~ .x[!is.na(.x)])

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With