Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NA key does not show up in legend when colour limits include NA #5749

Closed
davidhodge931 opened this issue Mar 5, 2024 · 9 comments · Fixed by #5759
Closed

NA key does not show up in legend when colour limits include NA #5749

davidhodge931 opened this issue Mar 5, 2024 · 9 comments · Fixed by #5759
Labels
bug an unexpected problem or unintended behavior scales 🐍
Milestone

Comments

@davidhodge931
Copy link

library(palmerpenguins)
library(tidyverse)

penguins |>
  ggplot() +
  geom_point(
    aes(x = flipper_length_mm, 
        y = body_mass_g,
        col = species)) +
  scale_colour_manual(values = c("red", "blue"), limits = c("Adelie", "Gentoo"))
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

penguins |>
  ggplot() +
  geom_histogram(
    aes(x = flipper_length_mm, 
        fill = species)) +
  scale_fill_manual(values = c("red", "blue"), limits = c("Adelie", "Gentoo"))
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> Warning: Removed 2 rows containing non-finite outside the scale range
#> (`stat_bin()`).

Created on 2024-03-06 with reprex v2.1.0

@teunbrand
Copy link
Collaborator

This is to be expected. For discrete scales, limits determine the 'universe' of values and if it doesn't include NA then NA shouldn't be shown.

A bug is that NA isn't shown as a key even if included as limits:

library(palmerpenguins)
library(ggplot2)

penguins |>
  ggplot() +
  geom_point(
    aes(x = flipper_length_mm, 
        y = body_mass_g,
        col = species)) +
  scale_colour_manual(
    values = c("red", "blue"), 
    limits = c("Adelie", "Gentoo", NA)
  )
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

Created on 2024-03-07 with reprex v2.1.0

@davidhodge931
Copy link
Author

Ah, okay it seems sort of inconsistent to me then, as if you set the limits to be only Adelie and Gentoo, it still plots NA

@teunbrand
Copy link
Collaborator

Sorry 'shouldn't be shown' should have been 'shouldn't be shown in the legend'. Apologies for the confusion

@davidhodge931
Copy link
Author

Oh okay. As a general rule, I think anything that is plotted should be in the legend. That last plot is weird the way there is no grey dot in the legend

library(palmerpenguins)
library(tidyverse)

p <- penguins |>
  ggplot() +
  geom_point(
    aes(x = flipper_length_mm, 
        y = body_mass_g,
        col = sex))

#I think there should be a legend NA value here if NA is plotted
p +
  scale_colour_manual(values = c("red", "blue"), limits = c("female", "male"))
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

#The NA is missing a grey dot in the legend
penguins |>
  ggplot() +
  geom_point(
    aes(x = flipper_length_mm, 
        y = body_mass_g,
        col = species)) +
  scale_colour_manual(
    values = c("red", "blue"), 
    limits = c("Adelie", "Gentoo", NA)
  )
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

Created on 2024-03-07 with reprex v2.1.0

@teunbrand
Copy link
Collaborator

teunbrand commented Mar 7, 2024

As a general rule, I think anything that is plotted should be in the legend

Well that is the default behaviour for scales that display a legend, but users can intervene by setting the limits or breaks of the scale.

@davidhodge931
Copy link
Author

Is the oob default for scale_colour_manual keep or censor?

@teunbrand
Copy link
Collaborator

Neither: there are no bounds to be out of, unlike continuous scales. For discrete scales, the default is to honour the na.translate setting.

@davidhodge931
Copy link
Author

Feel free to close, if you think everything is fine here

@teunbrand
Copy link
Collaborator

I think the NA-key not showing up when including NA in the limits is a bug, so let's keep this open for now.

@davidhodge931 davidhodge931 changed the title NA does not show in legends when limits are set NA key does not show up in legend when colour limits include NA Mar 7, 2024
@teunbrand teunbrand added this to the ggplot2 3.5.1 milestone Mar 7, 2024
@teunbrand teunbrand added bug an unexpected problem or unintended behavior scales 🐍 labels Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior scales 🐍
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants