1 Introduction to Mapview Maps

mapview provides functions to very quickly and conveniently create interactive visualizations of spatial data. Its main goal is to fill the gap of quick (not presentation grade) interactive plotting to examine and visually investigate both aspects of spatial data, the geometries, and their attributes.

It can also be considered a data-driven application programming interface (API) for the leaflet package as it will automatically render correct map types, depending on the type of the data (points, lines, polygons, raster).

In addition, it makes use of some advanced rendering functionality that will enable viewing of much larger data than is possible with leaflet.

mapview() - view (multiple) spatial objects on a set of background maps

viewExtent() - view extent / bounding box of spatial objects

viewRGB() - view RGB true- or false-color images of raster objects

mapshot() - easily save maps (including leaflet maps) as HTML and/or png (or other image formats).

mapview() function can work for a quick view of the data, providing choropleths, background maps, and attribute popups. Performance varies on the object and customization can be tricky.

In this note, we will use three data sets to illustrate how make maps with mapview(): state population, state population (50 state), and state capitals.

2 Choropleth Map with mapview

library(tigris)
library(mapview)
library(dplyr)
library(sf)
#invisible({capture.output({
## Download shapefile for all states into R in tigris
## states() is a function in library {tigris}
us_geo <- states(cb = F, resolution = '20m')
## caution: need to tell R that GEOIS should be a character variable since
## the same GEOID is character variable in the shape file us_geo with
## some leading zeros!
pop_data <- read.csv("https://raw.githubusercontent.com/pengdsci/sta553/main/data/state_population_data.csv", colClasses = c(GEOID = "character"))
## merger two data use the primary key: GEOID.
all_data <- inner_join(us_geo, pop_data, by = c("GEOID" = "GEOID")) 
##
mapview(all_data, zcol = "PctChange10_20", layer.name = "Per Chg")

We can see some of the unique features of mapview:

  1. The default mapview() is created with one small line of code.

  2. mapview() Choose defaults based on the type of geospatial file. code mapview(all_data, zcol = "PctChange10_20")

  3. The default popup includes every field in my data that is useful to explore the data.

There are unwanted features in the above default choropleth map that need to be improved. For example, too much information in the hover text, lengthy legend title, etc. We will revisit this map and enhance the choropleth map.