Extending the Plots with Titles, Axis Labels, and Themes

Thus far, we've learned much of how ggplot2 works by creating a few often-used plots. We have not addressed adding titles, customizing axis labels, or themes. Let's look at the basics of these three things.

Titles and axis labels can be added in two different ways in ggplot2.

If you're interested in changing everything in one go, you can use the labs() function. labs() takes as an argument anything you'd like to change the label of, such as title, subtitle, x, y, caption, or even the label of the legend. It is often used as follows:

mtcars_ggplot + geom_point() + labs(title = "mpg vs. wt",      ubtitle = "mtcars dataset",     x = "weight",     caption = "decreasing linear trend")

This adds a title ...

Get R Programming Fundamentals now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.