R:如何将光栅图像/矩阵值转换为系列以获得多个光栅图像的箱线图?

R: how to turn raster image / matrix values into series in order to obtain boxplot for multiple raster images?

我有一系列代表温度数据的 tiff 文件(~40 个文件),我想为每个文件获取值分布的简单箱线图。我知道如何直接对光栅文件进行箱线图;但是,我想使用 ggplot(需要数据框基础)以特定方式排列各个图。

理想的解决方案是提供一个数据框,其中每个栅格图像的值都由一列表示,因为数据的 x-y 位置并不重要,但我不确定这里的最佳解决方案是什么?

您可以使用以下代码

library(tidyverse)
library(raster)

#Make a list of the files
files <- list.files(path="E:\...", #Provide the path containing the tif files
                    pattern="tif", full.names=TRUE, recursive=TRUE)

#Stack rasters
Stack <- stack(files)

#Convert the rasters into data frame
df <- as.data.frame(Stack, xy=TRUE) %>% 
  drop_na()

#Create boxplot for each raster layer
df %>% 
  pivot_longer(-c(x, y)) %>% 
  ggplot(aes(x = name, y = value)) +
  geom_boxplot()