♣31♣A Grammar of Graphics with ggplot2

We already used ggplot2 in Chapter 9.6 “Violin Plots” on page 173, Chapter 22.2.3 “The AUC” on page 396 and in Chapter 30.7.7 “Dependencies of the Option Price” on page 660 (and following). We were confident in doing so, because ggplot2 is rather intuitive and it is not so difficult to understand what a given code segment does. In this section, we will use little words, but allow the code to speak for itself and provide tactical examples that learn how to use ggplot2 in practice.

With ggplot2 comes the notion of a “grammar of graphics.” Just as the grammar for the English language, it is a set of rules that allow different words to interact and produce something that makes sense. That something is in the case of ggplot2 is a professional and clear chart.

31.1 The Basics of ggplot2

To explore ggplot2, we will use the dataset mtcars from the library datasets. It is usually loaded as the start of R, and it is already known from other sections in this book.

Get The Big R-Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.