O'Reilly logo

R: Recipes for Analysis, Visualization and Machine Learning by Chiu Yu-Wei, Atmajitsinh Gohil, Shanthi Viswanathan, Viswa Viswanathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 <- ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required