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.
I would give it a try, although the Q should be improved. So you have two questions:
There are two approaches here, you can classify your
rast
to create categories or you can use a variation ofggplot2::scale_fill_binned()
when plotting. I think is better to use the first approach by applyingterra::classify
so you have better control of the groups.NA
data in raster.Usually in the
ggplot2::scale_fill_*
use the parametersna.value="transparent
andna.translate = FALSE
to avoid these data to be plotted.See an example with toy data:
Created on 2024-01-04 with reprex v2.0.2