class: center, middle, inverse, title-slide # Maps with ggplot2 and sf ### Data Visualization for Social Good
CorrelAid Switzerland
### February 2021 --- layout: true <div class="my-footer"> <span style="text-align:center"> <span> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/by-sa.png" height=14 style="vertical-align: middle"/> </span> <a href="https://therbootcamp.github.io/"> <span style="padding-left:82px"> <font color="#7E7E7E"> https://correlaid.org/correlaid-x/switzerland/ </font> </span> </a> <a href="https://correlaid.org/correlaid-x/switzerland/"> <font color="#7E7E7E"> Data Visualization for Social Good | February 2021 </font> </a> </span> </div> --- .pull-left3[ # Maps with <mono>ggplot</mono>/<mono>sf</mono> <ul> <li class="m1"><span>Maps require geometric shapes stored in <high>shapefiles</high>.</span></li><br> <li class="m2"><span>The <high>simple features</high> (<mono>sf</mono>) framework makes processing and visualizing maps with the <mono>tidyverse<mono> easy.</span></li> </ul> ] .pull-right6[ <br><br> <img src="Maps_files/figure-html/unnamed-chunk-2-1.png" style="display: block; margin: auto;" /> ] --- # Shapefiles .pull-left45[ <ul> <li class="m1"><span>Geospatial vector data format for <high>geographic information system</high> (GIS) software.</span></li><br> <li class="m2"><span>Necessary files:</span></li> <ul class="level"> <li><span><mono>.shp</mono> | actual shapefile</span></li> <li><span><mono>.shx</mono> | shape index format</span></li> <li><span><mono>.dbf</mono> | attribute format</span></li> </ul><br> <li class="m3"><span>Optional files:</span></li> <ul class="level"> <li><span><mono>.prj</mono> | projection description</span></li> <li><span><mono>.cpg</mono> | code page specification</span></li> </ul> </ul> ] .pull-right45[ <p align="center"> <img src="image/files.png" height="420px"> </p> ] --- # <mono>sf</mono> ```r read_sf('1_Data/quarters') ``` ``` Simple feature collection with 21 features and 5 fields geometry type: POLYGON dimension: XY bbox: xmin: 2609000 ymin: 1263000 xmax: 2619000 ymax: 1272000 projected CRS: CH1903+ / LV95 # A tibble: 21 x 6 OBJID OBJECTID TXT ZTXT TYPE geometry <chr> <dbl> <chr> <chr> <chr> <POLYGON [m]> 1 17136 1 7 07 Bruderholz ((2612556 1264548, 2612561 1264514, 2612565 1264482, 261… 2 17139 2 6 06 Gundeldingen ((2610887 1266551, 2610896 1266546, 2610918 1266533, 261… 3 17142 3 5 05 St. Alban ((2612942 1267023, 2613000 1267019, 2613027 1267020, 261… 4 17145 4 4 04 Breite ((2613684 1266891, 2613686 1266889, 2613689 1266887, 261… 5 17148 5 8 08 Bachletten ((2610561 1266791, 2610571 1266781, 2610595 1266757, 261… 6 17151 6 2 02 Vorstädte ((2610928 1268323, 2610934 1268293, 2610947 1268297, 261… 7 17154 7 1 01 Altstadt Gr… ((2611366 1267578, 2611375 1267568, 2611376 1267569, 261… 8 17157 8 3 03 Am Ring ((2610705 1267923, 2610680 1267844, 2610647 1267740, 261… 9 17160 9 9 09 Gotthelf ((2609245 1266802, 2609236 1266801, 2609366 1267143, 260… 10 17163 10 10 10 Iselin ((2610234 1267656, 2610284 1267389, 2610234 1267403, 261… # … with 11 more rows ``` --- # <mono>geom_sf</mono> .pull-left45[ <ul> <li class="m1"><span>Since <mono>read_sf</mono> creates a <mono>tibble</tibble>, it can be plugged <high>straight into <mono>ggplot</mono></high>.</span></li><br> <li class="m2"><span>The dedicated geom <mono>geom_sf</mono> plots the geometric polygons.</span></li> </ul> <br> ```r # read shapefiles quarters_map <- read_sf('1_Data/quarters') # plot quarters quarters_map %>% ggplot() + geom_sf() ``` ] .pull-right45[ <img src="Maps_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> ] --- # <mono>geom_sf</mono> .pull-left45[ <ul> <li class="m1"><span>Since <mono>read_sf</mono> creates a <mono>tibble</tibble>, it can be plugged <high>straight into <mono>ggplot</mono></high>.</span></li><br> <li class="m2"><span>The dedicated geom <mono>geom_sf</mono> plots the geometric polygons.</span></li> </ul> <br> ```r # read shapefiles quarters_map <- read_sf('1_Data/quarters') # plot quarters quarters_map %>% ggplot() + geom_sf() + # remove background theme_void() ``` ] .pull-right45[ <img src="Maps_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> ] --- # Wrangling .pull-left45[ <ul> <li class="m1"><span>Since <mono>read_sf</mono> creates a <mono>tibble</tibble>, one can easily <high>join additional data</high>.</span></li> </ul> <br> ```r # join basel tax data quarters_map <- quarters_map %>% left_join(filter(basel, year == 2017), by = c("TYPE" = "quarter")) # plot quarters quarters_map %>% ggplot() + geom_sf() + theme_void() ``` ] .pull-right45[ <img src="Maps_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> ] --- # Add color .pull-left45[ <ul> <li class="m1"><span>Colors are set using <mono>aes()</mono> just like in a regular <mono>ggplot</mono>.</span></li> </ul> ```r # join basel tax data quarters_map <- quarters_map %>% left_join(filter(basel, year == 2017), by = c("TYPE" = "quarter")) # plot quarters quarters_map %>% ggplot() + # fill color by income geom_sf(aes(fill = income_mean)) + theme_void() ``` ] .pull-right45[ <img src="Maps_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> ] --- # Styling .pull-left45[ <ul> <li class="m1"><span>Styling can be adjusted just like in any other <mono>ggplot<mono>.</span></li> </ul> ```r # join basel tax data quarters_map <- quarters_map %>% left_join(filter(basel, year == 2017), by = c("TYPE" = "quarter")) # plot quarters quarters_map %>% ggplot() + # add white outlines geom_sf(aes(fill = income_mean), col = "white") + theme_void() ``` ] .pull-right45[ <img src="Maps_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto;" /> ] --- # Styling .pull-left45[ <ul> <li class="m1"><span>Styling can be adjusted just like in any other <mono>ggplot<mono>.</span></li> </ul> ```r # join basel tax data quarters_map <- quarters_map %>% left_join(filter(basel, year == 2017), by = c("TYPE" = "quarter")) # plot quarters quarters_map %>% ggplot() + geom_sf(aes(fill = income_mean), col = "white") + theme_void() + # change legend title scale_fill_continuous(name = 'Income') ``` ] .pull-right45[ <img src="Maps_files/figure-html/unnamed-chunk-16-1.png" style="display: block; margin: auto;" /> ] --- # Styling .pull-left45[ <ul> <li class="m1"><span>Styling can be adjusted just like in any other <mono>ggplot<mono>.</span></li> </ul> ```r # join basel tax data quarters_map <- quarters_map %>% left_join(filter(basel, year == 2017), by = c("TYPE" = "quarter")) # plot quarters quarters_map %>% ggplot() + geom_sf(aes(fill = income_mean), col = "white") + theme_void() + scale_fill_continuous(name = 'Income') + # add annotion labs(title = "Inequality in Basel", subtitle = "Average income in Basel...", caption = "ource: Open Data Basel...") ``` ] .pull-right45[ <img src="Maps_files/figure-html/unnamed-chunk-18-1.png" style="display: block; margin: auto;" /> ] --- class: middle, center <h1><a href="https://correlaidswitzerland.github.io/DataViz4Good/">Schedule</a></h1>