Chapter 9 Visualization with ggplot()

Note that you will need the dcps data ("DCPS testing.RData") and the tidyverse package to replicate the commands. See Section 4.1 for a reminder on how to import the data and Section 2.4 for installing and loading packages.

Data visualization is a notable strength of R, and the ggplot() function offers a consistent approach to generate a tremendous variety of visualizations. There are 3 primary elements:

  • data to be plotted
  • “aesthetics” or connections between properties or values of the data and features of the graph
  • the “geometry” or plot type

The basic recipe begins by calling the ggplot() function, specifying the data to be plotted, connecting data to features of the graph (“aesthetics”), and selecting a plot type. As with the tidyverse syntax, using ggplot() often involves connecting a series of instructions. Rather than the pipe operator, ggplot() simply uses the plus sign (+).

The customization options are endless; the plot type, title, axis labels, scaling, colors, symbols, shapes, and much, much more. Here we will work through a few introductory examples, but, if you’d like to learn more, you may refer to R Studio’s Visualization Cheatsheet or the text written by ggplot()’s creator.