class: left, bottom, inverse, title-slide # GIS and mapping ## A workshop on GIS and mapping with R: part 2 ### Jakub Nowosad & Robin Lovelace --- class: inverse, left, bottom # Mapping --- # Mapping ```r library(sf) ``` ``` ## Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.2.1 ``` ```r library(terra) ``` ``` ## terra version 1.3.1 ``` ```r library(tmap) library(spData) library(spDataLarge) ``` --- # Mapping ```r nz_elev = rast(system.file("raster/nz_elev.tif", package = "spDataLarge")) ``` ```r plot(nz_elev) ``` <img src="workshop1_jn_files/figure-html/unnamed-chunk-3-1.png" style="display: block; margin: auto;" /> --- # Mapping ```r data("nz", package = "spData") plot(nz) ``` <img src="workshop1_jn_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> --- # Mapping ```r data("nz_height", package = "spData") plot(nz_height) ``` <img src="workshop1_jn_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> --- # Mapping .pull-left[ ```r tm_shape(nz_elev) + tm_graticules() + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_shape(nz) + tm_borders(col = "black", lwd = 1) + tm_shape(nz_height) + tm_symbols(size = 0.2, col = "red") + tm_add_legend(type = "symbol", col = "red", title = "Highest peaks") + tm_scale_bar(breaks = c(0, 100, 200), text.size = 1) + tm_compass(position = c("right", "top"), type = "rose", size = 2) + tm_credits(text = "J. Nowosad, 2021") + tm_layout(main.title = "New Zealand", bg.color = "lightblue", inner.margins = c(0, 0, 0, 0)) ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm1-1.png" width="672" style="display: block; margin: auto;" /> ] --- class: inverse, left, bottom # Shapes and layers --- # Shapes and layers .pull-left[ There is also `tm_rgb()`. ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm1a-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Shapes and layers .pull-left[ ```r *tm_shape(nz_elev) + tm_graticules() + * tm_raster(title = "Elevation (m asl)", * style = "cont", * palette = "-Spectral") + * tm_shape(nz) + * tm_borders(col = "black", * lwd = 1) + * tm_shape(nz_height) + * tm_symbols(size = 0.2, col = "red") + * tm_add_legend(type = "symbol", col = "red", * title = "Highest peaks") + tm_scale_bar(breaks = c(0, 100, 200), text.size = 1) + tm_compass(position = c("right", "top"), type = "rose", size = 2) + tm_credits(text = "J. Nowosad, 2021") + tm_layout(main.title = "New Zealand", bg.color = "lightblue", inner.margins = c(0, 0, 0, 0)) ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm2-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Shapes and layers .pull-left[ https://geocompr.github.io/post/2019/tmap-color-scales/ ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "pretty", palette = "-Spectral") ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm1b-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Shapes and layers .pull-left[ `tmaptools::palette_explorer()` ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "pretty", palette = "Reds") # minus reverse the color scale ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm1c-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Shapes and layers .pull-left[ There are also `tm_fill()` and `tm_polygons()`. ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_shape(nz) + tm_borders(col = "black", lwd = 1) ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm1d-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Shapes and layers .pull-left[ There are also `tm_fill()` and `tm_polygons()`. ```r tm_shape(nz) + tm_polygons(col = "Median_income", title = "Median income (USD)", style = "jenks", legend.hist = TRUE) ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm1e-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Shapes and layers .pull-left[ There are also `tm_dots()`, `tm_text()`, and more... ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_shape(nz) + tm_borders(col = "black", lwd = 1) + tm_shape(nz_height) + tm_symbols(size = 0.2, col = "red") ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm1f-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Shapes and layers .pull-left[ ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_shape(nz) + tm_borders(col = "black", lwd = 1) + tm_shape(nz_height) + tm_symbols(size = 0.2, col = "red") + tm_add_legend(type = "symbol", col = "red", title = "Highest peaks") ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm1g-1.png" width="672" style="display: block; margin: auto;" /> ] --- class: inverse, left, bottom # Attributes layers --- # Attributes layers .pull-left[ ```r tm_shape(nz_elev) + tm_graticules() + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_shape(nz) + tm_borders(col = "black", lwd = 1) + tm_shape(nz_height) + tm_symbols(size = 0.2, col = "red") + tm_add_legend(type = "symbol", col = "red", title = "Highest peaks") + * tm_scale_bar(breaks = c(0, 100, 200), * text.size = 1) + * tm_compass(position = c("right", "top"), * type = "rose", * size = 2) + * tm_credits(text = "J. Nowosad, 2021") + tm_layout(main.title = "New Zealand", bg.color = "lightblue", inner.margins = c(0, 0, 0, 0)) ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm3-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Attributes layers .pull-left[ ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_scale_bar(breaks = c(0, 100, 200), text.size = 1) + tm_compass(position = c("right", "top"), type = "rose", size = 2) + tm_credits(text = "J. Nowosad, 2021") ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm3a-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Attributes layers .pull-left[ ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_scale_bar(breaks = c(0, 100, 200), text.size = 1, position = c("left", "center")) + tm_compass(position = c("right", "bottom"), type = "rose", size = 2) + tm_credits(text = "J. Nowosad, 2021", position = c("right", "top")) ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm3b-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Attributes layers .pull-left[ ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_scale_bar() + tm_compass(position = c("right", "top"), type = "4star", size = 4) + tm_credits(text = "J. Nowosad, 2021") ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm3c-1.png" width="672" style="display: block; margin: auto;" /> ] --- class: inverse, left, bottom # Grids and graticules --- # Grids and graticules .pull-left[ ```r tm_shape(nz_elev) + * tm_graticules() + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_shape(nz) + tm_borders(col = "black", lwd = 1) + tm_shape(nz_height) + tm_symbols(size = 0.2, col = "red") + tm_add_legend(type = "symbol", col = "red", title = "Highest peaks") + tm_scale_bar(breaks = c(0, 100, 200), text.size = 1) + tm_compass(position = c("right", "top"), type = "rose", size = 2) + tm_credits(text = "J. Nowosad, 2021") + tm_layout(main.title = "New Zealand", bg.color = "lightblue", inner.margins = c(0, 0, 0, 0)) ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm4-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Grids and graticules .pull-left[ There is also `tm_grid()` - https://geocompr.github.io/post/2019/tmap-grid/. ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_graticules(x = seq(166, 178, 2)) ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm4a-1.png" width="672" style="display: block; margin: auto;" /> ] --- class: inverse, left, bottom # Layout --- # Layout .pull-left[ ```r tm_shape(nz_elev) + tm_graticules() + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_shape(nz) + tm_borders(col = "black", lwd = 1) + tm_shape(nz_height) + tm_symbols(size = 0.2, col = "red") + tm_add_legend(type = "symbol", col = "red", title = "Highest peaks") + tm_scale_bar(breaks = c(0, 100, 200), text.size = 1) + tm_compass(position = c("right", "top"), type = "rose", size = 2) + tm_credits(text = "J. Nowosad, 2021") + * tm_layout(main.title = "New Zealand", * bg.color = "lightblue", * inner.margins = c(0, 0, 0, 0)) ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm5-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Layout .pull-left[ ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_layout(main.title = "New Zealand", title = "My first {tmap}", bg.color = "#add8e6", legend.show = FALSE) ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm5a-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Layout .pull-left[ ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_layout(main.title = "New Zealand", title = "My first {tmap}", bg.color = "#add8e6") ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm5c-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Layout .pull-left[ ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_layout(main.title = "New Zealand", title = "My first {tmap}", bg.color = "#add8e6", legend.outside = TRUE, frame = FALSE) ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm5d-1.png" width="672" style="display: block; margin: auto;" /> ] --- # Layout .pull-left[ ```r tm_shape(nz_elev) + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_layout(main.title = "New Zealand", title = "My first {tmap}", bg.color = "#add8e6", inner.margins = c(0.01, 0.01, 0.01, 0.2)) ``` ] .pull-right[ <img src="workshop1_jn_files/figure-html/tm5e-1.png" width="672" style="display: block; margin: auto;" /> ] --- class: inverse, left, bottom # Modes --- # Modes .pull-left[ ```r tmap_mode("view") #plot or view tm_shape(nz_elev) + tm_graticules() + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_shape(nz) + tm_borders(col = "black", lwd = 1) + tm_shape(nz_height) + tm_symbols(size = 0.2, col = "red") + tm_add_legend(type = "symbol", col = "red", title = "Highest peaks") + tm_scale_bar(breaks = c(0, 100, 200), text.size = 1) + tm_compass(position = c("right", "top"), type = "rose", size = 2) + tm_credits(text = "J. Nowosad, 2021") + tm_layout(main.title = "New Zealand", bg.color = "lightblue", inner.margins = c(0, 0, 0, 0)) ``` ] .pull-right[
] --- class: inverse, left, bottom # Saving --- # Saving .pull-left[ ```r my_map = tm_shape(nz_elev) + tm_graticules() + tm_raster(title = "Elevation (m asl)", style = "cont", palette = "-Spectral") + tm_shape(nz) + tm_borders(col = "black", lwd = 1) + tm_shape(nz_height) + tm_symbols(size = 0.2, col = "red") + tm_add_legend(type = "symbol", col = "red", title = "Highest peaks") + tm_scale_bar(breaks = c(0, 100, 200), text.size = 1) + tm_compass(position = c("right", "top"), type = "rose", size = 2) + tm_credits(text = "J. Nowosad, 2021") + tm_layout(main.title = "New Zealand", bg.color = "lightblue", inner.margins = c(0, 0, 0, 0)) ``` ] -- .pull-right[ ```r tmap_mode("plot") my_map ``` <img src="workshop1_jn_files/figure-html/unnamed-chunk-27-1.png" style="display: block; margin: auto;" /> ] --- # Saving ```r tmap_save(my_map, filename = "my_map.png", width = 300, height = 800, dpi = 300) ``` --- class: inverse, left, bottom # Exercises --- # Exercises ```r 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") ``` 1. Run the code above. Create a simple map of the reprojected data for contiguous United States (object `us_states2163`) with the graticule lines. 2. Improve the map of the reprojected data for contiguous United States by coloring states based on their median income in 2015 (`median_income_15`). Improve the legend title. Try different color palettes. Change the width of the borders. Put the legend on the right side of the map. 3. Try different [color scale styles](https://geocompr.github.io/post/2019/tmap-color-scales/). Which one is the best in your opinion? 4. Improve the map of the reprojected data for contiguous United States (object `us_states2163`) further by adding a map title, scale bar, north arrow, and text annotation. Try to improve locations of the map elements. 5. Change the tmap mode to "view" and recreate maps from Exercise 4. What are the pros and cons of using static or interactive maps? What are the map's elements that can exist in static maps, but are not rendered in the interactive maps? 6. Save the map from Exercise 4 as a .png, .svg, and .html file. When can different file formats be useful?