# Adding titles and labels to graphs in R using plot() function

Adding titles to plot() graphs in R is quite straight forward. In this post I will explain how to do it.

Example 1:

The function used here is as follows:

plot(cars,
main = “Car Speed versus Stopping distance”,
xlab = “Speed (mph)”,
ylab = “Stopping distance (ft)”,
sub = “Source: R data set package”)

The data used here comes from the standard data set package that comes with R. As you can see I have used some arguments to add the titles:

main: for the main title
xlab: for the label on the x axisylab: for the label on the y axis
sub: for the sub title

Now this is all very black and white. Let’s add some color to it to make it even better readable.

Example 2

The function used here is as follows:

plot(cars,
main = “Car Speed versus Stopping distance”,
xlab = “Speed (mph)”,
ylab = “Stopping distance (ft)”,
sub = “Source: R data set package”,
col = “#dd2d2d”,
col.main = 604,
col.lab = “#227447”,
col.sub = “red”)

The arguments for the color used in this function call:

col: The graph dot(in this case) color
col.main: Main title color
col.lab: Label color of X – and Y axis
col.sub: Color of Sub title.

You can use Hexadecimal color notation (#dd2d2d), index numbers (see here for an overview), or simple text (red) to define the colors you wish to use.

You can use many more parameters for the plot() function. Just type ?par in your Rstudio console to view the list of possible parameters.

## Changing font size of plot() titles and labels.

Apart from changing colors, you can also change the size of the titles and labels by using cex.

Look at the following function call:

plot(cars,
main = “Car Speed versus Stopping distance”,
xlab = “Speed (mph)”,
ylab = “Stopping distance (ft)”,
sub = “Source: R data set package”,
col = “#dd2d2d”,
col.main = 604,
col.lab = “#227447”,
col.sub = “red”,
cex.main = 1.6,
cex.lab = 1.2,
cex.sub = 0.8,
cex.axis = 0.8)

The arguments:

cex.main: size of the main title
cex.lab: size of the axis labels
cex.sub: size of the sub title
cex.axis: size of axis annotation

Cex is a numerical value giving the amount by which plotting text and symbols should be magnified relative to the default.