Understanding the grammar of graphics
The previous example contained the three essential layers that need to be specified when plotting a graph: data, aesthetics, and geometries. The primary purpose of each layer is listed as follows:
- The data layer specifies the dataset to be plotted. This corresponds to the
mtcars
dataset we specified earlier. - The aesthetics layer specifies the scale-related items that map the variables to the visual properties of the plot. Examples include the variables to be shown for the x axis and y axis, the size and color, and other plot aesthetics. This corresponds to the
cyl
andmpg
variables we specified earlier. - The geometry layer specifies the visual elements used for the data, such as presenting the data via points, lines, or other forms. The
geom_point()
command we set in the previous example tells the plot to be shown as a scatter plot.
Other layers, such as the theme layer, also help beautify the plot, which we will cover later...