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

WIP: Missing values in colourbars #6023

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Aug 2, 2024

This PR aims to fix #4567.

Briefly, guide_colourbar() and guide_coloursteps() use the GuideLegend class to render a key for missing values.
Note that continuous scales never automatically include NA in breaks (AFAIK), which means that you'd have to add these manually.
It'd also will prevent some complaints that suddenly the colourbars show NAs by default (as they don't).

In the plot below you can see a demo of what these look like. Note that putting NA as the first break will put it on top of the bar, whereas using it as any other break will put it on the bottom. However, maybe we should reverse this logic to follow the order of the other breaks?

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
brks <- c(10, 15, 20, 25, 30)

ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = cty, fill = cty), shape = 21) +
  # NA break first
  scale_colour_viridis_c(
    breaks = c(NA, brks),
    labels = c("missing", brks),
    na.value = "red"
  ) +
  # NA break last
  scale_fill_viridis_c(
    breaks = c(brks, NA),
    option = "inferno", 
    na.value = "dodgerblue"
  )

Created on 2024-08-02 with reprex v2.1.1

I'm not 100% happy with the code as-is; it feels a little bit messy. Most of the logic is captured in Guide$assemble_drawing() but it doesn't quite feel like the correct place. In addition, getting binned breaks to include NA is messier than I'd like. Lastly, maybe we should also do this for guide_bins(), but I feel like it has less priority due to it being a more niche guide (and I'd rather not mess with binned breaks again).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: plotting the NA info in the legend when using scales with na.value=
1 participant