In my book on modern data analyisis using R, I show some basics of geoplotting. It seems that some software update for the package simple features broke my code. So, here ’s some update.
Load packages and data
library(tidyverse)
library(viridis)
library(sf)
data(socec, package = "pradadata")
data(wahlkreise_shp, package = "pradadata")
Check data
glimpse(socec)
#> Observations: 316
#> Variables: 51
#> $ V01 <chr> "Schleswig-Holstein", "Schleswig-Holstein", "Schleswig-Holst…
#> $ V02 <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 901, 12, 13, 14, 15, 16, …
#> $ V03 <chr> "Flensburg – Schleswig", "Nordfriesland – Dithmarschen Nord"…
#> $ V04 <int> 130, 197, 178, 163, 3, 92, 49, 95, 49, 126, 28, 1110, 132, 1…
#> $ V05 <dbl> 2128.1, 2777.0, 2000.5, 2164.8, 143.0, 1302.0, 664.3, 1333.4…
#> $ V06 <dbl> 282.8, 232.3, 220.8, 248.7, 268.0, 219.8, 307.5, 313.2, 219.…
#> $ V07 <dbl> 266.7, 219.7, 209.8, 239.4, 242.9, 206.8, 281.5, 294.9, 209.…
#> $ V08 <dbl> 5.7, 5.4, 5.0, 3.7, 9.4, 5.9, 8.4, 5.8, 4.3, 6.6, 8.0, 6.3, …
#> $ V09 <dbl> 132.9, 83.6, 110.4, 114.9, 1873.8, 168.8, 462.9, 234.9, 143.…
#> $ V10 <dbl> -3.7, -5.0, -5.3, -3.4, -0.1, -4.9, -2.3, -1.6, -6.5, -3.7, …
#> $ V11 <dbl> 12.3, 13.2, 11.9, 10.0, 11.9, 21.0, 13.1, 9.2, 13.2, 16.8, 1…
#> $ V12 <dbl> 16.5, 16.1, 16.7, 17.3, 14.6, 16.4, 16.8, 17.1, 15.0, 16.8, …
#> $ V13 <dbl> 8.7, 8.0, 7.6, 7.2, 10.8, 7.3, 7.2, 6.9, 6.7, 6.6, 8.2, 7.7,…
#> $ V14 <dbl> 11.3, 10.4, 9.9, 9.4, 17.4, 10.1, 10.7, 10.5, 9.1, 10.1, 13.…
#> $ V15 <dbl> 35.0, 35.1, 37.0, 37.2, 32.8, 35.8, 37.4, 37.9, 36.1, 36.9, …
#> $ V16 <dbl> 17.4, 18.5, 17.4, 17.9, 14.8, 18.3, 16.7, 17.0, 20.0, 17.3, …
#> $ V17 <dbl> 11.2, 11.9, 11.4, 11.0, 9.6, 12.1, 11.2, 10.5, 13.1, 12.3, 1…
#> $ V18 <dbl> 90.0, 92.0, 90.8, 91.5, 82.0, 89.2, 85.0, 86.7, 91.6, 87.3, …
#> $ V19 <dbl> 10.0, 8.0, 9.2, 8.5, 18.0, 10.8, 15.0, 13.3, 8.4, 12.7, 16.5…
#> $ V20 <dbl> 5.1, 5.2, 4.2, 4.9, 7.3, 5.6, 6.4, 5.8, 6.1, 6.8, 8.1, 6.0, …
#> $ V21 <dbl> 59.5, 65.0, 59.9, 58.3, 41.8, 54.1, 40.9, 44.1, 57.6, 44.1, …
#> $ V22 <dbl> 35.5, 29.8, 35.9, 36.8, 50.9, 40.3, 52.7, 50.1, 36.3, 49.0, …
#> $ V23 <dbl> 49.4, 58.8, 59.4, 58.7, 29.5, 53.0, 52.8, 54.6, 55.9, 55.6, …
#> $ V24 <dbl> 3.8, 6.0, 2.4, 5.5, 1.3, 2.4, 6.0, 3.1, 4.7, 2.8, 2.7, 3.7, …
#> $ V25 <dbl> 514.6, 566.1, 493.0, 492.2, 539.3, 501.9, 484.6, 469.6, 551.…
#> $ V26 <int> 20265, 22159, 20896, 22093, 18637, 20395, 23766, 22791, 2196…
#> $ V27 <int> 28300, 30078, 29298, 26659, 42836, 25983, 26342, 29135, 2335…
#> $ V28 <dbl> 696.9, 748.1, 724.5, 733.3, 518.2, 673.6, 651.9, 712.1, 719.…
#> $ V29 <chr> "4,4", "4", "2,7", "2,4", "4,8", "4", "2,1", "2", "3", "2", …
#> $ V30 <dbl> 11.9, 11.5, 11.7, 9.4, 8.7, 11.6, 11.0, 10.4, 10.0, 9.2, 9.2…
#> $ V31 <dbl> 8.2, 7.9, 7.8, 7.4, 6.6, 7.9, 6.0, 6.2, 9.8, 6.8, 9.5, 7.5, …
#> $ V32 <dbl> 19.8, 19.9, 19.1, 18.2, 15.2, 18.5, 14.8, 16.7, 19.4, 16.3, …
#> $ V33 <dbl> 41.6, 48.8, 44.1, 43.6, 34.5, 39.0, 40.1, 41.2, 41.3, 39.1, …
#> $ V34 <dbl> 30.4, 23.4, 29.0, 30.9, 43.6, 34.6, 39.1, 35.9, 29.4, 37.9, …
#> $ V35 <dbl> 41.1, 33.9, 35.0, 37.1, 42.1, 37.5, 40.2, 47.5, 33.4, 43.1, …
#> $ V36 <dbl> 44.0, 60.9, 43.8, 41.6, 37.9, 41.6, 48.5, 48.3, 51.1, 45.9, …
#> $ V37 <dbl> 7.3, 9.2, 7.1, 6.9, 4.2, 6.7, 7.2, 7.3, 7.3, 6.8, 5.5, 6.9, …
#> $ V38 <dbl> 337.4, 337.6, 290.2, 276.1, 473.7, 312.4, 290.2, 333.4, 300.…
#> $ V39 <chr> "1,6", "2,6", "2,7", "2,4", "0,2", "1,7", "2,3", "1,1", "1,5…
#> $ V40 <chr> "18,7", "19,9", "28,3", "24,4", "16", "22,5", "29,5", "27,8"…
#> $ V41 <chr> "28,1", "33,4", "22,9", "23", "20,1", "29,1", "29", "29,3", …
#> $ V42 <chr> "16,4", "12,4", "15,9", "15,5", "25,2", "17,6", "17,2", "17,…
#> $ V43 <dbl> 35.2, 31.7, 30.3, 34.7, 38.6, 29.1, 22.0, 24.3, 33.4, 26.5, …
#> $ V44 <dbl> 88.5, 70.9, 80.1, 59.4, 138.7, 86.8, 67.7, 54.5, 62.6, 59.5,…
#> $ V45 <dbl> 26.3, 26.2, 26.0, 28.1, 26.4, 26.2, 28.8, 28.5, 24.9, 27.8, …
#> $ V46 <dbl> 18.8, 16.7, 21.1, 23.8, 28.3, 22.1, 34.6, 28.3, 20.2, 28.7, …
#> $ V47 <dbl> 7.2, 7.2, 6.6, 5.1, 8.8, 6.9, 5.2, 4.6, 6.2, 4.8, 8.7, 6.4, …
#> $ V48 <dbl> 8.2, 7.7, 7.2, 5.5, 10.0, 7.5, 5.6, 4.9, 6.6, 5.1, 9.4, 6.9,…
#> $ V49 <dbl> 6.2, 6.6, 6.0, 4.7, 7.6, 6.4, 4.8, 4.2, 5.7, 4.5, 8.0, 5.8, …
#> $ V50 <dbl> 4.2, 3.6, 5.2, 3.2, 6.1, 4.4, 3.3, 2.7, 3.7, 3.2, 5.7, 4.0, …
#> $ V51 <dbl> 7.3, 8.4, 6.6, 5.6, 8.7, 7.7, 6.4, 5.1, 7.4, 5.5, 9.0, 6.9, …
glimpse(wahlkreise_shp)
#> Observations: 299
#> Variables: 5
#> $ WKR_NR <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,…
#> $ LAND_NR <fct> 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 01, 13, 13, 13…
#> $ LAND_NAME <fct> Schleswig-Holstein, Schleswig-Holstein, Schleswig-Hols…
#> $ WKR_NAME <fct> Flensburg – Schleswig, Nordfriesland – Dithmarschen No…
#> $ geometry <MULTIPOLYGON [m]> MULTIPOLYGON (((543474.9 60..., MULTIPOLY…
Let’s pick only a few columns to make life easier:
socec_short <- socec %>% # Spalten wählen und umbenennen
select(WKR_NAME = V03,
WKR_NR = V02,
wegzug = V11, # Wegzugssalde
migration = V19, # Migrationsanteil
auslaender = V08, # Auslaenderanteil
alq = V47, # Arbeitslosenquote
migration = V19) # Anteil mit Migrationshintergrund
Join datasets
There are two data sets: One for the socioeconomic indicators, one for the maps. Let’s join them.
socec_shape <- socec_short %>% # Plotten (Arbeitslosigkeit)
left_join(wahlkreise_shp)
This step is not strictly needed (I did not do it the book), but it may save some typing.
Now plot it:
socec_shape %>%
ggplot +
aes(fill = alq) +
theme_void() +
geom_sf(aes(geometry = geometry))
socec_shape %>%
ggplot +
aes(fill = migration) +
scale_fill_viridis() +
theme_void() +
geom_sf(aes(geometry = geometry))
What went wrong in my book?
geom_sf()
needs to be updated togeom_sf(aes(geometry = geometry))
. Apparently, the data column with the geo data is not found automatically anymore. I’m unsure about the causes.- Typo on page 219: after
theme_void()
there was a+
missing
sessionInfo
sessionInfo()
#> R version 3.6.0 (2019-04-26)
#> Platform: x86_64-apple-darwin15.6.0 (64-bit)
#> Running under: macOS Mojave 10.14.5
#>
#> Matrix products: default
#> BLAS: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
#> LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
#>
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] viridis_0.5.1 viridisLite_0.3.0 sf_0.7-6
#> [4] forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3
#> [7] purrr_0.3.2 readr_1.3.1 tidyr_0.8.3
#> [10] tibble_2.1.3 ggplot2_3.2.0 tidyverse_1.2.1
#>
#> loaded via a namespace (and not attached):
#> [1] tidyselect_0.2.5 xfun_0.8 haven_2.1.1
#> [4] lattice_0.20-38 colorspace_1.4-1 generics_0.0.2
#> [7] vctrs_0.2.0 htmltools_0.3.6 yaml_2.2.0
#> [10] rlang_0.4.0 e1071_1.7-2 pillar_1.4.2
#> [13] glue_1.3.1.9000 withr_2.1.2 DBI_1.0.0
#> [16] modelr_0.1.4 readxl_1.3.1 munsell_0.5.0
#> [19] blogdown_0.14 gtable_0.3.0 cellranger_1.1.0
#> [22] rvest_0.3.4 codetools_0.2-16 evaluate_0.14
#> [25] labeling_0.3 knitr_1.23 class_7.3-15
#> [28] broom_0.5.2 Rcpp_1.0.1 KernSmooth_2.23-15
#> [31] classInt_0.3-3 scales_1.0.0 backports_1.1.4
#> [34] jsonlite_1.6 gridExtra_2.3 hms_0.5.0
#> [37] digest_0.6.20 stringi_1.4.3 bookdown_0.12
#> [40] grid_3.6.0 cli_1.1.0 tools_3.6.0
#> [43] magrittr_1.5 lazyeval_0.2.2 crayon_1.3.4
#> [46] pkgconfig_2.0.2 zeallot_0.1.0 xml2_1.2.0
#> [49] lubridate_1.7.4 assertthat_0.2.1 rmarkdown_1.14
#> [52] httr_1.4.0 rstudioapi_0.10 R6_2.4.0
#> [55] units_0.6-3 nlme_3.1-140 compiler_3.6.0