Add titles to a plot in R software


The aim of this article is to show how to modify the title of graphs (main title and axis titles) in R software. There are two possible ways to do that :

  • Directly by specifying the titles to the plotting function (ex : plot() ). In this case titles are modified during the creation of plot.
  • the title() function can also be used. It adds titles on an existing plot.

Change main title and axis labels

The following arguments can be used :

  • main: the text for the main title
  • xlab: the text for the x axis label
  • ylab: the text for y axis title
  • sub: sub-title; It’s placed at the bottom of x-axis
# Simple graph
barplot(c(2,5))
# Add titles
barplot(c(2,5), main="Main title",
        xlab="X axis title",
        ylab="Y axis title",
        sub="Sub-title")

Plot title in RPlot title in R

title colors

The following parameters can be used to change the colors :

  • col.main: color the main title
  • col.lab: color of the axis titles (x and y axis)
  • col.sub: color of the sub-title
barplot(c(2,5), main="Main title",
        xlab="X axis title",
        ylab="Y axis title",
        sub="Sub-title",
        col.main="red", col.lab="blue", col.sub="black")

Plot title in R

Note that, the different colors available in R software are described here.

The font style for the text of the titles

The graphical parameters to use for customizing the font of the titles are :

  • font.main: font style for the main title
  • font.lab: font style for the axis titles
  • font.sub: font style for the sub-title

The value of these arguments should be an integer.

The possible values for the font style are :

  • 1: normal text
  • 2: bold
  • 3: italic
  • 4: bold and italic
  • 5 : Symbol font

Use the R code below to create a plot title with bold and italic font style.

# Titles in bold and italic
barplot(c(2,5), main="Main title",
        xlab="X axis title",
        ylab="Y axis title",
        sub="Sub-title",
        font.main=4, font.lab=4, font.sub=4)

Plot title in R

Change the font size

font size can be modified using the graphical parameter : cex. The default value is 1. If cex value is inferior to 1, then the text size is decreased. Conversely, any value of cex greater than 1 can increase the font size.

The following arguments can be used to change the font size :

  • cex.main : text size for main title
  • cex.lab : text size for axis title
  • cex.sub : text size of the sub-title

An example is shown below :

# Increase the size
barplot(c(2,5), main="Main title",
        xlab="X axis title",
        ylab="Y axis title",
        sub="Sub-title",
        cex.main=2, cex.lab=1.7, cex.sub=1.2)

Plot title in R

Use the title() function

title() can be also used to add titles to a graph.

A simplified format is :

title(main = NULL, sub = NULL, 
      xlab = NULL, ylab = NULL, ...)

Example of usage

x<-1:10; y<-x*x
plot(x,y, main = "", xlab="", ylab="",
     col.axis="blue") 
title(main = "Main title", sub = "Sub-title",
      xlab = "X axis", ylab = "Y axis",
      cex.main = 2,   font.main= 4, col.main= "red",
      cex.sub = 0.75, font.sub = 3, col.sub = "green",
      col.lab ="darkblue"
      )

Plot title in R

Customize the titles using par() function

Note that, the R par() function can be used to change the color, font style and size for the graph titles. The modifications done by the par() function are called ‘permanent modification’ because they are applied to all the plots generated under the current R session.

Read more on par() by clicking here.

par(
  # Change the colors
  col.main="red", col.lab="blue", col.sub="black",
  # Titles in italic and bold
  font.main=4, font.lab=4, font.sub=4,
  # Change font size
  cex.main=2, cex.lab=1.7, cex.sub=1.2
  )
barplot(c(2,5), main="TMain title",
        xlab="X axis title",
        ylab="Y axis title",
        sub="Sub title")

Plot title in R

Infos

This analysis has been performed using R statistical software (ver. 3.1.0).


Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!






This page has been seen 26925 times