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