2.4 Making maps with R

2.4.1 ggplot2

map <- st_as_sf(map)
ggplot(map) + geom_sf(aes(fill = SID74)) + theme_bw()

map <- st_as_sf(map)
ggplot(map) + geom_sf(aes(fill = SID74)) +
  scale_fill_viridis() + theme_bw()

# png("plot.png")
ggplot(map) + 
  geom_sf(aes(fill = SID74)) +
  scale_fill_viridis() + 
  theme_bw()

# dev.off()

2.4.2 leaflet

sf::st_crs(map)
## Coordinate Reference System:
##   User input: NAD27 
##   wkt:
## GEOGCRS["NAD27",
##     DATUM["North American Datum 1927",
##         ELLIPSOID["Clarke 1866",6378206.4,294.978698213898,
##             LENGTHUNIT["metre",1]]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["latitude",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["longitude",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     ID["EPSG",4267]]
pal <- colorNumeric("YlOrRd", domain = map$SID74)

leaflet::leaflet(map) %>%
  addTiles() %>%
  addPolygons(
    color = "white", fillColor = ~ pal(SID74),
    fillOpacity = 1
  ) %>%
  addLegend(pal = pal, values = ~SID74, opacity = 1)
## Warning: sf layer has inconsistent datum (+proj=longlat +datum=NAD27 +no_defs).
## Need '+proj=longlat +datum=WGS84'

2.4.3 mapview

mapview::mapview(map, zcol = "SID74")
pal <- colorRampPalette(RColorBrewer::brewer.pal(9, "YlOrRd"))
mapview::mapview(map,
  zcol = "SID74",
  map.types = "CartoDB.DarkMatter",
  col.regions = pal
)
m74 <- mapview(map, zcol = "SID74")
m79 <- mapview(map, zcol = "SID79")
m <- leafsync::sync(m74, m79) #(not from mapview)
m

2.4.4 tmap

Vignette

tmap::tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(map) + tm_polygons("SID74")