plot(mtcars$disp, mtcars$mpg,
main = 'Displacement vs Miles Per Gallon')
In this chapter, we will learn how to add:
to a plot and to modify:
In the previous chapter, we created plots which did not have any title or labels. Such plots are of no use to any one as they do not indicate what the X and Y axis represent or the primary information being communicated by the plot. The title and labels play an important part in making the plot holistic. There are two ways to add them to a plot:
plot() functiontitle() functionWe will explore both the above methods one by one, and you can choose the method most convenient to you. Let us begin with the plot() function:
| Feature | Argument | Value | Example |
|---|---|---|---|
| Title | main | string | “Scatter Plot” |
| Subtitle | sub | string | “Displacement vs Miles Per Gallon” |
| X Axis Label | xlab | string | “Displacement” |
| Y Axis Label | ylab | string | “Miles Per Gallon” |
You can add a title to the plot using the main argument in the plot() function. Ensure that the title is enclosed in single/double quotes as it is a string. Let us create a scatter plot of disp and mpg from mtcars data set, and add a title to it.
You can add a subtitle to the plot using the sub argument in the plot() function. The subtitle will appear below the X axis label. Ensure that the subtitle is enclosed in single/double quotes as it is a string. Let us add a subtitle to the plot we created in the previous example:
In the plots created in the previous examples, the axis labels appear as mtcars$mpg and mtcars$disp. It is not the best way to name the axis and it will make more sense to use names that describe the data. Let us modify the axis labels using the xlab and ylab arguments in the plot() function:
We can add title, subtitle and axis labels using the title() function as well. Let us recreate the plots from the previous examples but this time we will use the title() instead of the plot() function. We will continue to use the plot() function to create the plot.
# create scatter plot
plot(mtcars$disp, mtcars$mpg)
# add title, subtitle and axis labels
title(main = 'Displacement vs Miles Per Gallon',
sub = 'Mileage is inversely related to Displacement',
xlab = 'Displacement', ylab = 'Miles Per Gallon')
Do you notice that the axis labels are overwritten? This happens because the plot() function adds the default labels and we add a new set of labels without modifying the existing ones. The solution is to instruct the plot() function not to add any labels to the X and Y axis. This can be achieved using the ann (annotate) argument in the plot() function and set it to FALSE. Let us try it:
# create scatter plot
plot(mtcars$disp, mtcars$mpg, ann = FALSE)
# add title, subtitle and axis labels
title(main = 'Displacement vs Miles Per Gallon',
sub = 'Mileage is inversely related to Displacement',
xlab = 'Displacement', ylab = 'Miles Per Gallon')
The axis labels are legible and not overwritten. You can use either the plot() function or the title() function to add title, subtitle and axis labels but ensure that in case you use the title() function, set ann argument to FALSE in the plot() function.
In certain cases, you would want to modify the range of the axis of the plots. By default, the plot() function will take into account the min and max values of the variable(s) and set the range for the axis. We can modify the range by using the xlim and ylim arguments in the plot() function. Both the xlim and ylim arguments take 2 values as inputes. The first value is the minimum value for the axis and the second value is the maximum value for the axis. The plot() function will return an error if we do not specify two values for both xlim and ylim arguments. Let us recreate the plot from the previous examples but change the range of both the X and Y axis:
Keep in mind that the axis ranges cannot be modified using the title() function.
Try it in your browser. Edit the code below and press Run – it executes entirely in your browser via WebR, so no R installation is needed. The runtime downloads once, on this page only. The printed plots are produced in your browser and are not part of the PDF or ePub editions.
---
description: "Add titles and axis labels to base R plots: main, sub, xlab, ylab, xlim, ylim and title(), for figures that are readable on their own."
---
# Title & Axis Labels {#titlelabels}
## Introduction
In this chapter, we will learn how to add:
- Title
- Subtitle
- Axis Labels
to a plot and to modify:
- the range of an axis
In the previous chapter, we created plots which did not have any title or labels. Such plots are of no use to any one as they do not indicate what the X and Y axis represent or the primary information being communicated by the plot. The `title` and `labels` play an important part in making the plot holistic. There are two ways to add them to a plot:
- use the relevant arguments within the `plot()` function
- use the `title()` function
We will explore both the above methods one by one, and you can choose the method most convenient to you. Let us begin with the `plot()` function:
## Syntax
|Feature|Argument|Value|Example|
|:-------|:--------|:-----|:-------|
|Title |main |string|"Scatter Plot"|
|Subtitle |sub |string|"Displacement vs Miles Per Gallon"|
|X Axis Label |xlab |string|"Displacement"|
|Y Axis Label |ylab |string|"Miles Per Gallon"|
## Title
You can add a title to the plot using the `main` argument in the `plot()` function. Ensure that the title is enclosed in single/double quotes as it is a string. Let us create a scatter plot of `disp` and `mpg` from `mtcars` data set, and add a title to it.
```{r}
#| label: fig-tl-plot
#| fig-cap: "Adding a title and axis labels directly in the plot() call"
#| fig-alt: "A scatterplot of displacement against miles per gallon titled Displacement vs Miles Per Gallon, with the horizontal axis labelled Displacement and the vertical axis labelled Miles Per Gallon."
plot(mtcars$disp, mtcars$mpg,
main = 'Displacement vs Miles Per Gallon')
```
## Subtitle
You can add a subtitle to the plot using the `sub` argument in the `plot()` function. The subtitle will appear below the X axis label. Ensure that the subtitle is enclosed in single/double quotes as it is a string. Let us add a subtitle to the plot we created in the previous example:
```{r}
plot(mtcars$disp, mtcars$mpg,
main = 'Displacement vs Miles Per Gallon',
sub = 'Mileage is inversely related to Displacement')
```
## Axis Labels
In the plots created in the previous examples, the axis labels appear as `mtcars$mpg` and `mtcars$disp`. It is not the best way to name the axis and it will make more sense to use names that describe the data. Let us modify the axis labels using the `xlab` and `ylab` arguments in the `plot()` function:
```{r}
plot(mtcars$disp, mtcars$mpg,
main = 'Displacement vs Miles Per Gallon',
sub = 'Mileage is inversely related to Displacement',
xlab = 'Displacement', ylab = 'Miles Per Gallon')
```
## title()
We can add title, subtitle and axis labels using the `title()` function as well. Let us recreate the plots from the previous examples but this time we will use the `title()` instead of the `plot()` function. We will continue to use the `plot()` function to create the plot.
```{r}
# create scatter plot
plot(mtcars$disp, mtcars$mpg)
# add title, subtitle and axis labels
title(main = 'Displacement vs Miles Per Gallon',
sub = 'Mileage is inversely related to Displacement',
xlab = 'Displacement', ylab = 'Miles Per Gallon')
```
Do you notice that the axis labels are overwritten? This happens because the `plot()` function adds the default labels and we add a new set of labels without modifying the existing ones. The solution is to instruct the `plot()` function not to add any labels to the X and Y axis. This can be achieved using the `ann` (annotate) argument in the `plot()` function and set it to `FALSE`. Let us try it:
```{r}
#| label: fig-tl-title
#| fig-cap: "The two-step method: plot with ann = FALSE, then add text with title()"
#| fig-alt: "The same scatterplot and the same title and axis labels as the previous figure, but produced by suppressing the automatic annotations in plot() and adding them afterwards with the title() function. The result is visually identical."
# create scatter plot
plot(mtcars$disp, mtcars$mpg, ann = FALSE)
# add title, subtitle and axis labels
title(main = 'Displacement vs Miles Per Gallon',
sub = 'Mileage is inversely related to Displacement',
xlab = 'Displacement', ylab = 'Miles Per Gallon')
```
The axis labels are legible and not overwritten. You can use either the `plot()` function or the `title()` function to add title, subtitle and axis labels but ensure that in case you use the `title()` function, set `ann` argument to `FALSE` in the `plot()` function.
## Axis Range
In certain cases, you would want to modify the range of the axis of the plots. By default, the `plot()` function will take into account the `min` and `max` values of the variable(s) and set the range for the axis. We can modify the range by using the `xlim` and `ylim` arguments in the `plot()` function. Both the `xlim` and `ylim` arguments take 2 values as inputes. The first value is the minimum value for the axis and the second value is the maximum value for the axis. The `plot()` function will return an error if we do not specify two values for both `xlim` and `ylim` arguments. Let us recreate the plot from the previous examples but change the range of both the X and Y axis:
```{r}
#| label: fig-tl-final
#| fig-cap: "Title, subtitle and enlarged title text combined"
#| fig-alt: "A scatterplot of displacement against miles per gallon with a large bold main title, a smaller subtitle beneath it, and both axes labelled. The main title is set noticeably larger than the default."
plot(mtcars$disp, mtcars$mpg,
xlim = c(0, 600), ylim = c(0, 50))
```
Keep in mind that the axis ranges cannot be modified using the `title()` function.
## Try it live {.unnumbered}
> **Try it in your browser.** Edit the code below and press **Run** -- it
> executes entirely in your browser via
> [WebR](https://docs.r-wasm.org/webr/latest/), so no R installation is
> needed. The runtime downloads once, on this page only. The printed plots
> are produced in your browser and are not part of the PDF or ePub editions.
```{webr-r}
# every label argument from this chapter at once
plot(mtcars$disp, mtcars$mpg,
main = 'Displacement vs Miles Per Gallon',
sub = 'Mileage is inversely related to Displacement',
xlab = 'Displacement', ylab = 'Miles Per Gallon',
xlim = c(0, 600), ylim = c(0, 50))
title(main = 'Retitled with title()', cex.main = 1.4)
```