This map shows the probability that stream temperatures will exceed a maximum threshold. The data come from a stream habitat modeling project that I worked on in the past. I wanted to practice importing and viewing ArcGIS shapefile data within a Leaflet map. I load data from my research project and use US Tiger data to clip to one of the local counties. In the Leaflet, I add the spatial polygon data, a custom color palette, a pop-up label, and a legend.
# Load libraries
library(leaflet)
#library(maptools)
library(rgdal)
#library(rgeos)
library(tigris)
library(sp)
library(raster)
# Load temperature simulation data
TEMPdat <- readOGR(".","TEMP_AllNodes")
## OGR data source with driver: ESRI Shapefile
## Source: ".", layer: "TEMP_AllNodes"
## with 11280 features
## It has 34 fields
## Integer64 fields read as strings: PointID IDnum
# Transform to match leaflet map projection
TEMPdat <- spTransform(TEMPdat, CRS("+proj=longlat +ellps=GRS80"))
# Use package tigris to get state-county data
wake <- tracts(state = 'NC', county = c('Wake'))
# Transform to match leaflet map projection
wake <- spTransform(wake, CRS("+proj=longlat +ellps=GRS80"))
#Extract just the TEMP data for Wake County
TEMPwake <- intersect(wake, TEMPdat)
#Extract just the data column for this leaflet demo
TEMPwake <- TEMPwake[,"ETEMPCam"]
# Create a continuous palette function
pal <- colorNumeric(
palette = "Reds",
domain = TEMPwake$ETEMPCam)
#Create the popup text
popup <- paste0("<strong>P(Exceedance): </strong>",
TEMPwake$ETEMPCam)
# Generate the leaflet map
leaflet() %>% addTiles() %>%
setView(lng = -78.5, lat=35.8,zoom=10) %>%
addPolygons(data=TEMPwake,weight=0.5,color = ~pal(ETEMPCam), fillOpacity=0.3, popup=popup) %>%
addLegend("bottomright", pal = pal, values = TEMPwake$ETEMPCam,
title = "Probability of Exceeding<br>Suitable Temperature",
opacity = 0.3
)