Spatial Autocorrelation

Spatial autocorrelation activity (part 1)

# Adapted from "An Introduction to R for Spatial Analysis and Mapping" by Chris
                                        # Brusdon and Lex Comber
library(sf)
library(tmap)
library(RColorBrewer)

wi_hazards <- st_read("https://gitlab.com/mhaffner/data/-/raw/master/wi_hazards.geojson")

rand_maps <- function(variable, colors) {
  if (variable == 'Flood') {
    plot_var <- 'flod_evt'
  } else if (variable == 'Tornado') {
    plot_var <- 'torn_evt'
  } else if (variable == 'Hail') {
    plot_var <- 'hail_evt'
  } else {
    plot_var <- variable
  }

  real_data_i <- sample(1:6, 1)

  maps <- lapply(1:6, function(i) {
    dat <- wi_hazards
    if (i == real_data_i) {
      dat$plot_val <- dat[[plot_var]]
    } else {
      dat$plot_val <- sample(dat[[plot_var]])
    }

    tm_shape(dat) +
      tm_fill("plot_val",
              style = "equal",
              n = 9,
              palette = brewer.pal(9, colors),
              title = "") +
      tm_borders() +
      tm_layout(title = as.character(i),
                legend.show = FALSE)
  })

  print(tmap_arrange(maps, ncol = 2, nrow = 3))

  return(real_data_i)
}

val <- rand_maps("Flood", "Blues")

## use the function with different variables and colors

##### challenge 1
## use the rand_maps function with different variables and colors with
## schemes that can be found here:
## http://colorbrewer2.org/#type=sequential&scheme=PuBuGn&n=8
##
display.brewer.all()