OCHA-DAP / gghdx

HDX Theme, Scales, and Other Conveniences for 'ggplot2'
GNU General Public License v3.0
5 stars 0 forks source link

Fix NA transparent #27

Closed caldwellst closed 6 months ago

caldwellst commented 6 months ago

Fixes #24

zackarno commented 6 months ago

Nice nice, now I seem to be running into a new issue. I am extending the reprex from https://github.com/OCHA-DAP/gghdx/issues/24#issue-2253567148 to show below:

library(terra)
#> terra 1.7.65
library(tidyterra)
#> 
#> Attaching package: 'tidyterra'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(tidyverse)
library(gghdx)

# recreate raster in question
# save dput output of value array
array_vals <- structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
                          NA, NA), dim = c(17L, 15L, 1L))

# set res
resx <- 0.08333333
resy <- 0.08333333

num_rows <-  17
num_cols <-  15
xmn <- 43.08333 - 0.5 * resx
xmx <-  44.33333 + 0.5 * resx
ymn <- 2.5- 0.5 * resy
ymx <- 3.916667+ 0.5 * resy

# generate SpatExtent
r_ext <-  terra::ext(xmn, xmx, ymn, ymx)

# generate spatRaster
r <- rast(
  x=array_vals,
  extent =r_ext,
  crs= "EPSG:4326"
)

# base plot -- looks good!
r |> plot()


ggplot()+
  geom_spatraster(data=r)


ggplot()+
  geom_spatraster(data=r)+
  tidyterra::scale_fill_cross_blended_c(na.value = "transparent")

ok let's initialize gghdx()

gghdx()

ggplot()+
  geom_spatraster(data=r)

#> Error in `geom_spatraster()`:
#> ! Problem while mapping stat to aesthetics.
#> ℹ Error occurred in the 1st layer.
#> Caused by error in `ggplot2::continuous_scale()`:
#> ! argument "scale_name" is missing, with no default
#> Run `rlang::last_trace()` to see where the error occurred.
caldwellst commented 6 months ago

Ugh, annoying issue with ggplot2 with mismatches between 3.5.0 and later and earlier versions. See if the new changes have fixed it! Nice catch, thanks a lot.

zackarno commented 6 months ago

Ugh, annoying issue with ggplot2 with mismatches between 3.5.0 and later and earlier versions. See if the new changes have fixed it! Nice catch, thanks a lot.

sweet you fixed it!! reprex works now - and it looks like you also set na.value="transparent" as default when not specified. Wasn't sure if you were saying that wasn't desireable in the issue, but it seems correct to me (at least in this use-case).

Good to merge now!

caldwellst commented 6 months ago

Ah no, I think you're right, it's good! The default is set because there is always a "default" scale that is set. You can see that in gghdx(), the default scale is one of the gradient scales that has na.value = "transparent" as the default I think.

I have an explicit value for NA for the discrete scales, like a nice color, because by default I think NA values are not dropped when plotting discrete values unlike continuous ones. What do you think about that? We can decide what's best before we merge.

zackarno commented 6 months ago

Makes sense. Yes I like transparent for continuous fill and maybe a grey for discrete???

Potentially transparent could be okay for discrete fill as well... I am thinking of geom_point(), geom_bar() transparent fill would be fine because the outline color would be there.... I guess it would be weird if the background was white though.

Not sure what is best, but having na.value="transparent" for continuous raster vis is a big improvement. I think the base plot raster method deals with this by having an interpolation arg set to FALSE by default

zackarno commented 6 months ago

Extending the reprex further to showcase the default scale_fill_discrete na.value in the context of raster visualizations.

This seems good to me


r_factor <- as.factor(r)

ggplot()+
  geom_spatraster(data=r_factor)

Created on 2024-05-13 with reprex v2.0.2

caldwellst commented 6 months ago

Thanks for your checks and flagging all these issues! Merging now!