GIS_workshop2_GIS_Mapping_Nowosad.Rmd
Slides: https://nowosad.github.io/SIGR2021/workshop1/workshop1_jn.html#1
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.3.0, PROJ 8.0.1
library(terra)
#> terra version 1.3.4
library(tmap)
library(spData)
library(spDataLarge)
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)
tm_raster
: elevation, pluviometry, etc.tm_rgb
: satellite imagery with 3 layers dataset (visible, false colors)
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)
#> Warning in showSRID(SRS_string, format = "PROJ", multiline = "NO", prefer_proj
#> = prefer_proj): Discarded datum Unknown_based_on_GRS80_ellipsoid in Proj4
#> definition
#> stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)
library(sf)
nz_4326 = st_transform(nz, 4326)
tm_shape(nz_4326) +
tm_polygons(text.size = 1, breaks = c(0,20))
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
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)
#> tmap mode set to interactive viewing
#> Credits not supported in view mode.
#> Compass not supported in view mode.
#> Warning in showSRID(SRS_string, format = "PROJ", multiline = "NO", prefer_proj
#> = prefer_proj): Discarded datum Unknown_based_on_GRS80_ellipsoid in Proj4
#> definition
#> stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)
#> Warning: In view mode, scale bar breaks are ignored.
#> Symbol shapes other than circles or icons are not supported in view mode.
#> only legends of type "fill" supported in view mode
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
#> Credits not supported in view mode.
#> Compass not supported in view mode.
#> Warning in showSRID(SRS_string, format = "PROJ", multiline = "NO", prefer_proj
#> = prefer_proj): Discarded datum Unknown_based_on_GRS80_ellipsoid in Proj4
#> definition
#> stars object downsampled to 877 by 1140 cells. See tm_shape manual (argument raster.downsample)
#> Warning: In view mode, scale bar breaks are ignored.
#> Symbol shapes other than circles or icons are not supported in view mode.
#> only legends of type "fill" supported in view mode
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")
tm_
: functions to make mapstmap_
: interact with the whole map
library(sf);library(tmap);library(spData);library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:terra':
#>
#> intersect, src, union
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
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