tmap package

tmap is used for making nice maps that layer in ways similar to ggplot2. tmaps use spatial objects for the data, geometries and symbols are then layered on top.

The first example is looking at data for Syracuse, NY.

library(tmap)
## The legacy packages maptools, rgdal, and rgeos, underpinning this package
## will retire shortly. Please refer to R-spatial evolution reports on
## https://r-spatial.org/r/2023/05/15/evolution4.html for details.
## This package is now running under evolution status 0
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
NY8 <- st_read("NY_data/NY_data/NY8_utm18.shp")
## Reading layer `NY8_utm18' from data source 
##   `C:\Users\sherl\Documents\Geog 6680 - R\Mod15\NY_data\NY_data\NY8_utm18.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 281 features and 17 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 358241.9 ymin: 4649755 xmax: 480393.1 ymax: 4808545
## Projected CRS: WGS 84 / UTM zone 18N
Syracuse <- NY8[NY8$AREANAME == "Syracuse city", ]

tm_shape(Syracuse) + tm_borders() + tm_fill("POP8", palette = "Greens", style = "quantile")

Color pallettes can be found in “tmaptools” pkg. I like that they have pallettes for color blindness. I have three sons and all are mildly colorblind with red/greens/oranges. I try to consider this when making graphs and maps. Plotting the last map with “viridis” pallette

library(tmaptools)

#palette_explorer()

tm_shape(Syracuse) + tm_borders() + tm_fill("POP8", palette = "viridis", style = "quantile")

Adding more to complete the map.

tm_shape(Syracuse) + tm_graticules(col = "lightgray") + tm_borders() + 
  tm_fill("POP8", palette = "viridis", style = "quantile") + 
  tm_compass(position = c("left", "bottom")) + 
  tm_credits("2019-10-19", position = c("right", "top"))

Interactive maps are cool!

tmap_mode("view")
## tmap mode set to interactive viewing
names(Syracuse)
##  [1] "AREANAME"   "AREAKEY"    "X"          "Y"          "POP8"      
##  [6] "TRACTCAS"   "PROPCAS"    "PCTOWNHOME" "PCTAGE65P"  "Z"         
## [11] "AVGIDIST"   "PEXPOSURE"  "Cases"      "Xm"         "Ym"        
## [16] "Xshift"     "Yshift"     "geometry"
tm_shape(Syracuse) + tm_borders() + tm_fill("Cases", palette = "viridis")
tmap_mode("plot")
## tmap mode set to plotting

Example 2: Western North America.

wna_climate <- read.csv("WNAclimate.csv")

# convert to df to sf object
wna_climate <- st_as_sf(wna_climate, 
                        coords = c("LONDD", "LATDD"),
                        crs = 4326)

# get country shapefiles
countries <- st_read("ne_50m_admin_0_countries/ne_50m_admin_0_countries/ne_50m_admin_0_countries.shp",
                     quiet = TRUE)

tm_shape(wna_climate) + 
  tm_symbols(col="Jan_Tmp", alpha = 0.75, palette = "-RdBu", size = 0.5) + 
  tm_shape(countries) + tm_borders(col="gray") + tm_style("cobalt") 
## Variable(s) "Jan_Tmp" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.

Can arrange maps in-line for easier comparison

tm1 <- tm_shape(wna_climate) + 
  tm_symbols(col="Jan_Tmp", alpha = 0.75, palette = "-RdBu", size = 0.5) + 
  tm_shape(countries) + tm_borders(col="gray") + tm_style("classic") 

tm2 <- tm_shape(wna_climate) + 
  tm_symbols(col="Jul_Tmp", alpha = 0.75, palette = "-RdBu", size = 0.5) + 
  tm_shape(countries) + tm_borders(col="gray") + tm_style("classic") 

tmap_arrange(tm1, tm2)
## Variable(s) "Jan_Tmp" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.
## Variable(s) "Jan_Tmp" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.


Example 3: global air temperature (Its RASTER time baby!)

library(raster)
## Loading required package: sp
r <- raster("air.mon.ltm.nc", varname="air")
## Loading required namespace: ncdf4
r <- rotate(r)
names(r) <- "Jan_Temp"

# fix projection to WGS84
crs(r) <- 4326
## Please note that rgdal will be retired during October 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## See https://r-spatial.org/r/2023/05/15/evolution4.html and https://github.com/r-spatial/evolution
## rgdal: version: 1.6-7, (SVN revision 1203)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.6.2, released 2023/01/02
## Path to GDAL shared files: C:/Users/sherl/AppData/Local/R/win-library/4.3/rgdal/gdal
##  GDAL does not use iconv for recoding strings.
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 9.2.0, March 1st, 2023, [PJ_VERSION: 920]
## Path to PROJ shared files: C:/Users/sherl/AppData/Local/R/win-library/4.3/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.6-1
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
# make bkgnd for legend
tm_shape(r) + 
  tm_raster("Jan_Temp", style="fisher", palette="-RdBu", n = 9, title = "January temperature") +
  tm_shape(countries) + tm_borders() + 
  tm_layout(legend.bg.color = "white", legend.bg.alpha = 0.6)
## Warning in sf::st_is_longlat(shp2): bounding box has potentially an invalid
## value range for longlat data
## Variable(s) "Jan_Temp" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.
## Warning in sf::st_is_longlat(shp): bounding box has potentially an invalid
## value range for longlat data

Also able to move legend to outside of plot area. Also added histogram of temperatures.

tm_shape(r) + 
  tm_raster("Jan_Temp", style="fisher", palette="-RdBu", legend.hist = TRUE, 
            n = 9, title = "January temperature") +
  tm_shape(countries) + tm_borders() + tm_layout(legend.outside = TRUE, 
                                                 legend.outside.position = "left")
## Warning in sf::st_is_longlat(shp2): bounding box has potentially an invalid
## value range for longlat data
## Variable(s) "Jan_Temp" contains positive and negative values, so midpoint is set to 0. Set midpoint = NA to show the full spectrum of the color palette.
## Warning in sf::st_is_longlat(shp): bounding box has potentially an invalid
## value range for longlat data