# Adapted from "An Introduction to R for Spatial Analysis and Mapping" by Chris
# Brusdon and Lex Comber
## load required libraries: sf, GISTools, haffutils
## read hazard data as an sf object
wi.hazards <- st_read("https://gitlab.com/mhaffner/data/-/raw/master/wi_hazards.geojson")
## create function
rand_maps <- function(variable, colors) {
## this function will draw six maps - one will contain the actual data, the
## other five will be randomizations
## account for three different variable types
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
}
## convert from sf to sp
wi.hazards <- as(wi.hazards, 'Spatial')
## set up the plotting area (3 rows, 2 columns)
par(mfrow=c(3,2))
# set the page margins
par(mar=c(1,1,1,1))
## select a random value between 1 and 6 for the actual data
real.data.i <- sample(1:6,1)
## create color scheme
shades <- auto.shading(wi.hazards[[plot.var]], n = 9, cols = brewer.pal(9, colors))
## create maps
for (i in 1:6) {
## case where i is equal to the value corresponding to the real data, plot
## the actual data
if (i == real.data.i) {
choropleth(wi.hazards, wi.hazards[[plot.var]], shades)
} else {
## if i does not correspond to the real data, plot a randomization using the sample function
choropleth(wi.hazards, sample(wi.hazards[[plot.var]]), shades)
}
title(i)
}
return(real.data.i)
}
## use the function here, set the result equal to a variable
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 color palettes first
display.brewer.all()