Skip to content

Instantly share code, notes, and snippets.

@marcosci
Created September 25, 2018 10:03
Show Gist options
  • Save marcosci/a0af2503848d210fc3aa927c33e854e9 to your computer and use it in GitHub Desktop.
Save marcosci/a0af2503848d210fc3aa927c33e854e9 to your computer and use it in GitHub Desktop.
animation to illustrate sample_lsm (sample landscape metrics in buffers around points of interest)
library(NLMR)
library(sf)
library(purrr)
library(rcartocolor)
library(gganimate)
library(classInt)
landscape <- NLMR::nlm_fbm(50, 100, fract_dim = 1.2)
breaks <- landscape %>%
raster::getValues() %>%
na.omit() %>%
classInt::classIntervals(n=5, style="jenks")
landscape <- raster::cut(landscape, breaks=breaks$brks, include.lowest=T)
landscape <- raster::as.data.frame(landscape, xy = TRUE)
x <- st_sfc(st_polygon(list(rbind(c(0,0),
c(50,0),
c(50,100),
c(0,100),
c(0,0)))))
y <- st_sample(x, 100)
y_buffered <- purrr::map(seq_len(100), function(buffer_width){
st_sf(bufferwidth = rep(buffer_width / 20, 100), st_buffer(y, buffer_width / 20))
}) %>% do.call(rbind, .)
a1 <- ggplot(y_buffered) +
geom_raster(data= landscape,
aes(x,y,fill = layer)) +
coord_equal() +
geom_sf(data = y_buffered,
inherit.aes = FALSE,
alpha = 0.4) +
rcartocolor::scale_fill_carto_c(palette = "TealRose") +
theme_void() +
theme(panel.grid.major = element_line(colour = 'transparent')) +
transition_states(bufferwidth, transition_length = 1, state_length = 1) +
guides(fill = FALSE)
animate(a1, width = 500, height = 1000, duration = 6)
anim_save("/home/marco/Downloads/sample_lsm.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment