R Markdown and Problem Set 3

R Markdown files contain

  • A YAML header (yet-another-markup-language), offset by —-
  • Text with markdown formatting
  • Chunks of R code, offset by ``` (keyboard shortcut: Cmd/Ctrl + Alt + I)

Good Viz Examples

  • Your examples
  • Kieran Healy’s, The Polarization of Death
  • Your visualizations
    • First cut presented in class next week, to get feedback
    • Should be an R markdown file using code folding (so we can see the code, but aren’t distracted by it)
    • With at least three visualizations
    • And prose explaining your goal, your data and variables, and your intent with selected visuals

Annotating Figures

Sometimes we want to annotate the figure directly

  • Highlight something important
  • Add context
  • An alternative to the legend

annotate()!

  • not a geom – doesn’t accept mappings (things that vary)
  • can use geoms – text, point, segment, etc.

Maps

Projections and CRS

Projection: “The challenge in map-making is that we need to take the spherical surface of the earth and flatten it out so we can display it on a map.”

EPSG (European Petroleum Survey Group) and ESRI (Environmental Systems Research Institute) maintain registries of projections. Several sites provide convenient access to registered projections, including http://spatialreference.org/ and https://epsg.io/.

Coordinates can only be placed on the Earth’s surface when their coordinate reference system (CRS) is known. The CRS includes both the projection and extent of the map. ggplot will use the coordinate system of the data

  • We can use coord_sf to change the CRS when plotting
  • Or use st_transform to convert data frames to a different CRS

Choropleths

Mapping sequential colors to quantities to show variance across locations produces a choropleht map.

Simple Features

SF represents simple features as records in a data.frame or tibble with a geometry list-column. To learn more: r-spatial.github.io/sf

The simple features are in the way geometry is represented. The geometry types include:

Some notes on installation

Joins

Joins merge data sets based on key variables. The syntax is always name_join(x, y, by = "key")

Animated visuals created by Garrick Aden-Buie

  • full_join(): keeps all observations in x and y

  • left_join(): keeps all observations in x

Bonus Material: Leaflet

There’s a good chance we won’t get to this, but Leaflet is one of the most popular open-source JavaScript libraries for interactive maps. RStudio has created a package to integrate and generate Leaflet maps in R.

Today’s script has an example meant to walk you though a few key bits.

XKCD Inspiration

XKCD, Randall Munroe, https://xkcd.com/1138/