The landscapemetrics package in R is a powerful tool designed for the quantitative analysis of landscape patterns, commonly used in the fields of landscape ecology, environmental science, and spatial planning. It provides a comprehensive set of functions to calculate landscape metrics at three different levels: patch, class, and landscape. These metrics help describe spatial characteristics such as patch size, shape, edge length, diversity, and fragmentation of land cover types represented in categorical raster data (e.g., land use or vegetation maps). Built on top of R’s spatial data infrastructure, including packages like terra, raster, and sf, landscapemetrics supports reproducible and scalable workflows for both single and multi-temporal datasets. It is particularly valuable for ecological monitoring, conservation planning, and assessing the impact of human activities on natural habitats. Additionally, it offers visualization tools and integrates well with popular R plotting libraries, making it accessible and efficient for spatial data analysis.
Pre Requirements:
# 1. Install and Load Required Packages
install.packages("landscapemetrics") #calculating spatial metrics.
install.packages("landscapetools") #additional landscape utilities.
install.packages("raster") #reading and handling raster data.
install.packages("tidyverse") #plotting and data wrangling.
install.packages("janitor") #data cleaning (not directly used in the shown code).
library(landscapemetrics)
library(landscapetools)
library(raster)
library(tidyverse)
library(janitor)
#2. Load the Pre-Fire (Past) Raster Data
landscape <- raster("C:/Users/........./name of raster file.tif")
#3. Calculate Landscape Metrics (Pre-Fire)
landscape_mets <-calculate_lsm(landscape,
what = c("lsm_l_contag", #Contagion
"lsm_l_pd", #Patch density
"lsm_l_ed", #Edge density
"lsm_l_lsi", #landscape shape index
"lsm_l_lpi", #Largest patch index
"lsm_l_pr"), #Patch richness
directions = 8)
#The directions = 8 setting tells it to consider all 8 neighboring cells (like diagonal adjacency).
#Computes these metrics for the raster:
#lsm_l_contag: Contagion.
#lsm_l_pd: Patch density.
#lsm_l_ed: Edge density.
#lsm_l_lsi: Landscape shape index.
#lsm_l_lpi: Largest patch index.
#lsm_l_pr: Patch richness.
#more indices can be added from https://cran.r-project.org/web/packages/landscapemetrics/landscapemetrics.pdf
#Next 3 commands are optional
landscape_mets$metric<- c("Contagion", "Edge density", "Largest patch index", "landscape shape index", "Patch density", "Patch richness") # Renames metrics for clarity
landscape_mets$layer <- "Past" #This gives the layer a name associated with the landscape so we can bind them!
landscape_mets$layer <- factor(land1_mets$layer, levels = c("Past", "Current")) # This orders the data so pre-fire data is displayed first in our plots
#Show Data
landscape_mets
#Create matrix
allmetrics <- list(landscape_mets) #This creates a list of the data tables
metric_table <- do.call(rbind, allmetrics) #This binds the tables together
p <- ggplot(data = metric_table, mapping = aes(x = as.factor(layer), y = value, fill = layer))
p + geom_bar(stat = "identity") +
facet_wrap(~metric, scales = "free") +
xlab("Landscape") +
ggtitle("Landscape level metrics for Past and Present landscapes")
# Save the metric data
write_csv(metric_table, "C:/Users/......./name if csv file.csv")
# Save the plot
plot_path <- "C:/Users/...../name of plot.png"
ggsave(plot_path, plot = p +
geom_bar(stat = "identity") +
facet_wrap(~metric, scales = "free") +
xlab("Landscape") +
ggtitle("Landscape level metrics for Past and Present landscapes"),
width = 10, height = 6, dpi = 500)