Creating plots with the ggplot2 package

ggplot2 graphs are built iteratively, starting with the most basic plot. Additional layers are chained with the + sign to generate the final plot.

To construct a plot we need at least data, aesthetics (color, shape, and size), and geoms (points, lines, and smooth). The geoms determine which type of graph is drawn. Facets can be added for conditional plots.

Getting ready

Download the files for this chapter and copy the auto-mpg.csv file to your R working directory. Read the file using the read.csv command and save in the auto variable. Convert cylinders into a factor variable. If you have not done so already, install the ggplot2 package as follows:

> install.packages("ggplot2") > library(ggplot2) > auto <- ...

Get R: Recipes for Analysis, Visualization and Machine Learning 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.