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

时间:2021-05-23 06:33:22

标签: r ggplot2 raster boxplot

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

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

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码

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()