Skip to content

ggplot2 ignores the user's input for transparency via NA's in scale_fill_manual() #5929

@binkleym

Description

@binkleym

I encountered a problem with scale_fill_manual where ggplot was explicitly ignoring the NA (for transparency) I was attempting to use.

The problem appears to be this commit, where the default value for NA's is set to "gray50". Instead of trusting that the user knows what they're doing when they set NA for transparency, it overrides them behind their back without letting them know.

c9adeed

It was quite maddening to try to figure out what was going on, since scale_fill_manual() changes the behavior silently.

It would be useful (and merciful on anyone trying to debug similar things) to have scale_fill_manual() spit out a message on NA's similar to

Setting NA's to "gray50".    Please add 'na.value = NA' if you wish transparency

Code to demonstrate issue:

library(tidyverse)
library(sf)

map_wind <-
  read_sf("https://www.spc.noaa.gov/products/outlook/archive/2024/day1otlk_20240530_0100_hail.nolyr.geojson")

ggplot() +
    theme_bw() + 
    geom_sf(data = map_wind, aes(fill = LABEL), color = NA, show.legend = TRUE) +

    scale_fill_manual(
      limits = c("0.05",     "0.15",    "0.30",    "0.45",    "0.60",   "SIGN"),
      values = c("#c5a393", "#ffeb80", "#ff8080", "#ff00ff", "#912cee", NA)
    )

The image on the left shows the incorrect behavior. scale_fill_manual() is silently replacing my NA (transparency) with 'gray50'.

The image on the right shows how it should look. scale_fill_manual() does what I tell it to do, and renders transparency as actual transparency.

na_value

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions