2.1 Introduction

The goal of this chapter is to teach you how to produce useful graphics with ggplot2 as quickly as possible. You’ll learn the basics of ggplot() along with some useful “recipes” to make the most important plots. ggplot() allows you to make complex plots with just a few lines of code because it’s based on a rich underlying theory, the grammar of graphics. Here we’ll skip the theory and focus on the practice, and in later chapters you’ll learn how to use the full expressive power of the grammar.

In this chapter you’ll learn:

  • About the mpg dataset included with ggplot2, Section 2.2.

  • The three key components of every plot: data, aesthetics and geoms, Section 2.3.

  • How to add additional variables to a plot with aesthetics, Section 2.4.

  • How to display additional categorical variables in a plot using small multiples created by faceting, Section 2.5.

  • A variety of different geoms that you can use to create different types of plots, Section 2.6.

  • How to modify the axes, Section 2.7.

  • Things you can do with a plot object other than display it, like save it to disk, Section 2.8.