ggplot2: Functions and Resources
Table of contents
While the ggplot2 syntax may seem scary to you at first, you will learn to appreciate its logic once you get a feel for what is what.
ggplot2 Functions
The ggplot2 package employs two major functions to draw graphs:
qplot()
- “quick plot”, used to easily produce simple plots for data exploration- uses similar syntax to base R methods; see Quick R by DataCamp for a brief overview, with examples
- usage:
qplot(x, y, data=, color=, shape=, size=, alpha=, geom=, method=, formula=, facets=, xlim=, ylim= xlab=, ylab=, main=, sub=)
ggplot()
- a more flexible and robust function for building a plot layer by layer- uses explicit Grammar for Graphics syntax
- different layers are typically arranged on different rows for clarity
The basic format for a ggplot2 graph is Plot = data + aesthetics + geometry (other layers are optional):
ggplot(data = <data.frame>, # data (a data frame)
mapping = aes(x= ,y= , col= )) + # aesthetics
# x,y are columns of the data frame
geom_<type of geometry>() + # geometry
stat() + # statistics (optional)
theme() # theme (optional)
To get a feel for the similarities and differences between base R plotting, qplot()
, and ggplot()
syntax, see the numerous examples in Chapter 2 of the excellent R Graphics Cookbook by Winston Chang.
We will go through lots of examples in the course.
Extensions
Many add-ons to ggplot have been developed, some of which are listed below. We will learn more about a few of these during the course.
Some useful packages
- ggpubr - publication-ready plots
- contains the incredibly useful
ggarrange()
function for arranging and annotating multiple plots in a single figure - also see cowplot, patchwork, grid, and gridExtra
- contains the incredibly useful
- ggsignif, ggstatsplot - display statistical significance and other quantities on your plots
- ggdistribute - overlay information about a distribution’s intervals on unimodal distributions
- ggcorrplot - chart correlation matrices
- ggdendro - flexible dendrogram manipulation
- ggridges - ridgeline plots (helpful for showing changes in distributions over time)
- ggthemes - extended themes to control the look and feel of graphs, including colorblind palette
- ggradar - radar / spider charts
- gggenomes - a grammar of graphics for comparative genomics
- ggmap - spatial data and models
- ggrepel - greater control over how text looks in plots (and keep text labels away from each other)
- GGally - reduce the complexity of combining geometric objects with transformed data
- includes ggpairs for fancy plot matrices
- interactive graphics
- gganimate - animate ggplot2 visualizations
- ggiraph - interactive charts with html and javascript integration
- Plotly - graphing library for interactive and dynamic plots
Some lists of extensions
- ggplot Extensions gallery - start here!
- opensource: top 46 - a list of 46 ggplot extension open source projects on GitHub
- Mode blog: 12 Extensions to ggplot2 for More Powerful R Visualizations
- A seemingly random gallery of ggplot extensions
Resources
Cheatsheets
- RStudio: ggplot cheatsheet (PDF)
- comprehensive guide to syntax
- qplot RGraphics Cheatsheet - by David Gerard (2019-01-22)
- simple examples of different kinds of plots
DataCamp: Interactive Online Courses
Both of these are freely available through the XDASI DataCamp for Education course site.
Texts and Tutorials
- R Programming for Research: Chapter 3.7 - a concise introduction to ggplot2
- by Winston Chang
- Cookbook for R: Graphs - examples of different graph types (with code)
- R Graphics Cookbook - a comprehensive guide for making all kinds of graphs with ggplot2
- STHDA
- ggplot Essentials - covers all the basics of plotting techniques for ggplot2, with examples (unfortunately it also has a lot of ads, but it’s still pretty useful!)
- Basic plots - qplot, boxplots, violin plots, dot plots, strip charts, density plots, histograms, scatter plots, bar plots, line plots, error bars, pie chars, qqplots, ECDF plots, saving plots
- Graphical parameters - Main title, axis labels; legend title; legend position and appearance; controlling colors; point shapes, colors, and sizes; text annotations; line types; themes and background colors; axis scales and transformations; axis ticks; add straight lines to a plot; rotate, flip, and reverse plots; faceting
- Extensions to ggplot2 - R packages for extending ggplot functionality
- Easy Way to Mix Multiple Graphs on The Same Page
- examples using different packages to arrange multiple graphs on the same page
- really useful for producing publication-quality figures!
- covers
ggarange()
from the ggpubr package, cowplot, grid, and gridExtra packages
- ggplot Essentials - covers all the basics of plotting techniques for ggplot2, with examples (unfortunately it also has a lot of ads, but it’s still pretty useful!)