The R
package tmap
includes extensive functionality for making quality maps. We will use it here.
library(tmap)
library(dplyr)
library(sf)
Import shape file of class sf
(created in Weighting and Transformations tutorial):
pv <- read_sf(dsn="../data", layer="pov_uscounties_sf")
class(pv)
## [1] "sf" "tbl_df" "tbl" "data.frame"
For the example, we will only look at a single state, Wisconsin (state FIPS code 55):
wisc <- pv %>%
filter(STATEFP=="55")
str(wisc)
## Classes 'sf', 'tbl_df', 'tbl' and 'data.frame': 72 obs. of 21 variables:
## $ FIPS : chr "55001" "55003" "55005" "55007" ...
## $ AREA : num 0.2 0.311 0.265 0.457 0.157 ...
## $ PERIMETER : num 2.13 3.75 2.09 3.07 2.1 ...
## $ STATE_x : chr "55" "55" "55" "55" ...
## $ COUNTY : chr "001" "003" "005" "007" ...
## $ CO99_D00_ : chr "553" "250" "387" "257" ...
## $ CO99_D00_I: chr "552" "249" "386" "256" ...
## $ NAME_x : chr "Adams" "Ashland" "Barron" "Bayfield" ...
## $ LSAD : chr "06" "06" "06" "06" ...
## $ LSAD_TRANS: chr "County" "County" "County" "County" ...
## $ COUNTYFP : chr "001" "003" "005" "007" ...
## $ STATEFP : chr "55" "55" "55" "55" ...
## $ X1 : num 3047 3048 3049 3050 3051 ...
## $ STATE_y : chr "55" "55" "55" "55" ...
## $ NAME_y : chr "Adams County" "Ashland County" "Barron County" "Bayfield County" ...
## $ totpopn : num 18643 16866 44963 15013 226778 ...
## $ poverty : num 102.3 113.8 86.8 124 66.7 ...
## $ ag : num 22.05 20.87 37.61 27.11 6.63 ...
## $ manu : num 74.1 79.2 128.3 41 112.2 ...
## $ retail : num 48.2 48.7 61.7 47.6 67.2 ...
## $ geometry :sfc_MULTIPOLYGON of length 72; first list element: List of 1
## ..$ :List of 1
## .. ..$ : num [1:321, 1:2] -89.8 -89.8 -89.8 -89.7 -89.7 ...
## ..- attr(*, "class")= chr "XY" "MULTIPOLYGON" "sfg"
## - attr(*, "sf_column")= chr "geometry"
## - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
## ..- attr(*, "names")= chr "FIPS" "AREA" "PERIMETER" "STATE_x" ...
#basic grey map with county borders
basemap <- tm_shape(wisc) +
tm_borders()
basemap + tm_fill()
We can also plot one of the variables, poverty:
povmap <- basemap + tm_fill(col="poverty")
povmap
Create a map with panels of maps:
#create and store maps
agmap <- basemap + tm_fill(col="ag")
retailmap <- basemap + tm_fill(col="retail")
manumap <- basemap + tm_fill(col="manu")
#those maps into panels
tmap_arrange(povmap, agmap, retailmap, manumap)
Make an interactive map you can scroll over and visually explore poverty in Wisconsin:
tmap_mode("view")
basemap + tm_polygons(col="poverty")
tmap_mode("plot")
basemap + tm_polygons(col="poverty") +
tm_symbols(col = "red", size = "totpopn", alpha=0.5) +
tm_layout(legend.title.size = 0.7,
legend.text.size = 0.5,
legend.position = c("right","top"),
legend.bg.color = "white",
legend.bg.alpha = 1)
Add a point to Madison, Wisconsin:
mad <- data.frame(c("Madison,Wisconsin"),
lat=43.0731,
long = -89.4012)
site_mad <- sf::st_as_sf(mad, coords = c("long", "lat"),
crs = "+proj=longlat +datum=WGS84",
agr="identity")
basemap + tm_shape(site_mad) +
tm_dots(size=2, col="red", shape=21) +
tm_compass() +
tm_layout(title="Madison, WI", title.position = c("center","top"))