R Tutorial: ggplot2 layers

Learn how ggplot2 builds plots by stacking layers: data, aesthetics that map variables to axes, geometry like scatterplots, and theme for clean visuals. Uses the Iris dataset to show sepal length and width plotted as points.
Chapters
Now that we have some idea about the different grammatical elements of graphics, let's see how this works in practice. The grammar of graphics is implemented in r using the ggplot2 package. There are two key functions that ggplot2 serves. First, we construct plots by layering grammatical elements on top of each other. Second, we use aesthetic mappings to bridge the link between data and its visual interpretation. We are going to go through each grammatical element in depth in this and the next course. Here I'll introduce a dataset which will be used throughout the videos and we'll go over some simple examples. The bottom layer is the data element. Obviously we need some data to plot. I'm going to use several different datasets in the course videos, one of which is the classic IRIS dataset collected by Edgar Anderson in the 1930s and thereafter popularized by Ronald Fisher. The dataset contains information on three Iris, setosa, versicolor and Virginica. Four measurements were taken from each plant. The petal length and width and the sepal length and width. You're probably familiar with petals. They are the colorful part of a flower. Sepals are the outer leaves of the flower. They are typically green, but in this case they are also colorful. There are 50 specimens in each species. The data is stored in an object called Iris. There are five variables, the species and one for each of the properties which were measured. The next layer we'll add is the aesthetics element, which tells us which scales we should map our data onto. This is where the second main component of the grammar graphics comes into play. On top of layering the grammatical elements, it's here that we establish our aesthetic mappings. In this case, we are going to make a scatterplot. So we're going to map sepal length onto the X aesthetic and sepal width onto the Y aesthetic. The next element is the geometry element. This allows us to choose how the plot will look. After we've established our three essential layers, we have enough instructions to make a basic scatter plot. It's pretty rough, so to get a more meaningful and cleaner visualization, we'll have to use the other layers. The next layer we'll look at is the themes element. It controls all the non data ink on our plot, which allows us to get a nice looking, meaningful and publication quality plot directly in R. We'll discuss the remaining grammatical elements in the next course. For now, let's begin by exploring these concepts.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







