Search code examples
rggplot2terrarescaletidyterra

Control rescaling when binning continuous values in tidyterra


I'm trying to use tidyterra's scale_*_whitebox_b() to bin continuous values using custom breaks that are unevenly distributed, to emphasize the difference in the low values. I'm having trouble because the binned scale automatically rescales the color palette to match the size of my breaks, which means that the low values at the bottom of the range are similarly colored and hard to distinguish.

library(ggplot2)
library(terra)
library(tidyterra)

#sample data, with some sparse high values
r <- rast(nrows=10, ncols=10)
values(r) <- abs(rnorm(100))
r[17] <- 12
r[22] <- 17
r[72] <- 7
breaks=c(0, 0.25, 0.5, 1, 2, 5, 10, 20)

ggplot() +
  geom_spatraster(data=r) +
  scale_fill_whitebox_b("viridi", 
                        breaks=breaks, direction=-1)

ggplot figure

The documentation for scale_fill_whitebox_b() points to the underlying ggplot binning scale constructors, which has a rescaler argument, but I can't figure out how to call it for scale_fill_whitebox. With ggplot scale_fill_steps, you can do this using values and limits:

ggplot(r) +
  geom_raster(aes(x=x, y=y, fill=lyr.1)) + 
  scale_fill_stepsn(colors=terrain.colors(7),
                    breaks=breaks,
                    values = scales::rescale(breaks),
                    limits = range(breaks))

ggplot figure

But scale_fill_whitebox doesn't accept a "values" argument, and I haven't had any luck playing around with the rescaler argument.

I'd like to replicate the colors I get when I use the same parameters with a contour plot; here the color palette is evenly distributed across my specified breaks.

ggplot() +
  geom_spatraster_contour_filled(data=r, breaks=breaks) +
  scale_fill_whitebox_d("viridi", direction=-1)

ggplot figure


Solution

  • Instead of using scale_fill_whitebox_xxx one option would be to use scale_fill_stepsn with your desired whitebox.colors. Additionally note that I use the midpoints of the bins in values= to get the proper colors as explained in Specify asymetrical breaks in scale_fill_stepsn():

    library(ggplot2)
    library(terra)
    library(tidyterra)
    
    set.seed(123)
    
    # sample data, with some sparse high values
    r <- rast(nrows = 10, ncols = 10)
    values(r) <- abs(rnorm(100))
    r[17] <- 12
    r[22] <- 17
    r[72] <- 7
    breaks <- c(0, 0.25, 0.5, 1, 2, 5, 10, 20)
    
    midpoints <- breaks[-1] - diff(breaks) / 2
    
    ggplot() +
      geom_spatraster(data = r) +
      scale_fill_stepsn(
        colors = whitebox.colors(7, palette = "viridi", alpha = 1, rev = TRUE),
        breaks = breaks,
        values = scales::rescale(
          midpoints,
          from = range(breaks)
        ),
        limits = range(breaks),
        guide = guide_coloursteps(reverse = TRUE)
      )
    

    enter image description here