+ - 0:00:00
Notes for current slide
Notes for next slide

Space

Session 12

PMAP 8921: Data Visualization with R
Andrew Young School of Policy Studies
Summer 2022

1 / 43

Plan for today

2 / 43

Plan for today

Maps and truth

2 / 43

Plan for today

Maps and truth

Putting data on maps

2 / 43

Plan for today

Maps and truth

Putting data on maps

GIS in R with sf

2 / 43

Maps and truth

3 / 43

John Snow and 1854 cholera epidemic

John Snow
This Jo(h)n Snow knows things
4 / 43

John Snow and 1854 cholera epidemic

John Snow
This Jo(h)n Snow knows things

10% of the population of Soho died in a week (!!)

Miasma theory said it was because the air was bad

4 / 43
5 / 43

The Broad Street pump

John Snow's 1854 cholera map
The Broad Street Pump
6 / 43

Outright lies

Fake map
7 / 43

Outright lies

Fake map
Fake map again
7 / 43

Points can be useless

xkcd population maps
9 / 43

Choropleths can be great

10 / 43

Choropleths can distort

White House framed map of election results
11 / 43

Land doesn't vote

12 / 43

https://demcastusa.com/2019/11/11/land-doesnt-vote-people-do-this-electoral-map-tells-the-real-story/

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

Cartograms

 

2016 map by county
FiveThirtyEight hex cartogram
2016 cartogram by county
13 / 43
14 / 43
xkcd 2016 election map
15 / 43

Projections

Animated world projections

16 / 43

Disable my video so OBS is happy

World projections

17 / 43

US projections

18 / 43

Finding projection codes

spatialreference.org

epsg.io

proj.org

19 / 43

Finding projection codes

spatialreference.org

epsg.io

proj.org

Most common ones listed on the
course website example page

19 / 43

Finding projection codes

spatialreference.org

epsg.io

proj.org

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

19 / 43

Which projection is best?

20 / 43

Which projection is best?

None of them

20 / 43

Which projection is best?

None of them

There are no good or bad projections

20 / 43

Which projection is best?

None of them

There are no good or bad projections

There are appropriate and
inappropriate projections

20 / 43

Which projection is best?

None of them

There are no good or bad projections

There are appropriate and
inappropriate projections

(but also ew mercator)

20 / 43

Putting data on maps

21 / 43

Maps with lines

23 / 43
24 / 43

Maps with shapes

NYT: States Where Insured Could Renew Plans Before Change by Obama
29 / 43

Small multiples that look like maps

geofacet package
facet_geo() in the geofacet package
30 / 43

GIS in R with sf

31 / 43

Shapefiles

Geographic information is shared as shapefiles

32 / 43

Shapefiles

Geographic information is shared as shapefiles

These are not like regular single CSV files!

32 / 43

Shapefiles

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

Shapefile folder structure
32 / 43

Structure of a shapefile

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,…
33 / 43

Where to find shapefiles

34 / 43

Where to find shapefiles

Natural Earth for international maps

34 / 43

Where to find shapefiles

Natural Earth for international maps

US Census Bureau for US maps

34 / 43

Where to find shapefiles

Natural Earth for international maps

US Census Bureau for US maps

For anything else…

34 / 43

Where to find shapefiles

Natural Earth for international maps

US Census Bureau for US maps

For anything else…

Search for shapefiles
34 / 43

Scales

10m scale

1:10m = 1:10,000,000

1 cm = 100 km

50m scale

1:50m = 1:50,000,000

1cm = 500 km

110m scale

1:110m = 1:110,000,000

1 cm = 1,100 km

35 / 43

Scales

10m scale

1:10m = 1:10,000,000

1 cm = 100 km

50m scale

1:50m = 1:50,000,000

1cm = 500 km

110m scale

1:110m = 1:110,000,000

1 cm = 1,100 km

 

Using too high of a resolution
makes your maps slow and huge

35 / 43

Latitude and longitude

36 / 43

The magic geometry column

As 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)

37 / 43

The magic geometry column

Use coord_sf() to change projections

ggplot() +
geom_sf(data = world_shapes) +
coord_sf(crs = "+proj=merc")

38 / 43

The magic geometry column

Use coord_sf() to change projections

ggplot() +
geom_sf(data = world_shapes) +
coord_sf(crs = "+proj=robin")

39 / 43

Use aesthetics like normal

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")

40 / 43

No 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
41 / 43

No 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)
41 / 43

sf is for all GIS stuff

42 / 43

sf is for all GIS stuff

Draw maps

42 / 43

sf is for all GIS stuff

Draw maps

Calculate distances between points

42 / 43

sf is for all GIS stuff

Draw maps

Calculate distances between points

Count observations in a given area

42 / 43

sf is for all GIS stuff

Draw maps

Calculate distances between points

Count observations in a given area

Anything else related to geography!

42 / 43

sf is for all GIS stuff

Draw maps

Calculate distances between points

Count observations in a given area

Anything else related to geography!

See here or here for full textbooks

42 / 43

geom_sf() is today’s standard

You'll sometimes find older tutorials and StackOverflow answers about using geom_map() or ggmap or other things

43 / 43

geom_sf() is today’s standard

You'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

43 / 43

geom_sf() is today’s standard

You'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

43 / 43

Plan for today

2 / 43
Paused

Help

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
oTile View: Overview of Slides
Esc Back to slideshow