Graphics with ggplot2


Date
Event
College of Forestry Workshop

This two-hour workshop focuses on introducing students to the ggplot2 package in R for making graphics.

The first half of the workshop focuses on learning the syntax and terminology used in ggplot2 while making a series of simple, exploratory plots with a built in dataset. The goal of this section is to expose students to many different kinds of graphs as well as cover some of the common pitfalls folks run into when first using ggplot2.

In the second half of the workshop we focus on controlling the overall appearance of a graphic when making “publication-ready” plots. We’ll go through the code to create two fairly complicated graphics, building each one layer by layer so students can see how the plot changes with each addition. Students will also see some data manipulation to reshape a dataset prior to plotting, which is often necessary when plotting data with ggplot2.

I provide an R script that we’ll run code from during the workshop as well as a PDF document. The PDF is a written version of the workshop, including code and output, to be used as a reference. Make sure you save the R script as a file ending in .R and not as a text document. You may need to set it to “All Files” and manually add .R when saving.

Interactive tutorials

In addition to the materials above, I have converted the workshop material into three interactive tutorials that can be run on your own computer. To use this you will need to have package learnr installed (install.packages("learnr")).

Click on a tutorial listed below and save the file as a .Rmd when the download window opens. Open this saved Rmd file with RStudio and click the Run Document button at the top to start the tutorial.

Tutorial 1: ggplot2 basics
Tutorial 2: controlling plot appearance
Tutorial 3: controlling plot appearance with a legend

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.