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
Create a new R Markdown document. Knit the document.
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
- 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
- Insert an
ggplot2
image that we created last time in the document.
Here’s the data from last week:
- Libraries points
- Community Area polygons
Remember: you can click on the “API” button to directly access the data, rather than having to download a csv via “Export”.
Challenge
- Add code chunks that:
- load packages
- read data
- create a ggplot2 plot
Challenge
- 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)
- Try naming your chunks something useful.
Challenge
- Try making a map with
tmap
(similar syntax asggplot2
), using thetmap
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.
5.5 Links
All the links in this workshop:
- Link to Chicago Libraries data: https://data.cityofchicago.org/Parks-Recreation/Waterways/eg9f-z3t6
- Link to Chicago Community Areas data: https://data.cityofchicago.org/Facilities-Geographic-Boundaries/Boundaries-Community-Areas-current-/cauq-8yn6
- Link to R Markdown site: https://rmarkdown.rstudio.com
- Link to Software Carpentry R Markdown tutorial: https://swcarpentry.github.io/r-novice-gapminder/15-knitr-markdown/
- Getting started with
tmap
vignette: https://cran.r-project.org/web/packages/tmap/vignettes/tmap-getstarted.html - Geocomputation with R - good chapter on making maps with
tmap
: https://geocompr.robinlovelace.net/adv-map.html