Example 1: Syracuse Data

library(tmap)
## Warning: package 'tmap' was built under R version 4.0.5
library(sf)
## Warning: package 'sf' was built under R version 4.0.5
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
NY8 = st_read("NY_data/NY8_utm18.shp")
## Reading layer `NY8_utm18' from data source `C:\Users\ctlan\Documents\School\Summer21\GEOG5680\Module15\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", ]
plot(Syracuse[, "POP8"])

tm_shape(Syracuse) + tm_borders()

tm_shape(Syracuse) + tm_borders() + tm_fill("POP8")

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

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

Interactive maps

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 = "Greens")
tmap_mode("plot")
## tmap mode set to plotting

Example 2: WNA Site Data

wna_climate = read.csv("WNAclimate.csv")
str(wna_climate)
## 'data.frame':    2012 obs. of  8 variables:
##  $ WNASEQ  : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ LONDD   : num  -108 -105 -103 -110 -114 ...
##  $ LATDD   : num  50.9 55.3 41.7 44.3 59.2 ...
##  $ ELEV    : int  686 369 1163 2362 880 900 1100 1480 651 725 ...
##  $ totsnopt: num  78.8 145.4 42.7 255.1 164.9 ...
##  $ annp    : int  326 499 450 489 412 451 492 606 443 455 ...
##  $ Jan_Tmp : num  -13.9 -21.3 -4.2 -10.9 -23.9 -17.5 -18.8 -11 -22.4 -23.5 ...
##  $ Jul_Tmp : num  18.8 16.8 23.3 14.1 14.4 13.8 13.2 12.8 15.3 15.4 ...
wna_climate <- st_as_sf(wna_climate, coords = c("LONDD", "LATDD"),crs = 4326)
tm_shape(wna_climate) + tm_symbols(col="Jan_Tmp")
## 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.

tm_shape(wna_climate) + 
  tm_symbols(col="Jan_Tmp", alpha = 0.5, palette = "-RdBu")
## 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.

Load Natural Earth Shapefile

countries = st_read("ne_50m_admin_0_countries/ne_50m_admin_0_countries.shp")
## Reading layer `ne_50m_admin_0_countries' from data source `C:\Users\ctlan\Documents\School\Summer21\GEOG5680\Module15\ne_50m_admin_0_countries\ne_50m_admin_0_countries.shp' using driver `ESRI Shapefile'
## Simple feature collection with 241 features and 94 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -89.99893 xmax: 180 ymax: 83.59961
## Geodetic CRS:  WGS 84
tm_shape(wna_climate) + 
  tm_symbols(col="Jan_Tmp", alpha = 0.75, palette = "-RdBu") + 
  tm_shape(countries) + tm_borders(col="gray")
## 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.

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.

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

library(raster)
## Warning: package 'raster' was built under R version 4.0.5
## Loading required package: sp
## Warning: package 'sp' was built under R version 4.0.5
r = raster("air.mon.ltm.nc", varname="air")
## Loading required namespace: ncdf4
r = rotate(r)
names(r)
## [1] "Monthly.Long.Term.Mean.Air.Temperature.at.sigma.level.0.995"
names(r) = "jan_tmp"
tm_shape(r) + 
  tm_raster("jan_tmp", style="fisher", palette="-RdBu") +
  tm_shape(countries) + tm_borders() 
## Warning: The projection of the shape object r is not known, while it seems to be
## projected.
## Warning: Current projection of shape r unknown and cannot be determined.
## 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.

Fix error in projection

crs(r) = 4326
tm_shape(r) + 
  tm_raster("jan_tmp", palette="RdBu") +
  tm_shape(countries) + tm_borders() 
## Warning: The projection of the shape object r is not known, while it seems to be
## projected.
## Warning: Current projection of shape r unknown and cannot be determined.
## 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.

Fix map issues

tm_shape(r) + 
  tm_raster("jan_tmp", 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: The projection of the shape object r is not known, while it seems to be
## projected.
## Warning: Current projection of shape r unknown and cannot be determined.
## 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.

tm_shape(r) + 
  tm_raster("jan_tmp", 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: The projection of the shape object r is not known, while it seems to be
## projected.
## Warning: Current projection of shape r unknown and cannot be determined.
## 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.

## [1] "#134B86" "#3581B9" "#82BBD8" "#CAE1EE" "#F7F7F7" "#FDDBC7" "#F4A582"
## [8] "#D6604D" "#B2182B"