Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust plot title and sub-title in base R

Tags:

r

How do I get base R plots title and subtitle to mimic ggplots? I'd prefer everything left aligned, nothing in bold, and subtitle to be directly below title.

I'd also like a little more space between everything. Maybe my newline \n 'hack' is the best way to accomplish this?

plot(mtcars)
title(main = "I want main title NOT bold and left aligned\n\n", 
      sub = "Sub title should be under the main title left aligned")

r_titles

like image 414
Display name Avatar asked Mar 05 '19 11:03

Display name


People also ask

How do I add a title to a base plot in R?

Add Titles to a Graph in R Programming – title() Function title() function in R Language is used to add main title and axis title to a graph. This function can also be used to modify the existing titles. Syntax: title(main = NULL, sub = NULL, xlab = NULL, ylab = NULL, …)

How do I change a plot name in R?

R plot title In order to change the plot title position you can set the adj argument with a value between 0 (left) and 1 (right) and the line argument, where values greater than 1.7 (default) move the title up and values lower than 1.7 to move it down. Negative values of line will make the title go inside the plot.

How do you change the axis titles in R studio?

Changing axis labels To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .


1 Answers

Based on Cettt's suggestions, what about using mtext as follows:

plot(mtcars, oma=c(2, 3, 5, 2))
mytitle = "I want main title NOT bold and left aligned"
mysubtitle = "Sub title should be under the main title left aligned"
mtext(side=3, line=3, at=-0.07, adj=0, cex=1, mytitle)
mtext(side=3, line=2, at=-0.07, adj=0, cex=0.7, mysubtitle)

The adj=0 option requests left alignment.
The line= options define the vertical position of each title, counting outwards from the border of the plot top margin.
You can play with the at= option to move the titles horizontally as wished.

Note also the use of the oma= option in the plot() call so that there is enough space to place the titles above the pairs plot.

Here is the plot: enter image description here

like image 67
mastropi Avatar answered Oct 11 '22 09:10

mastropi