stars icon indicating copy to clipboard operation
stars copied to clipboard

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

Open dholstius opened this issue 2 years ago • 3 comments

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

Summary

:white_check_mark: Rendering finite values in a semi-transparent manner works by itself, via alpha = I(...) in geom_stars() :white_check_mark: Rendering NAs as fully transparent also works by itself, via na.value = "transparent" in, e.g., scale_fill_viridis_c() :x: 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

dholstius avatar Aug 04 '21 18:08 dholstius

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")

edzer avatar Aug 30 '21 18:08 edzer

Thank you, @edzer. An interpretation and a functional solution have been provided by @thomasp85 at https://github.com/tidyverse/ggplot2/issues/4630.

dholstius avatar Nov 14 '21 18:11 dholstius

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 avatar Nov 14 '21 18:11 dholstius