R can create almost any plot imaginable and as with most things in R if you don’t know where to start, try Google. The Introduction to R curriculum summarizes some of the most used plots, but cannot begin to expose people to the breadth of plot options that exist.There are existing resources that are great references for plotting in R:
In base R:
Breakdown of how to create a plot from R-bloggers
Another blog breaking down basic plotting from FlowingData
Basic plots (histograms, boxplots, scatter plots, QQ plots) from University of Georgia
Intermediate plots (error bars, density plots, bar charts, multiple windows, saving to a file, etc) from University of Georgia
In ggplot2:
ggplot2 homepage
ggplot2 video tutorial
Website with everything you want to know about ggplot2 by Selva Prabhakaran
R graphics cookbook site
ggplot2 cheatsheet
ggplot2 reference guide
In the Introduction to R class, we have switched to teaching ggplot2 because it works nicely with other tidyverse packages (dplyr, tidyr), and can create interesting and powerful graphics with little code. While ggplot2 has many useful features, this blog post will explore how to create figures with multiple ggplot2 plots.
You may have already heard of ways to put multiple R plots into a single figure - specifying mfrow or mfcol arguments to par, split.screen, and layout are all ways to do this. However, there are other methods to do this that are optimized for ggplot2 plots.
Multiple plots in one figure using ggplot2 and facets
When you are creating multiple plots and they share axes, you should consider using facet functions from ggplot2 (facet_grid, facet_wrap). You write your ggplot2 code as if you were putting all of the data onto one plot, and then you use one of the faceting functions to specify how to slice up the graph.
Let’s start by considering a set of graphs with a common x axis. You have a data.frame with four columns: Date, site_no, parameter, and value. You want three different plots in the same figure - a timeseries for each of the parameters with different colored symbols for the different sites. Sounds like a lot, but facets can make this very simple. First, setup your ggplot code as if you aren’t faceting.
We will download USGS water data for use in this example from the USGS National Water Information System (NWIS) using the dataRetrieval package (you can learn more about dataRetrieval in this curriculum). Three USGS gage sites in Wisconsin were chosen because they have data for all three water quality parameters (flow, total suspended solids, and inorganic nitrogen) we are using in this example.
library(dataRetrieval)
library(dplyr) # for `rename` &amp; `select`
library(tidyr) # for `gather`
library(ggplot2)
# Get the data by giving site numbers and parameter codes
# 00060 = stream flow, 00530 = total suspended solids, 00631 = concentration of inorganic nitrogen wi_daily_wq % gather(key = &quot;parameter&quot;, value = &quot;value&quot;, -site_no, -Date)
# Setup plot without facets
p