This workshop focuses on
ggplot2 for making plots in R. The workshop also briefly covers additional packages.
This workshop also makes use of other
tidyverse packages for data manipulation and transformation, but you do not need to know these packages for the exercises.
Software and Files
For this workshop, you'll need to install some R packages and download the workshop files from this repository. See below.
Slides are available to download separately at the links below; click on the link (which goes to Box), then choose the download option if you want. You may be able to view the Powerpoint version directly in Box. If you don't have these programs, try uploading to use Google Slides to view them.
- Keynote: this is the original; ~250MB
- Powerpoint: much smaller, some alignment/fonts might be slightly off from the conversion
Whether using R on your laptop or in the cloud, there are several packages you'll need to install:
Install these with the R command
install.packages(c("tidyverse", "babynames"), repos="http://cran.rstudio.com")
or use the buttons in the Packages window in RStudio to install them.
If you previously installed
ggplot2, please update
ggplot2 to the current version (3.2). You can use the Update button the Packages tab in RStudio to update the package.
Option 1: On your laptop
R and RStudio
This workshop assumes you have recent versions of R and RStudio - R 3.5 or later and RStudio 1.1. Install or update packages listed above.
To download workshop materials, click on the green Clone or Download button, then download the repository as a ZIP file.
Find the downloaded .zip file on your computer, likely in your Downloads folder. Unzip it - usually by double-clicking. This will create a directory called r_ggplot2_july2018-master. Move this somewhere on your computer where you'll be able to find it, like your Documents folder.
Double click on the .Rproj folder in the folder to launch the project in RStudio. Alternatively, open RStudio, then go to File
Alternatively, if you're familiar with git, you can create a new RStudio project directly from this repository (again, once the materials are posted).
Option 2: RStudio Cloud
RStudio Cloud is a way to run RStudio in your web browser. Set up an account. In Your Workspace, create a new project: Click on the down arrow next to New Project in the blue button, then select New Project from a Git Repository. Provide the address of this repository: https://github.com/nuitrcs/r_ggplot_july2018. This will copy all of the files from this repository into your new project.
You should wait until the morning of the workshop to do this, to ensure you have the most current version of the workshop files.*
You will also need to install packages in this project space.
For more practice after the workshop, see the exercises below.
Some of these we written with a version of
ggplot2 < 3.0.0, so a few things may be different.
R for Data Science: Chapter 3 covers visualization with
ggplot2 and includes exercises. Chapter 28 includes more advanced topics, also with exercises. Individuals have posted their (non-vetted) solutions online (i.e. Jeffrey Arnold). There is also an online learning group, organized through Slack, that is working through this book: details.
R Exercises: a set of plots, with the challenge to make them. Includes answers.
Harvard IQSS Tutorial: includes a section "Putting It All Together" with a challenge of recreating a graph from The Economist, with the answer code included (not hidden). This is a little about the plotting, but mostly about styling.
Computing for the Social Sciences Course Exercises: from instructor Benjamin Soltoff, exercises using the
gapminder data, with solutions.
UVA BIMS8382 Course Exercises: more exercises from a course, with solutions. Part exericse, part walkthrough. You can see the plots you're trying to make.
See additional resources and links to some visualization examples in the main R workshop repository.
ggplot2 Cheat Sheet: you'll want to have this for reference
R Graph Gallery for
ggplot2 (it has base R graphics as well). This site has code for each plot, so find what you're looking for, and modify the code. from Data to Viz has charts to help you choose a plot type (linking to the graph gallery), plus a list of data visualization choices to avoid.
ggplot2 Data Visualization Vocabulary from Jesse Maegan: confused by unfamiliar terms in the
ggplot2 help and functions? You can find explanations of many of them here.
ggplot2 Extensions: list of packages that extend
ggplot2 using the same logic, with links to them on GitHub
ggplot2 Extensions described: another list of extensions with blurbs - these extensions are generally included on the list above too
A Layered Grammar of Graphics: The paper by Hadley Wickham that introduced
ggplot2; explains the grammar of graphics approach.
ggplot2 Aesthetics Vignette: part of the package documentation, how the different aesthetics elements work.
ggplot2 Tutorial from Sean Anderson, University of Washington
ggplot2 Tutorial From the Harvard IQSS Data Science Services group.
Getting started with data visualization in R using
ggplot2 from Martin Frigaard at Storybench
ggplot2 Tutorial from Selva Prabhakaran with examples of many different kinds of plots
An introduction to ggplot2 by Melanie Frazier, shows lots of color/style options along with a good general overview of ggplot
Software Carpentry: R for Reproducible Scientific Analysis: course material for introductory R workshop, includes a section on
Swirl: Interactive tutorials that you run in R. The Exploratory Data Analysis course includes sections on
ggplot2 Visualizations - Example visualizations with code, from Selva Prabhakaran; also has some tutorials on the site.
R for Data Science: free online book using the
tidyverse of packages, has a chapter on data visualization. It includes a chapter on Graphics for communication which doesn't teach
ggplot2 so much as give you good principles of making charts with it.
Data Visualization for Social Science: by Kieran Healy, an online book that uses
Base R Graphics
A comparison on Base graphics and
ggplot2 from Flowing Data, which is a great site in general for R graphics, but he doesn't use
Stack Overflow: great for searching for issues; most questions you have will have already been asked by someone else; be aware of the date of answers, as sometimes an answer may be a little out of date.
Colors, Styles, and Formats
colorblindr: package to help you check what your plots might look like to someone who is colorblind
ColorBrewer: color palettes, intended for maps, but useful for other applications; accessible through
ggplot2 Colors: examples of how to change different color options