O'Reilly logo

R Programming Fundamentals by Kaelen Medeiros

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

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

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