当使用
tidyterra
进行多层栅格绘图时,当其中一层具有空值“NA”时,facet_*
功能似乎无法有效工作。不知道这是一个bug还是有更好的解决方案?
library(terra)
library(tidyverse)
library(tidyterra)
m <- matrix(c(1:24,NA), nrow=5, ncol=5)
n <- matrix(rep(5,time=25),nrow = 5,ncol = 5)
dt <- rast(c(A = rast(m),B = rast(n)))
ggplot()+
geom_spatraster(data = dt,na.rm = TRUE)+
scale_fill_viridis_c(na.value = "transparent")+
facet_grid(~lyr)+
theme_bw()
! Plotting only layer "A" of class <integer>
Warning message:
Mixed layer classes found in `tidyterra::geom_spat*()`.
预期成果:
data <- terra::as.data.frame(dt,xy=TRUE) %>%
pivot_longer(cols = c(A,B),names_to = 'layer', values_to = 'pre')
head(data)
ggplot(data = data,aes(x=x,y=y,fill = pre))+
geom_tile(na.rm = TRUE)+
scale_fill_viridis_c(na.value = "transparent")+
facet_grid(~layer)+
theme_bw()