options(scipen = 999)

library(tidyverse)

Welcome!

Why should you map in R?

  • Reproducible. Writing code provides a road map for other researchers (and future you) on how your map was created and what analysis steps were taken.
  • Iteration. Want to make the same map for 10 different years? Want to make a map of all the counties in each state? This can be as simple as adding one more line of code.
  • Easy for analysis. There are huge benefits to using the same software to run your analysis and then make your map.
  • Free and open source. We are lucky and have great access to software at Urban- but outside of work, it can be expensive (financially and computationally) to use other GIS software.

Getting Started with R

A few basics:

  • What is R Studio?
  • The console and the global environment
  • The assignment operator
  • R packages
  • R projects

Getting Started with Mapping

  • Common types of maps: choropleth, dot density, heat maps…
  • Maps are 2D representations of 3D objects- there is always some distortion

Exercise 0

Step 1: Create a new directory called mapping

Step 2: Open RStudio. Click “Project: (None)” in the top right corner. Click “New Project” and create a project based on the existing mapping directory.

Step 3: Open a .R script with the button in the top left. Save the script as 01_intro-to-mapping.R.

Step 4: Submit install.packages("tidyverse") to the Console.

Step 5: Submit install.packages("remotes") to the Console.

Step 6: Submit remotes::install_github("UrbanInstitute/urbnmapr") to the Console.

Step 7: Submit remotes::install_github("UrbanInstitute/urbnthemes") to the Console.

Step 8: At the top of 01_intro-to-mapping.R write: library(tidyverse) library(urbnmapr) library(urbnthemes) library(ggplot2)

With the cursor on the line (or selecting the text you want to run), hit Control-Enter simultaneously.

ggplot2

To make maps in R, we use the ggplot2 charting library. ggplot2 is an R package that is built upon the Grammar of Graphics and creates a readable consistent syntax for creating charts and plots. All graphics in this library are built using a layered approach, building layers up to create the final graphic. The power of this, is that we use almost identical syntax to create both graphs and maps in R.

picture credits: @allisonhorst

ggplot2 syntax

  • 1 Data are the values represented in the visualization.

  • 2 Aesthetic mappings are directions for how data are mapped in a plot in a way that we can perceive. Aesthetic mappings include linking variables to the x-position, y-position, color, fill, shape, transparency, and size.

  • 3 Geometric objects are representations of the data, including points, lines, and polygons. The most common geom that we will use for mapping is geom_sf().

Exercise 1: Let’s make a map

Step 1: Type the following code into your script.

states <- get_urbn_map(map = "states", sf = TRUE)

Type View(states) into your console.

Step 2 Type the following code into your script.

ggplot() +
  geom_sf(data = states, mapping = aes())

Step 3 Change the color of the states to be Urban cyan (#1696d2) and the outline of the states to be white.

ggplot() +
  geom_sf(data = states, mapping = aes(),
          fill = "#1696d2", color = "white")

Step 4 Remove the gridlines and axis labels from the map by adding a + sign and then theme_urbn_map() to the end of the map.

Exercise 2: Mapping Household Median Income by State

Step 1: Copy and paste the following code into your script.

data <- read_csv("https://raw.githubusercontent.com/UI-Research/urbn101-mapping/day1/data/state_data.csv?token=AHJ7BDMLC7LYH4P2NM5A5EC5PZYCW")

Type View(data) into the console.

Step 2: Merge the dataset with the geographic data using left_join(). Make sure that the geographic data is on the left side of the join.

geo_data <- left_join(states, data, by = "state_name")

Step 2: Use ggplot() to map the variable medhhincome. Hint: use the same code from the previous map, but move the fill = command to inside the aes().

Step 3: Type set_urbn_defaults(style = "map") near the top of your script.

Step 4: Add the line scale_fill_gradientn(labels = scales::dollar) to the map. What changed?

Step 5: Change the outline of the states to be white.

Exercise 3: Mapping Categorical Variables

Step 1: Using the same geo_data dataframe, we will now map a categorical variable.

ggplot() +
  geom_sf(data = geo_data, mapping = aes(fill = cat_var))

Step 2: Change the colors on the map to four other Urban colors using scale_fill_manual(). (Hint: type view_palette() into the console to get hex codes for Urban colors. Use values = and a vector of hex codes).

Step 3: Change the title of your legend by adding labs(fill = "My categorical variable").

What are simple features?

knitr::include_graphics("../www/images/sf.png")

Artwork by @allison_horst

Before sf, life was harder

  • Tibbles with hundreds of thousands of rows
  • sp objects that were hard to work with

Recap

  • Mapping with ggplot2() fits the same framework as plotting
  • sf is magical

Coming next week

  • Reading and writing spatial data
  • Map projections
  • Points, lines and polygons