options(scipen = 999)
library(tidyverse)
Why should you map in R?
A few basics:
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.
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()
.
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.
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.
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")
.
knitr::include_graphics("../www/images/sf.png")
Artwork by @allison_horst
Before sf
, life was harder
sp
objects that were hard to work withggplot2()
fits the same framework as plottingsf
is magical