Search code examples
rggplot2rasterboxplot

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


I have a series of tiff files representing temperature data (~40 files) for each of which I would like to get a simple boxplot of the value distribution. I know how to boxplot the raster files directly; however, I would like to use ggplot (requires dataframe base) to arrange individual plots in a specific manner.

Ideally a solution would provide a dataframe where each raster image's values are represented by a column as the x-y-position of the data is unimportant, but I am not sure of what the best solution is here?


Solution

  • You can use the following code

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