rterratidyterra

How to visualize the continuous raster data by quantile colors in R


I have a raster list (raster data with 6 layers) with Continuous values. Now I would like to visualize them using R::tidyterra. I have two questions: 1、How to represent the continuous values by quantile color (for example 5 types). 2、How to deal with the NA data in raster.

library(terra)
library(tidyterra)

tif <- list.files(path = "/Users/Aa/Desktop/HR", pattern = '*.tif',
                  full.names = TRUE, recursive = TRUE)
HR <- rast(tif)
names(HR) <- c(2012: 2017)

ggplot() +
  geom_spatraster(data = HR) +
  facet_wrap(~lyr, ncol = 2)

Obviously we'll get 6 graphs, and the labeling is continuous values, as below (left). However, I'd like to show the values with different colors based on the quartiles (for example 5 types) of each layers respectively, such as below (right). I don't know if there's a convenient way to achieve it.

enter image description here


Solution

  • I would give it a try, although the Q should be improved. So you have two questions:

    1. How to represent the continuous values by quantile color (for example 5 types).

    There are two approaches here, you can classify your rast to create categories or you can use a variation of ggplot2::scale_fill_binned() when plotting. I think is better to use the first approach by applying terra::classify so you have better control of the groups.

    1. How to deal with the NA data in raster.

    Usually in the ggplot2::scale_fill_* use the parameters na.value="transparent and na.translate = FALSE to avoid these data to be plotted.

    See an example with toy data:

    library(terra)
    #> terra 1.7.65
    library(tidyterra)
    #> 
    #> Attaching package: 'tidyterra'
    #> The following object is masked from 'package:stats':
    #> 
    #>     filter
    library(ggplot2)
    
    # Toy data
    data <- geodata::worldclim_country("Spain", "tmin", path = tempdir())
    
    # Resample to speed up
    data <- spatSample(data, size = 100000, "regular", as.raster = TRUE)
    
    # Six layers mocking your data
    HR <- subset(data, 1:6)
    names(HR) <- c(2012:2017)
    
    # Classify by quartiles
    quants <- classInt::classIntervals(values(HR, mat = FALSE), 
                                       style = "quantile", n = 5)
    #> Warning in classInt::classIntervals(values(HR, mat = FALSE), style =
    #> "quantile", : var has missing values, omitted in finding classes
    quants
    #> style: quantile
    #> [-11.5,3.2)   [3.2,6.2)   [6.2,9.7)  [9.7,14.3) [14.3,28.1] 
    #>       57157       58578       59333       58171       58589
    quants$brks
    #> [1] -11.5   3.2   6.2   9.7  14.3  28.1
    
    # Create categorical raster
    
    HR_class <- terra::classify(HR, quants$brks)
    
    HR_class
    #> class       : SpatRaster 
    #> dimensions  : 268, 374, 6  (nrow, ncol, nlyr)
    #> resolution  : 0.06149733, 0.06156716  (x, y)
    #> extent      : -18.5, 4.5, 27.5, 44  (xmin, xmax, ymin, ymax)
    #> coord. ref. : lon/lat WGS 84 (EPSG:4326) 
    #> source(s)   : memory
    #> varname     : ESP_wc2.1_30s_tmin 
    #> names       :        2012,        2013,        2014,        2015,        2016,        2017 
    #> min values  : (-11.5–3.2], (-11.5–3.2], (-11.5–3.2], (-11.5–3.2], (-11.5–3.2], (-11.5–3.2] 
    #> max values  : (14.3–28.1], (14.3–28.1], (14.3–28.1], (14.3–28.1], (14.3–28.1], (14.3–28.1]
    
    # And plot, note the na.translate param
    
    ggplot() +
      geom_spatraster(data = HR_class, na.rm = TRUE) +
      facet_wrap(~lyr, ncol = 2) +
      scale_fill_discrete(na.translate = FALSE)
    
    

    enter image description here

    Created on 2024-01-04 with reprex v2.0.2