2.3 Anatomy of a ggplot command

To learn more about visualizing data with the ggplot2 R package, see Hadley Wickham’s textbook.

In this course, we’ll largely construct visualizations using the ggplot() function from the ggplot2 R package. NOTE: gg is short for “grammar of graphics”. Plots constructed from the ggplot() function are constructed in layers, and the syntax used to create plots is meant to reflect this layered construction. As you read through the rest of this chapter, pay attention to how the syntax generally follows this structure:

data %>%
    ggplot(aes(x = X_AXIS_VARIABLE, y = Y_AXIS_VARIABLE)) +
    VISUAL_LAYER1 +
    VISUAL_LAYER2 +
    VISUAL_LAYER3 + ...

We pass the aesthetic mapping from the data set to the plot with aes(). The visual layers are features such as points, lines, and panels. We’ll introduce these soon. The +’s allow us to add layers to build up a plot (note this is not the pipe!).

What are the function names in the example above? There are only two as it is written right now.