tidyverse / tidyverse/ggplot2

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

Open
#4,567 2 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature guides :straight_ruler:
Dominant language
R
Stars
7k
Forks
2.1k
Avg merge
59m
Merged PRs (30d)
2

Description

Many scales offer the possibility to flag NAs with a particular colour on the plot but not in the legend.
For example, here in a map we flag missing values in orange but the orange is not shown in the guide at the right of the plot:

library(tidyverse)
library(rnaturalearth)
world_sf <- ne_countries(scale = "medium", returnclass = "sf")

ggplot() +
  geom_sf(mapping = aes(fill = lastcensus), data = world_sf) +
  scale_fill_fermenter(palette = 2, na.value = "orange")

Created on 2021-07-29 by the reprex package (v2.0.0)

AFAWK there is no direct way for the user to add the missing information. Of course, one can think of various hacks to do such thing but as the argument na.value is readily available, users may expect/wish to see that handled automatically.

Ideally, one could decide to add the NA colour on top or bottom (when vertical, left/right when horizontal) of the existing scale and one could also change the label associated with NA in the legend.

[Joint post with @liamdbailey]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the scale_fill_fermenter example and investigate how scales using na.value are represented in the legend guide. Define how the NA key should be positioned and labeled, then verify the result with a plot containing missing values and a continuous or binned fill scale.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.