0

I have individual rasters for the modeled distribution of different species in different seasons. I'd like to plot the rasters using ggplot and facet_grid(), with each column representing a different season, and each row representing a different species. I've tried creating a RasterStack from the individual rasters, converting the RasterStack to a stars object. However, I think I would need to assign attributes to each raster or the RasterStack or stars object (species name and season) to be able to use facet_grid()?

Here's a start with some simulated rasters:

library(NLMR)
library(stars)
library(tidyverse)
library(raster)

# simulating rasters for each season x species combination
spring_mall <- nlm_mpd(ncol = 50, nrow = 50, roughness = 0.6)
fall_mall <- nlm_mpd(ncol = 50, nrow = 50, roughness = 0.6)
spring_rndu <- nlm_mpd(ncol = 50, nrow = 50, roughness = 0.6)
fall_rndu <- nlm_mpd(ncol = 50, nrow = 50, roughness = 0.6)

# stack
r <- stack(c(spring_mall, fall_mall, spring_rndu, fall_rndu))

# rename
names(r) <- c('spring_mall', 'fall_mall', 'spring_rndu', 'fall_rndu')

# convert to stars object
r_stars <- r %>%
  st_as_stars()

# plot...I'd like the plot to look like this, except the panels are labeled
# just by season on top, just by species on side
ggplot() +
  geom_stars(data = r_stars) +
  facet_wrap(~band, ncol = 2)
Jason
  • 892
  • 1
  • 8
  • 19

0 Answers0