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

alpha in geom_stars() breaks na.value in scale_fill() #445

Open
dholstius opened this issue Aug 4, 2021 · 3 comments
Open

alpha in geom_stars() breaks na.value in scale_fill() #445

dholstius opened this issue Aug 4, 2021 · 3 comments

Comments

@dholstius
Copy link

dholstius commented Aug 4, 2021

If this is a value in ggplot2, I'm happy to file it there. Thanks for a wonderful package!

Summary

✅ Rendering finite values in a semi-transparent manner works by itself, via alpha = I(...) in geom_stars()
✅ Rendering NAs as fully transparent also works by itself, via na.value = "transparent" in, e.g., scale_fill_viridis_c()
❌ Combining the two does not work; the former seems to "override" or obviate the latter

Smallish reprex

# Setup
library(stars)
library(ggplot2)
x <- read_stars(system.file("tif/L7_ETMs.tif", package = "stars"))
x[[1]][x[[1]] < 30] <- NA # Set some values to be NA
# This works; NAs become fully transparent
ggplot() + geom_stars(data = x) + scale_fill_viridis_c(na.value = "transparent")

image

# This also works as intended; everything becomes 50% transparent
ggplot() + geom_stars(data = x, alpha = I(0.5))

image

# The combination does not work---NAs become white instead of fully transparent
ggplot() + geom_stars(data = x, alpha = I(0.5)) + scale_fill_viridis_c(na.value = "transparent")

image

@edzer
Copy link
Member

edzer commented Aug 30, 2021

Thanks! Seems a ggplot2 issue, as stars simply calls, for this case

# library(tibble)
ggplot() + geom_raster(data = as_tibble(x), aes(x = x, y = y, fill = L7_ETMs.tif), alpha = 0.5) + 
  scale_fill_viridis_c(na.value = "transparent")

@dholstius
Copy link
Author

Thank you, @edzer. An interpretation and a functional solution have been provided by @thomasp85 at tidyverse/ggplot2#4630.

@dholstius
Copy link
Author

Closed too soon. When trying to translate that solution (for geom_raster()) back to geom_stars(), I get this error:

Error: Aesthetics must be valid data columns. Problematic aesthetic(s): fill = stage(...). 
Did you mistype the name of a data column or forget to add after_stat()?

I can modify the reprex above or post a new one here as soon as I can (not immediately), if that's helpful.

@dholstius dholstius reopened this Nov 14, 2021
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

No branches or pull requests

2 participants