Session 12
PMAP 8921: Data Visualization with R
Andrew Young School of Policy Studies
Summer 2022
Maps and truth
Maps and truth
Putting data on maps
Maps and truth
Putting data on maps
GIS in R with sf
10% of the population of Soho died in a week (!!)
Miasma theory said it was because the air was bad
Bottom map doesn't even show crime rates—it's the results from the 2012 presidential election
https://me.me/i/electoral-map-2016-election-crime-rate-milo-democrats-are-as-3983235
Cryptic command to convert gif to mp4:
ffmpeg -r 30 -i input.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" out.mp4
Disable my video so OBS is happy
Most common ones listed on the
course website example page
This is an excellent overview of how this all works
And this is a really really helpful overview of all these moving parts
None of them
None of them
There are no good or bad projections
None of them
There are no good or bad projections
There are appropriate and
inappropriate projections
None of them
There are no good or bad projections
There are appropriate and
inappropriate projections
(but also ew mercator)
Geographic information is shared as shapefiles
Geographic information is shared as shapefiles
These are not like regular single CSV files!
Geographic information is shared as shapefiles
These are not like regular single CSV files!
Shapefiles come as zipped files with
a bunch of different files inside
library(sf)world_shapes <- read_sf("data/ne_110m_admin_0_countries/ne_110m_admin_0_countries.shp")
## Simple feature collection with 7 features and 3 fields## Geometry type: MULTIPOLYGON## Dimension: XY## Bounding box: xmin: -180 ymin: -18 xmax: 180 ymax: 83## Geodetic CRS: WGS 84## # A tibble: 7 × 4## TYPE GEOUNIT ISO_A3 geometry## <chr> <chr> <chr> <MULTIPOLYGON [°]>## 1 Sovereign country Fiji FJI (((180 -16, 180 -17, 179 -17, 179 -17…## 2 Sovereign country Tanzania TZA (((34 -0.95, 34 -1.1, 38 -3.1, 38 -3.…## 3 Indeterminate Western Sahara ESH (((-8.7 28, -8.7 28, -8.7 27, -8.7 26…## 4 Sovereign country Canada CAN (((-123 49, -123 49, -125 50, -126 50…## 5 Country United States of America USA (((-123 49, -120 49, -117 49, -116 49…## 6 Sovereign country Kazakhstan KAZ (((87 49, 87 49, 86 48, 86 47, 85 47,…## 7 Sovereign country Uzbekistan UZB (((56 41, 56 45, 59 46, 59 46, 60 45,…
Natural Earth for international maps
US Census Bureau for US maps
For anything else…
Natural Earth for international maps
US Census Bureau for US maps
For anything else…
1:10m = 1:10,000,000
1 cm = 100 km
1:50m = 1:50,000,000
1cm = 500 km
1:110m = 1:110,000,000
1 cm = 1,100 km
1:10m = 1:10,000,000
1 cm = 100 km
1:50m = 1:50,000,000
1cm = 500 km
1:110m = 1:110,000,000
1 cm = 1,100 km
Using too high of a resolution
makes your maps slow and huge
geometry
columnAs long as you have a magic geometry
column,
all you need to do to plot maps is geom_sf()
ggplot() + geom_sf(data = world_shapes)
geometry
columnUse coord_sf()
to change projections
ggplot() + geom_sf(data = world_shapes) + coord_sf(crs = "+proj=merc")
geometry
columnUse coord_sf()
to change projections
ggplot() + geom_sf(data = world_shapes) + coord_sf(crs = "+proj=robin")
All regular ggplot layers and aesthetics work
ggplot() + geom_sf(data = world_shapes, aes(fill = POP_EST), color = "white", size = 0.15) + coord_sf(crs = "+proj=robin") + scale_fill_gradient(labels = scales::comma) + labs(fill = NULL) + theme_void() + theme(legend.position = "bottom")
geometry
column?Make your own with st_as_sf()
other_data
## # A tibble: 2 × 3## city long lat## <chr> <dbl> <dbl>## 1 Atlanta -84.4 33.8## 2 Washington, DC -77.1 38.9
geometry
column?Make your own with st_as_sf()
other_data
## # A tibble: 2 × 3## city long lat## <chr> <dbl> <dbl>## 1 Atlanta -84.4 33.8## 2 Washington, DC -77.1 38.9
other_data %>% st_as_sf(coords = c("long", "lat"), crs = st_crs("EPSG:4326"))
## Simple feature collection with 2 features and 1 field## Geometry type: POINT## Dimension: XY## Bounding box: xmin: -84 ymin: 34 xmax: -77 ymax: 39## Geodetic CRS: WGS 84## # A tibble: 2 × 2## city geometry## * <chr> <POINT [°]>## 1 Atlanta (-84 34)## 2 Washington, DC (-77 39)
Draw maps
Draw maps
Calculate distances between points
Draw maps
Calculate distances between points
Count observations in a given area
Draw maps
Calculate distances between points
Count observations in a given area
Anything else related to geography!
geom_sf()
is today’s standardYou'll sometimes find older tutorials and StackOverflow answers about using geom_map()
or ggmap or other things
geom_sf()
is today’s standardYou'll sometimes find older tutorials and StackOverflow answers about using geom_map()
or ggmap or other things
Those still work, but they don't use the same magical sf system with easy-to-convert projections and other GIS stuff
geom_sf()
is today’s standardYou'll sometimes find older tutorials and StackOverflow answers about using geom_map()
or ggmap or other things
Those still work, but they don't use the same magical sf system with easy-to-convert projections and other GIS stuff
Stick with sf and geom_sf()
and your life will be easy
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
o | Tile View: Overview of Slides |
Esc | Back to slideshow |
Session 12
PMAP 8921: Data Visualization with R
Andrew Young School of Policy Studies
Summer 2022
Maps and truth
Maps and truth
Putting data on maps
Maps and truth
Putting data on maps
GIS in R with sf
10% of the population of Soho died in a week (!!)
Miasma theory said it was because the air was bad
Bottom map doesn't even show crime rates—it's the results from the 2012 presidential election
https://me.me/i/electoral-map-2016-election-crime-rate-milo-democrats-are-as-3983235
Cryptic command to convert gif to mp4:
ffmpeg -r 30 -i input.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" out.mp4
Disable my video so OBS is happy
Most common ones listed on the
course website example page
This is an excellent overview of how this all works
And this is a really really helpful overview of all these moving parts
None of them
None of them
There are no good or bad projections
None of them
There are no good or bad projections
There are appropriate and
inappropriate projections
None of them
There are no good or bad projections
There are appropriate and
inappropriate projections
(but also ew mercator)
Geographic information is shared as shapefiles
Geographic information is shared as shapefiles
These are not like regular single CSV files!
Geographic information is shared as shapefiles
These are not like regular single CSV files!
Shapefiles come as zipped files with
a bunch of different files inside
library(sf)world_shapes <- read_sf("data/ne_110m_admin_0_countries/ne_110m_admin_0_countries.shp")
## Simple feature collection with 7 features and 3 fields## Geometry type: MULTIPOLYGON## Dimension: XY## Bounding box: xmin: -180 ymin: -18 xmax: 180 ymax: 83## Geodetic CRS: WGS 84## # A tibble: 7 × 4## TYPE GEOUNIT ISO_A3 geometry## <chr> <chr> <chr> <MULTIPOLYGON [°]>## 1 Sovereign country Fiji FJI (((180 -16, 180 -17, 179 -17, 179 -17…## 2 Sovereign country Tanzania TZA (((34 -0.95, 34 -1.1, 38 -3.1, 38 -3.…## 3 Indeterminate Western Sahara ESH (((-8.7 28, -8.7 28, -8.7 27, -8.7 26…## 4 Sovereign country Canada CAN (((-123 49, -123 49, -125 50, -126 50…## 5 Country United States of America USA (((-123 49, -120 49, -117 49, -116 49…## 6 Sovereign country Kazakhstan KAZ (((87 49, 87 49, 86 48, 86 47, 85 47,…## 7 Sovereign country Uzbekistan UZB (((56 41, 56 45, 59 46, 59 46, 60 45,…
Natural Earth for international maps
US Census Bureau for US maps
For anything else…
Natural Earth for international maps
US Census Bureau for US maps
For anything else…
1:10m = 1:10,000,000
1 cm = 100 km
1:50m = 1:50,000,000
1cm = 500 km
1:110m = 1:110,000,000
1 cm = 1,100 km
1:10m = 1:10,000,000
1 cm = 100 km
1:50m = 1:50,000,000
1cm = 500 km
1:110m = 1:110,000,000
1 cm = 1,100 km
Using too high of a resolution
makes your maps slow and huge
geometry
columnAs long as you have a magic geometry
column,
all you need to do to plot maps is geom_sf()
ggplot() + geom_sf(data = world_shapes)
geometry
columnUse coord_sf()
to change projections
ggplot() + geom_sf(data = world_shapes) + coord_sf(crs = "+proj=merc")
geometry
columnUse coord_sf()
to change projections
ggplot() + geom_sf(data = world_shapes) + coord_sf(crs = "+proj=robin")
All regular ggplot layers and aesthetics work
ggplot() + geom_sf(data = world_shapes, aes(fill = POP_EST), color = "white", size = 0.15) + coord_sf(crs = "+proj=robin") + scale_fill_gradient(labels = scales::comma) + labs(fill = NULL) + theme_void() + theme(legend.position = "bottom")
geometry
column?Make your own with st_as_sf()
other_data
## # A tibble: 2 × 3## city long lat## <chr> <dbl> <dbl>## 1 Atlanta -84.4 33.8## 2 Washington, DC -77.1 38.9
geometry
column?Make your own with st_as_sf()
other_data
## # A tibble: 2 × 3## city long lat## <chr> <dbl> <dbl>## 1 Atlanta -84.4 33.8## 2 Washington, DC -77.1 38.9
other_data %>% st_as_sf(coords = c("long", "lat"), crs = st_crs("EPSG:4326"))
## Simple feature collection with 2 features and 1 field## Geometry type: POINT## Dimension: XY## Bounding box: xmin: -84 ymin: 34 xmax: -77 ymax: 39## Geodetic CRS: WGS 84## # A tibble: 2 × 2## city geometry## * <chr> <POINT [°]>## 1 Atlanta (-84 34)## 2 Washington, DC (-77 39)
Draw maps
Draw maps
Calculate distances between points
Draw maps
Calculate distances between points
Count observations in a given area
Draw maps
Calculate distances between points
Count observations in a given area
Anything else related to geography!
geom_sf()
is today’s standardYou'll sometimes find older tutorials and StackOverflow answers about using geom_map()
or ggmap or other things
geom_sf()
is today’s standardYou'll sometimes find older tutorials and StackOverflow answers about using geom_map()
or ggmap or other things
Those still work, but they don't use the same magical sf system with easy-to-convert projections and other GIS stuff
geom_sf()
is today’s standardYou'll sometimes find older tutorials and StackOverflow answers about using geom_map()
or ggmap or other things
Those still work, but they don't use the same magical sf system with easy-to-convert projections and other GIS stuff
Stick with sf and geom_sf()
and your life will be easy