Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to omit NA in the legend only

Tags:

r

legend

ggplot2

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:

like image 588
Andreas Avatar asked Oct 27 '25 04:10

Andreas


1 Answers

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)])

enter image description here

like image 147
stefan Avatar answered Oct 29 '25 18:10

stefan