knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Slides: https://nowosad.github.io/SIGR2021/workshop1/workshop1_jn.html#1

library(sf)
library(terra)
library(tmap)
library(spData)
library(spDataLarge)

Mapping

nz_elev = terra::rast(system.file("raster/nz_elev.tif", package = "spDataLarge"))
plot(nz_elev)

Tmap functions starts with tm_ prefix

tm_borders : outline empty polygons tm_legends : create manual legends tm_layout : overall look of the map (margin, frame, title, background color)

Shapes and layers

raster data

tm_shape(nz_elev) + # data what we want to plot
  tm_graticules() + # add graticules, lines in degrees (not working ?)
  #tm_grid() + # grid for projected data
  tm_raster( # we want to plot the shape as a raster
    title = "elevation as asl", # change legend title
    style = "cont", # discretization (échelle continue, sans classes)
    palette = "-Spectral" # palette spectal inversée
    #palette = c("green","blue", "yellow", "red") # palette custom
            ) +
  tm_shape(nz) + # New Zealand vector data
  tm_borders(col = "black", # black borders with empty polygons see tm_fill() and tm_polygons()
             lwd = 1 ) +
  tm_shape(nz_height) +
  tm_symbols(size = 0.2, col = 'red', shape = 17) + # chnage point size and colors
  tm_add_legend(type = "symbol", col = "red", shape = 17, title = "Highest peaks") +
  tm_scale_bar(breaks = c(0,100,200)) +
  tm_compass(position = c("right", "top"),
             type = "radar",
             size = 2) +
  tm_credits(text = "N. Roelandt, 2021") +
  tm_layout(main.title = "New Zealand topography",
            bg.color = "lightblue", # background color #add8e6
            inner.margins = c(0.01,0.01,0.01,0.2), # bottom, left, top, right
            legend.outside = TRUE,
            frame = FALSE)
library(sf)

nz_4326 = st_transform(nz, 4326)

tm_shape(nz_4326) +
  tm_polygons(text.size = 1, breaks = c(0,20))

Explore color palettes in {tmap}

tmaptools::palette_explorer()# needs shiny installed
tm_shape(nz) +
  tm_polygons(col = "Median_income",
              title = "Median income (USD)",
              style = "jenks",
              n = 20,
              legend.hist = TRUE) +
  tm_layout(inner.margins = c(0,0.02,0,0.5)) # ajust margins
tm_shape(nz) +
  tm_polygons(col = "Median_income",
              title = "Median income (USD)",
              style = "jenks",
              n = 20,
              legend.hist = TRUE,
              legend.format = list(digits =1 )) + # mise en forme de la legend
  tm_layout(legend.outside = TRUE) # move legend outside the map

Modes

Static map / interactive map

tmap_mode("view") +
tm_shape(nz_elev) + # data what we want to plot
  tm_graticules() + # add graticules, lines in degrees (not working ?)
  #tm_grid() + # grid for projected data
  tm_raster( # we want to plot the shape as a raster
    title = "elevation as asl", # change legend title
    style = "cont", # discretization (échelle continue, sans classes)
    palette = "-Spectral" # palette spectal inversée
    #palette = c("green","blue", "yellow", "red") # palette custom
            ) +
  tm_shape(nz) + # New Zealand vector data
  tm_borders(col = "black", # black borders with empty polygons see tm_fill() and tm_polygons()
             lwd = 1 ) +
  tm_shape(nz_height) +
  tm_symbols(size = 0.2, col = 'red', shape = 17) + # chnage point size and colors
  tm_add_legend(type = "symbol", col = "red", shape = 17, title = "Highest peaks") +
  tm_scale_bar(breaks = c(0,100,200)) +
  tm_compass(position = c("right", "top"),
             type = "radar",
             size = 2) +
  tm_credits(text = "N. Roelandt, 2021") +
  tm_layout(main.title = "New Zealand topography",
            bg.color = "lightblue", # background color #add8e6
            inner.margins = c(0.01,0.01,0.01,0.2), # bottom, left, top, right
            legend.outside = TRUE,
            frame = FALSE)

Saving

my_map = tm_shape(nz_elev) + # data what we want to plot
  tm_graticules() + # add graticules, lines in degrees (not working ?)
  #tm_grid() + # grid for projected data
  tm_raster( # we want to plot the shape as a raster
    title = "elevation as asl", # change legend title
    style = "cont", # discretization (échelle continue, sans classes)
    palette = "-Spectral" # palette spectal inversée
    #palette = c("green","blue", "yellow", "red") # palette custom
            ) +
  tm_shape(nz) + # New Zealand vector data
  tm_borders(col = "black", # black borders with empty polygons see tm_fill() and tm_polygons()
             lwd = 1 ) +
  tm_shape(nz_height) +
  tm_symbols(size = 0.2, col = 'red', shape = 17) + # change point size and colors
  tm_add_legend(type = "symbol", col = "red", shape = 17, title = "Highest peaks") +
  tm_scale_bar(breaks = c(0,100,200)) +
  tm_compass(position = c("right", "top"),
             type = "radar",
             size = 2) +
  tm_credits(text = "N. Roelandt, 2021") +
  tm_layout(main.title = "New Zealand topography",
            bg.color = "lightblue", # background color #add8e6
            inner.margins = c(0.01,0.01,0.01,0.2), # bottom, left, top, right
            legend.outside = TRUE,
            frame = FALSE)

my_map

tmap::tmap_mode("view")
my_map
tmap::tmap_mode("plot")
my_map
tmap_save(my_map, filename = "vignettes/images/nz.png", width = 300, height = 800, dpi = 300)
tmap_save(my_map, filename = "vignettes/images/nz.svg")
tmap_save(my_map, filename = "vignettes/images/nz.html")

Exercices

library(sf);library(tmap);library(spData);library(dplyr)
us_states = left_join(us_states, us_states_df, by = c("NAME" = "state"))
us_states2163 = st_transform(us_states, "EPSG:2163")
us_map = tm_shape(us_states2163) + # data what we want to plot
  tm_grid() +
  tm_borders(col = "black", lwd =1)
us_map
us_map = us_map +
  tm_fill(col = "median_income_15",
          title = "2015 median income (in US$)",
          palette = )

us_map


Bakaniko/notesSIGR2021 documentation built on Dec. 17, 2021, 10:45 a.m.