Exercise 1: Package Installation

Introduction to Geospatial Techniques for Social Scientists in R

Author

Stefan Jünger, Anne Stroppe & Dennis Abel

Exercises

This is the first exercise in this workshop, and it is relatively easy and short. Its purpose is to get used to this exercise format and, more importantly, to install all necessary packages for this course.

And here comes our very first exercise (which is not very spatial…):

Note🏋 Exercise 1

Please install all required packages for the course. This is the only exercise where we actively encourage you to directly scroll down to the end of this document and copy+paste the solution.

We often recommend using the easypackages package, which can be installed with the command install.packages("easypackages"). After loading the package with library(easypackages) you can load and install packages with the command easypackages::packages("fancy_package_1", "fancy_package_2", ...).

For packages hosted on github, you can use install.packages(pak).

That’s it. Please notify us if there are any issues with installing the packages or, more generally, your R installation.

…oh, and while you are done and the others are still struggling with package installation:

Note🏋 Exercise 2

Visit the website https://thetruesize.com/ and check out how the (projected) sizes of countries can change depending on their location.

Solutions

library(easypackages)

# Geospatial core
easypackages::packages(
  "sf",             # Simple Features: vector data
  "terra",          # Raster data (modern replacement for raster)
  "tmap",           # Thematic maps
  "tmaptools",      # Helper tools for tmap
  "ggspatial",      # Spatial layers for ggplot2 (scale bar, north arrow)
  "tidyterra",      # tidyverse integration for terra
  "osmdata",        # Download OpenStreetMap data
  "OpenStreetMap",  # Render OSM tiles as background maps
  "tidygeocoder",   # Geocoding via public APIs
  "exactextractr"   # Fast raster extraction for polygons
)

# Spatial statistics
easypackages::packages(
  "spdep",          # Spatial dependence (weights, autocorrelation tests)
  "sfdep",          # tidyverse-compatible interface to spdep
  "spatialreg",     # Spatial regression models
  "spatstat"        # Spatial point pattern analysis
)

# Data manipulation
easypackages::packages(
  "dplyr",          # Data wrangling
  "tidyr",          # Data tidying
  "tibble",         # Modern data frames
  "readr",          # Fast data import
  "purrr",          # Functional programming
  "magrittr",       # Pipe operators
  "glue",           # String interpolation
  "stringr",        # String manipulation
  "units"           # Physical units for numeric vectors
)

# Visualization
easypackages::packages(
  "ggplot2",        # Grammar of graphics
  "cowplot",        # Plot composition and alignment
  "colorBlindness", # Color-blind-friendly palettes
  "gganimate",      # Animated ggplot2 maps and plots
  "leaflet",        # Interactive web maps
  "mapview",        # Quick interactive spatial data viewing
  "patchwork"       # Combine multiple ggplot2 plots
)


# --- Non-CRAN Packages --------------------------------------------------------
# These packages are hosted on GitHub or GESIS GitLab.
# Install 'pak' first if not already available.

if (!requireNamespace("pak", quietly = TRUE)) install.packages("pak")

pak::pkg_install(c(
  "StefanJuenger/bkggeocoder",                          # Offline geocoding (BKG), developed at GESIS
  "StefanJuenger/z11",                                  # German Census 2011 grid data
  "jslth/z22",                                          # German Census 2022 grid data
  "jslth/ffm",                                          # BKG administrative boundaries archive
  "git::https://git.gesis.org/sora-service/sora.git"    # GESIS survey data access 
))