# How to Add Titles to Plots in Base R

When it comes to data visualization in R, plots are indispensable. From histograms and bar charts to scatter plots and boxplots, visualizations are an effective way to present data and summarize statistical results. However, a plot without an appropriate title or subtitles is incomplete. Titles are an essential part of a plot as they describe what the plot is about. They are especially crucial when sharing your visualizations with others, as they make your graphics more understandable and informative.

In this comprehensive guide, we will discuss how to add titles and subtitles to plots using base R functions. We will be covering everything from basic plots to more complex multiple plots and explore some more subtle details, such as positioning and formatting of titles.

## Adding Titles in Base R Plots

Let’s begin with the basic R plot, where we will use the plot() function to generate a simple scatter plot. For the purpose of this guide, we will use the built-in mtcars dataset in R, which includes various car attributes.

To add a main title, we use the main argument in the plot() function. Here’s how we can create a scatter plot of miles per gallon (mpg) against horsepower (hp) with a title:

# Create a basic scatter plot
plot(mtcars$mpg ~ mtcars$hp,
main = "Scatterplot of MPG vs. Horsepower")

In the code above, the main argument is used to specify the main title of the plot.

Subtitles provide additional details about the plot. To add a subtitle, we use the sub argument in the plot() function.

# Create a scatter plot with a subtitle
plot(mtcars$mpg ~ mtcars$hp,
main = "Scatterplot of MPG vs. Horsepower",
sub = "Data from mtcars dataset")

In the code above, the sub argument is used to specify the subtitle of the plot.

In addition to main titles and subtitles, we can also add titles to the x-axis and y-axis using the xlab and ylab arguments respectively.

# Create a scatter plot with axis titles
plot(mtcars$mpg ~ mtcars$hp,
main = "Scatterplot of MPG vs. Horsepower",
sub = "Data from mtcars dataset",
xlab = "Horsepower",
ylab = "Miles Per Gallon")

## Formatting Titles

R allows you to customize the appearance of the titles using various graphical parameters. This includes changing the color, size, and font of the text. The col.main, cex.main, and font.main parameters are used to control the color, size, and font of the main title. Similar parameters (col.sub, cex.sub, font.sub, col.lab, cex.lab, and font.lab) are used for the subtitle and axis labels.

# Create a scatter plot with formatted titles
plot(mtcars$mpg ~ mtcars$hp,
main = "Scatterplot of MPG vs. Horsepower",
sub = "Data from mtcars dataset",
xlab = "Horsepower",
ylab = "Miles Per Gallon",
col.main = "blue",
cex.main = 1.5,
font.main = 4,
col.sub = "red",
cex.sub = 0.9,
col.lab = "darkgreen",
cex.lab = 1.2)

In the code above, col.main, cex.main, and font.main are used to change the color, size, and font of the main title, and similarly for the subtitle and axis labels.

## Adding Titles to Multiple Plots

When creating multiple plots in one graphic window, you can add a main title to the entire plot using the title() function.

# Create multiple plots
par(mfrow = c(2, 1))  # divide graphic into a two by one array
plot(mtcars$mpg ~ mtcars$hp,
main = "Scatterplot 1: MPG vs. Horsepower")
plot(mtcars$mpg ~ mtcars$cyl,
main = "Scatterplot 2: MPG vs. Number of Cylinders")
title("Scatterplots from mtcars dataset", outer = TRUE, line = -1)

In the above code, par(mfrow = c(2, 1)) is used to create an area for two plots, one above the other. title() with outer = TRUE is used to add a title to the entire plot area.

## Conclusion

Adding appropriate titles and subtitles is an integral part of creating meaningful plots in R. They not only give a brief introduction of what your plot is about but also make your visualizations more professional and informative. With base R’s built-in functions and parameters, you can create and customize plot titles with relative ease.

Posted in RTagged