-
Notifications
You must be signed in to change notification settings - Fork 2.1k
ggplot2 ignores the user's input for transparency via NA's in scale_fill_manual() #5929
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
Comments
Simplified reprex: library(ggplot2)
ggplot(mpg, aes(displ, hwy, colour = drv)) +
geom_point() +
scale_colour_manual(values = c("red", "blue", NA)) The simple solution would be to use ggplot(mpg, aes(displ, hwy, colour = drv)) +
geom_point() +
scale_colour_manual(values = c("red", "blue", "transparent")) Created on 2024-06-03 with reprex v2.1.0 I think the scale is doing exactly what is supposed to do by translating |
The above is contrary to ggplot2's own documentation: https://ggplot2.tidyverse.org/reference/aes_colour_fill_alpha.html "An NA, for a completely transparent colour." By the docs, NA should select transparency, not "gray50". Which I'm not asking to revert, I'm just asking that info be printed when NA's are overridden so that people don't spend hours debugging when default behavior changes. |
@teunbrand Not sure I follow:
In your reprex, It's a question of ordering of operations. Do you first convert all I haven't looked at the code, it may be a minor fix changing ordering of operations or it may be a major headache and not worth the cost to fix, but I agree it feels wrong to me the way it currently works. |
Ok, seems like the issue is that the code uses Line 985 in 28aec3a
Definitely not correct behavior in my opinion but maybe not worth the effort to fix. |
If it shouldn't, then Lines 984 to 988 in 28aec3a
Briefly, |
According to the documentation, |
(Simple thought experiment: I might want to translate |
The docs of |
Here is an example to show my thought experiment is not completely contrived. The only working solution is to specify the transparent color as library(tidyverse)
library(sf)
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
texas_income <- readRDS(url("https://wilkelab.org/SDS375/datasets/Texas_income.rds"))
p <- texas_income %>%
mutate(
top_ten = if_else(is.na(median_income), NA, rank(desc(median_income)) <= 10)
) %>%
ggplot(aes(fill = top_ten)) +
geom_sf() +
theme_bw()
# works
p +
scale_fill_manual(
breaks = c(TRUE, FALSE, NA),
labels = c("top 10 income", "other", "no data"),
values = c(`TRUE` = "#D55E00", `FALSE` = "transparent")
) # doesn't work
p +
scale_fill_manual(
breaks = c(TRUE, FALSE, NA),
labels = c("top 10 income", "other", "no data"),
values = c(`TRUE` = "#D55E00", `FALSE` = NA)
) # doesn't work either
p +
scale_fill_manual(
breaks = c(TRUE, FALSE, NA),
labels = c("top 10 income", "other", "no data"),
values = c(`TRUE` = "#D55E00", `FALSE` = NA),
na.translate = FALSE
) Created on 2024-06-03 with reprex v2.0.2 |
My problem is already solved, but for other R users who might hit this issue because the default behavior has changed, I still think printing something that lets them know NA's are being overridden is the right thing. Changing documentation doesn't change existing code bases, nor does it inform users used to the older behavior that things have changed. By letting users know what's changed and how to fix it, you get the new behavior, and they don't spend hours debugging. It's win-win for everyone. |
@binkleym I suspect that's a completely different issue. Either way, can you use my example to make a simple reprex? |
Life suddenly got busy, but I will as soon as I can. |
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
Code to demonstrate issue:
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.
The text was updated successfully, but these errors were encountered: