This tutorial shows how to plot samc analyses using ggplot2. It is based on the code in the basic tutorial.
# First step is to load the libraries. Not all of these libraries are stricly
# needed; some are used for convenience and visualization for this tutorial.
library("terra")
library("samc")
library("ggplot2")
# "Load" the data. In this case we are using data built into the package.
# In practice, users will likely load raster data using the raster() function
# from the raster package.
<- samc::example_split_corridor$res
res_data <- samc::example_split_corridor$abs
abs_data <- samc::example_split_corridor$init
init_data
# To make things easier for plotting later, convert the matrices to rasters
<- samc::rasterize(res_data)
res_data <- samc::rasterize(abs_data)
abs_data <- samc::rasterize(init_data)
init_data
# Setup the details for our transition function
<- list(fun = function(x) 1/mean(x), # Function for calculating transition probabilities
rw_model dir = 8, # Directions of the transitions. Either 4 or 8.
sym = TRUE) # Is the function symmetric?
# Create a samc object using the resistance and absorption data. We use the
# recipricol of the arithmetic mean for calculating the transition matrix. Note,
# the input data here are matrices, not RasterLayers.
<- samc(res_data, abs_data, model = rw_model)
samc_obj
# Convert the initial state data to probabilities
<- init_data / sum(values(init_data), na.rm = TRUE)
init_prob_data
# Calculate short- and long-term mortality metrics and long-term dispersal
<- mortality(samc_obj, init_prob_data, time = 4800)
short_mort <- mortality(samc_obj, init_prob_data)
long_mort <- dispersal(samc_obj, init_prob_data)
long_disp
# Create rasters using the vector result data for plotting.
<- map(samc_obj, short_mort)
short_mort_map <- map(samc_obj, long_mort)
long_mort_map <- map(samc_obj, long_disp) long_disp_map
# Convert the landscape data to RasterLayer objects, then to data frames for ggplot
<- as.data.frame(res_data, xy = TRUE, na.rm = TRUE)
res_df <- as.data.frame(abs_data, xy = TRUE, na.rm = TRUE)
abs_df <- as.data.frame(init_data, xy = TRUE, na.rm = TRUE)
init_df
# When overlaying the patch raster, we don't want to plot cells with values of 0
<- init_df[init_df$lyr.1 != 0, ]
init_df
# Plot the example resistance and mortality data using ggplot
<- ggplot(res_df, aes(x = x, y = y)) +
res_plot geom_raster(aes(fill = lyr.1)) +
scale_fill_viridis_c() +
geom_tile(data = init_df, aes(x = x, y = y, fill = lyr.1), fill = "grey70", color = "grey70") +
ggtitle("Resistance Data") +
coord_equal() + theme_bw()
print(res_plot)
<- ggplot(abs_df, aes(x = x, y = y)) +
abs_plot geom_raster(aes(fill = lyr.1)) +
scale_fill_viridis_c() +
geom_tile(data = init_df, aes(x = x, y = y, fill = lyr.1), fill = "grey70", color = "grey70") +
ggtitle("Absorption Data") +
coord_equal() + theme_bw()
print(abs_plot)
# Convert result RasterLayer objects to data frames for ggplot
<- as.data.frame(short_mort_map, xy = TRUE, na.rm = TRUE)
short_mort_df <- as.data.frame(long_mort_map, xy = TRUE, na.rm = TRUE)
long_mort_df <- as.data.frame(long_disp_map, xy = TRUE, na.rm = TRUE)
long_disp_df
# Plot short-term mortality
<- ggplot(short_mort_df, aes(x = x, y = y)) +
stm_plot geom_raster(aes(fill = lyr.1)) +
scale_fill_viridis_c() +
geom_tile(data = init_df, aes(x = x, y = y, fill = lyr.1), fill = "grey70", color = "grey70") +
ggtitle("Short-term Mortality") +
coord_equal() + theme_bw()
print(stm_plot)
# Plot long-term mortality
<- ggplot(long_mort_df, aes(x = x, y = y)) +
ltm_plot geom_raster(aes(fill = lyr.1)) +
scale_fill_viridis_c() +
geom_tile(data = init_df, aes(x = x, y = y,fill = lyr.1), fill = "grey70", color = "grey70") +
ggtitle("Long-term Mortality") +
coord_equal() + theme_bw()
print(ltm_plot)
# Plot long-term dispersal
<- ggplot(long_disp_df, aes(x = x, y = y)) +
ltd_plot geom_raster(aes(fill = lyr.1)) +
scale_fill_viridis_c() +
geom_tile(data = init_df, aes(x = x, y = y, fill = lyr.1), fill = "grey70", color = "grey70") +
ggtitle("Long-term Dispersal") +
coord_equal() + theme_bw()
print(ltd_plot)