Chapter 5 R Markdown and Custom Maps

5.1 Learning Objectives

  • Create a R Markdown document
  • Embed code and text in an R Markdown document
  • Manipulate R Markdown chunk options
  • Become familiar with the tmap package

5.2 Topics Learned

  • .rmd structure
  • Code chunks in R Markdown

Note: If you do not have LaTeX installed on your computer, please install tinytex using the following commands so that you will be able to Knit to a PDF and Beamer during this workshop.

install.packages(‘tinytex’) tinytex::install_tinytex()

Hint: Use the R Markdown guides in RStudio under Help > Cheatsheets > R Markdown Cheatsheet (or R Markdown Reference Guide) for help.

5.3 Interactive Tutorial

This workshop’s R Markdown can be found here.

5.4 Challenges

We’re going to continue using the data we used last week from the Chicago Data Portal, but we’ll be working in a R Markdown document instead of a R script.

Challenge

  1. Create a new R Markdown document. Knit the document.

  2. Change the YAML header at the top to include your personal information. Try changing some of the options in the header (aka the options between the “—”):

  • output: pdf_document
  • fontsize: 14pt

You can use this reference guide to help you out.

Challenge

  1. Use Markdown formatting to write some information about the Chicago libraries and community areas datasets. Use:
  • sections
  • bold/italic text
  • two types of lists

Challenge

  1. Insert an ggplot2 image that we created last time in the document.

Here’s the data from last week:

Remember: you can click on the “API” button to directly access the data, rather than having to download a csv via “Export”.

Challenge

  1. Add code chunks that:
  • load packages
  • read data
  • create a ggplot2 plot

Challenge

  1. Add chunk options that:
  • load packages (hide this code)
  • read data (show this code, and run it)
  • create a ggplot2 plot (don’t show this code, but run it, and control the size of the figure)
  1. Try naming your chunks something useful.

Challenge

  1. Try making a map with tmap (similar syntax as ggplot2), using the tmap vignette.

Luc Anselin has written a more in-depth tutorial for tmap, which you can find and go through here, under Introduction to Spatial Data Science > Basic Mapping.