class: center, middle, inverse, title-slide # Geospatial Techniques for Social Scientists in R ## Basic Maps ### Stefan Jünger & Anne-Kathrin Stroppe
February 08, 2021 --- layout: true --- class: middle ## Check-In and Q&A .center[ Check-In: [Click here](https://www.menti.com/efi4iyigyg) ] -- Any questions remaining from the last session you need us to answer? Any questions concerning the organization of the course? --- ## Now <table class="table" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> Day </th> <th style="text-align:left;"> Time </th> <th style="text-align:left;"> Title </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;color: gray !important;"> February 08 </td> <td style="text-align:left;color: gray !important;"> 09:00am-10:30am </td> <td style="text-align:left;font-weight: bold;"> Introduction </td> </tr> <tr> <td style="text-align:left;color: gray !important;"> February 08 </td> <td style="text-align:left;color: gray !important;"> 11:00am-12:30pm </td> <td style="text-align:left;font-weight: bold;"> Vector Data </td> </tr> <tr> <td style="text-align:left;color: gray !important;color: gray !important;"> February 08 </td> <td style="text-align:left;color: gray !important;color: gray !important;"> 12:30pm-01:30pm </td> <td style="text-align:left;font-weight: bold;color: gray !important;"> Lunch Break </td> </tr> <tr> <td style="text-align:left;color: gray !important;background-color: yellow !important;"> February 08 </td> <td style="text-align:left;color: gray !important;background-color: yellow !important;"> 01:30pm-03:00pm </td> <td style="text-align:left;font-weight: bold;background-color: yellow !important;"> Basic Maps </td> </tr> <tr> <td style="text-align:left;color: gray !important;border-bottom: 1px solid"> February 08 </td> <td style="text-align:left;color: gray !important;border-bottom: 1px solid"> 03:30pm-05:00pm </td> <td style="text-align:left;font-weight: bold;border-bottom: 1px solid"> Raster Data </td> </tr> <tr> <td style="text-align:left;color: gray !important;"> February 09 </td> <td style="text-align:left;color: gray !important;"> 09:00am-10:30am </td> <td style="text-align:left;font-weight: bold;"> Advanced Data Import </td> </tr> <tr> <td style="text-align:left;color: gray !important;"> February 09 </td> <td style="text-align:left;color: gray !important;"> 11:00am-12:30pm </td> <td style="text-align:left;font-weight: bold;"> Applied Data Wrangling </td> </tr> <tr> <td style="text-align:left;color: gray !important;color: gray !important;"> February 09 </td> <td style="text-align:left;color: gray !important;color: gray !important;"> 12:30pm-13:30pm </td> <td style="text-align:left;font-weight: bold;color: gray !important;"> Lunch Break </td> </tr> <tr> <td style="text-align:left;color: gray !important;"> February 09 </td> <td style="text-align:left;color: gray !important;"> 01:30pm-03:00pm </td> <td style="text-align:left;font-weight: bold;"> Advanced Maps I </td> </tr> <tr> <td style="text-align:left;color: gray !important;"> February 09 </td> <td style="text-align:left;color: gray !important;"> 03:30pm-05:00pm </td> <td style="text-align:left;font-weight: bold;"> Advanced Maps II </td> </tr> </tbody> </table> --- ## Fun with Flags, eh, MAPS .center[ <img src="data:image/png;base64,#./img/fun_with_flags.jpg" width="90%" style="display: block; margin: auto;" /> ] <br> .center[ <small><small><small> Fun with Flags by Dr. Sheldon Cooper. [Big Bang Theory](https://youtu.be/_e8PGPrPlwA)</small></small></small> ] --- ## Fun with Maps .pull-left[ We already learned how we can make very basic maps using the `plot` function. However, this command does not allow us to manipulate the maps in any way. But we already have the two most essential ingredients to create a nice map: 1. At least one vector layer like an ESRI Shapefile. 2. Some interesting attributes linked with the geometries. ] .pull-right[ <img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/cologne-covid-1.png" style="display: block; margin: auto;" /> ] --- ## What Makes a Good Map? There is a difference between a great and a fantastic map. And there are hundreds of really, really bad maps out there. What makes the difference? .pull-left[ A Good Map: - reduction to most important information - legends, scales, descriptions - audience oriented - adjusted for color blindness ] .pull-right[ A Bad Map: - overcrowding and overlapping - unreadable information - missing information like the source - poor choice of color palettes ] --- ## What Makes a Good Map? ... but there is one other type: .center[ The fast but nice map. ] This is what we plan to do now: A fast exploration of spatial data by visualizing the geometries and attributes. They might not be publication-ready yet, but they are more rewarding than just plotting information. --- ## Sophie´s Choice: Packages for Mapping As always, *RStudio* offers several ways to map spatial data, and the provided packages are various. What is out there? - base R graphics package: `maps` and [`mapdata`](https://rpubs.com/rbatzing/mapping) - mobile-friendly interactive maps: [`leaflet`](https://rstudio.github.io/leaflet/) - interactive and static thematic maps based on shapefiles: [`tmap`](https://mran.microsoft.com/snapshot/2014-12-22/web/packages/tmap/vignettes/tmap-nutshell.html) - the all-rounder: `ggmap` in [`ggplot2`](https://www.r-spatial.org/r/2018/10/25/ggplot2-sf.html) --- ## Sophie´s Choice: Packages for Mapping Today, we'll concentrate on the package `tmap`. - Firstly, it focuses on vector data. - Secondly, it offers not only static but a quick interactive feature. - Thirdly, it is very intuitive and makes "good" decisions for us. Lastly, the syntax is very similar to `ggplot2` and will help us tomorrow when we dive deeper into the art of mapping. A wide-known secret is that `tmap` creates a map based on `ggplot2`, so `ggplot2`-specific graphics manipulations will work as well. --- ## First Map in Four Lines Quick Map of Worldwide Life Expectancy? Here we go! There are several build-in shapefiles, like *World*, which we can access by calling `data`. .pull-left[ ```r library(tmap) data("World") tm_shape(World) + tm_fill("life_exp") ``` ] .pull-right[ <img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/first-map-1.png" style="display: block; margin: auto;" /> ] --- ## `tmap` in a nutshell There is not much to consider when using `tmap`, but essential two requirements: 1. Define your spatial object. 2. Choose a building block to determine how to display information. ```r # required first step: # define every (new) shapefile tm_shape() + # required second : # choose at least one building block as 'aesthetic layer' # for polygon layer choose from: tm_fill() + # polygons without borders tm_polygons() + # polygons with borders tm_borders() + # only borders of polygons # for line layer choose: tm_lines() + # for point layer choose: tm_dots() + tm_bubbles() + # for all of them: ?'tmap-element' ``` --- ## `tmap` in a nutshell: Polygon layer .pull-left[ ```r tm_shape(World) + tm_fill() tm_shape(World) + tm_polygons() tm_shape(World) + tm_borders() ``` ] -- .pull-right[ <img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/pol-layer-1.png" style="display: block; margin: auto;" /><img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/pol-layer-2.png" style="display: block; margin: auto;" /><img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/pol-layer-3.png" style="display: block; margin: auto;" /> ] --- ## `tmap` in a nutshell: Line and Point layer .pull-left[ ```r # data set rivers data("rivers") tm_shape(rivers) + tm_lines() # metropolitan cities worldwide data("metro") tm_shape(metro) + tm_dots() ``` ] -- .pull-right[ <img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/line-layer-1.png" style="display: block; margin: auto;" /><img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/line-layer-2.png" style="display: block; margin: auto;" /> ] --- ## `tmap` in a nutshell: Put it all together We can map the geometric attributes as single layers, but we can also layer our map and stack the layers on each other. ```r tm_shape(World) + tm_polygons() + tm_shape(rivers) + tm_lines() + tm_shape(metro) + tm_dots() ``` <img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/over-lay-1.png" width="80%" style="display: block; margin: auto;" /> --- ## Add Some Information After we took care of our geometric types, we want to add some information to our data. The inner construction of each building block of `tm_elements` is the same. 1. Define the variable of interest first by stating the column name. 2. Add a name for legend title, color palette, adjust legend, scales ... .pull-left[ ```r # define variable tm_shape(World) + tm_polygons("life_exp") ``` ] -- .pull-right[ .center[ <img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/life-exp-1.png" style="display: block; margin: auto;" /> ] ] --- ## Add Some Information After we took care of our geometric types, we want to add some information to our data. The inner construction of each building block of `tm_elements` is the same. 1. Define the variable of interest first by stating the column name. 2. Add a name for legend title, color palette, adjust legend, scales ... .pull-left[ ```r # add color, title and more tm_shape(World) + tm_polygons("life_exp", palette = "RdPu", title = "Life Expectancy", style = "kmeans") ``` ] -- .pull-right[ .center[ <img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/add-attributes-1.png" style="display: block; margin: auto;" /> ] ] --- ## Change the surroundings We can change the map self, but we can also change the overall layout like title, frame, fonts, margins, legend position, and more. There is another building block organizing everything surrounding our map: `tm_layout`. .pull-left[ ```r # add names tm_shape(World) + tm_polygons("life_exp", palette = "RdPu", title = "Life Expectancy", style = "kmeans") + tm_layout(main.title = "Life Expectancy ", legend.text.color = "purple", legend.title.color = "purple", legend.bg.color = "lightgrey", legend.outside = TRUE) ``` ] .pull-right[ .center[ <img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/change-layout-1.png" style="display: block; margin: auto;" /> ] ] --- class: middle ## Exercise 1_3_1: Basic Maps [Exercise](https://stefanjuenger.github.io/gesis-workshop-geospatial-techniques-R/exercises/1_3_1_Basic_Maps_question.html) [Solution](https://stefanjuenger.github.io/gesis-workshop-geospatial-techniques-R/solutions/1_3_1_Basic_Maps_solution.html) --- ## What's left? .center[ <img src="data:image/png;base64,#./img/enough_maps.jpg" width="70%" style="display: block; margin: auto;" /> ] --- ## Explore Your Data We just can't get enough of creating maps. One simple benefit of quick maps: You can explore your data efficiently and make spatial correlations visible. In the previous months, German researchers and journalists claimed that in areas where the Right-Wing Populist Party *AfD* is supported by voters, the rate of Covid-19 infections is exceptionally high. The argument supporting this hypothesis is that *AfD* supporters have more conspiracy beliefs and are less compliant with state rules. Can we support this claim based on our data and our basic map skills? --- ## Map of AfD Support .pull-left[ ```r afd_map <- tm_shape(german_districts_enhanced) + tm_fill(col = "afd_voteshare_2017", title = " AfD Voteshare (%)", palette = "Blues") + tm_layout(main.title = "Electoral Success AfD", main.title.color = "black" ) + tm_legend(legend.outside = TRUE, legend.outside.position = "right") afd_map ``` ] .pull-right[ <img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/afd-map-1.png" style="display: block; margin: auto;" /> ] --- ## AfD Success and Covid-19 Cases If we place the Covid-19 map created in the exercise and the AfD map right next to each other, we might get a first impression. The tool we use here is named `tmap_arrange()`. ```r tmap_arrange(covid_map, afd_map) ``` -- <img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/arranged-maps-1.png" width="50%" style="display: block; margin: auto;" /> --- ## AfD Success and Covid-19 Cases We can see some kind of correlation. But as we know: Correlation should not be confused with causation. We can assume some confounding factors like the sociodemographic composition, state governments' decisions on regulations, or shared borders with other European countries. --- ## Putting on Some Layers Instead of placing maps next to each other, we can also explore spatial correlations by adding layers. Another benefit of layering: We can make our map more realistic and aesthetic. For example, we can add a background layer of neighboring countries, add a layer of German cities, or the German states' borders for orientation. Each layer can visualize another attribute of our map. For today, we want to add a layer of hospital point data to our Covid-19 map. ```r # import point layer hospital sf hospitals_sf <- read.csv("./data/hospital_points.csv", header = T, fill = T, sep = ",") %>% st_as_sf(., coords = c("X", "Y"), crs = 3035) %>% mutate(beds = as.numeric(beds)) ``` --- ## Putting on Some Layers .pull-left[ ```r # map the hospitals on the covid-19 map hospital_covid_map <- tm_shape(german_districts_enhanced) + tm_fill(col = "cases_per_100k", title = "Covid-19 Cases per 100k", palette = "RdPu") + tm_layout(main.title = "Covid Cases in Germany", main.title.color = "black" ) + tm_legend(legend.outside = TRUE, legend.outside.position = "left") + # Add hospital data tm_shape(hospitals_sf) + tm_dots("beds", col = "black", title.size = "Number of Hospital Beds") hospital_covid_map ``` ] .pull-right[ <img src="data:image/png;base64,#1_3_Basic_Maps_files/figure-html/hospital-map-1.png" style="display: block; margin: auto;" /> ] --- class: middle ## Exercise 1_3_2: Fun with Maps (& a Mapp-Off afterwards) [Exercise](https://stefanjuenger.github.io/gesis-workshop-geospatial-techniques-R/exercises/1_3_2_Fun_with_Maps_question.html) --- # A Short Note on Mapping Responsible In the best cases, maps are easy to understand and an excellent way to transport (scientific) messages. In the worst cases, they simplify (spurious) correlations and draw a dramatic picture of the world. Maps can shape narratives, and they do not only depend on the manipulation of the data. Decisions on which projection you use (remember the `true size` projector?), the segment of the world you choose, and the colors you add have a strong influence. As an example, check out this fantastic blog post on **mapping the Coronavirus responsible** by [Kenneth Field](https://www.esri.com/arcgis-blog/products/product/mapping/mapping-coronavirus-responsibly/) --- ## Wrap-Up If you want to dive deeper into mapping with `tmap` check out: - Introduction by [Thomas Lo Russo](https://tlorusso.github.io/geodata_workshop/tmap_package) - Blogpost by [Hollie Zevross](http://zevross.com/blog/2018/10/02/creating-beautiful-demographic-maps-in-r-with-the-tidycensus-and-tmap-packages/) And if you want to get some inspiration, keep an eye out for the #30DayMapChallenge on Twitter. Repository of Last Year's Challenge [here](https://github.com/tjukanovt/30DayMapChallenge). You want to know how to advance your maps? We'll hava a look at this tomorrow! --- class: middle ## Break ☕ --- layout: false class: center background-image: url(data:image/png;base64,#./img/the_end.png) background-size: cover .left-column[ </br> <img src="data:image/png;base64,#./img/anne.png" width="90%" style="display: block; margin: auto;" /> ] .right-column[ .left[.small[<svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;"> [ comment ] <path d="M464 64H48C21.49 64 0 85.49 0 112v288c0 26.51 21.49 48 48 48h416c26.51 0 48-21.49 48-48V112c0-26.51-21.49-48-48-48zm0 48v40.805c-22.422 18.259-58.168 46.651-134.587 106.49-16.841 13.247-50.201 45.072-73.413 44.701-23.208.375-56.579-31.459-73.413-44.701C106.18 199.465 70.425 171.067 48 152.805V112h416zM48 400V214.398c22.914 18.251 55.409 43.862 104.938 82.646 21.857 17.205 60.134 55.186 103.062 54.955 42.717.231 80.509-37.199 103.053-54.947 49.528-38.783 82.032-64.401 104.947-82.653V400H48z"></path></svg> [`anne-kathrin.stroppe@gesis.org`](mailto:anne-kathrin.stroppe@gesis.org)] </br> .small[<svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;"> [ comment ] <path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"></path></svg> [`@AStroppe`](https://twitter.com/AStroppe)] </br> .small[<svg viewBox="0 0 496 512" xmlns="http://www.w3.org/2000/svg" style="height:1em;fill:currentColor;position:relative;display:inline-block;top:.1em;"> [ comment ] <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg> [`stroppan`](https://github.com/stroppan)]] </br> ]