Link Search Menu Expand Document (external link)

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
  • 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


Resources

Cheatsheets

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
  • 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