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)
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)
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)
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")
tm_
: functions to make mapstmap_
: interact with the whole maplibrary(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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.