Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adjust plot title (main) position

Tags:

plot

r

I have been unable to find a way to adjust the (vertical) distance between plot and main title in R using par. In this example:

plot(1, 1, main = "Title") 

I can adjust the position of the axis titles using:

par(mgp = c(2.5, 1, 0)) 

But I see no way to similarly adjust the main title. I am aware that more manual control is possible using title or mtext, but I assume that there is a way setting the title distance using par as well, which would be more elegant for my purposes.

like image 281
Rob Hall Avatar asked Dec 03 '13 15:12

Rob Hall


People also ask

How do I change the position of a title in Matplotlib?

Matplotlib can display plot titles centered, flush with the left side of a set of axes, and flush with the right side of a set of axes. Automatic positioning can be turned off by manually specifying the y keyword argument for the title or setting rcParams["axes. titley"] (default: None ) in the rcParams.

How do I change the position of a title in R?

You can adjust the position of the title with the adj argument, that takes values from 0 (left-justified) to 1 (right-justified). Default value is 0.5. However, if you specify the argument adj inside your plotting function all text will be adjusted. If you only want some texts adjusted use it inside the title function.

Which method should be used to change the title of a plot?

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.


2 Answers

We can use title() function with negative line value to bring down the title.

See this example:

plot(1, 1) title("Title", line = -2) 

enter image description here

like image 138
zx8754 Avatar answered Sep 17 '22 04:09

zx8754


To summarize and explain visually how it works. Code construction is as follows:

par(mar = c(3,2,2,1)) barplot(...all parameters...) title("Title text", adj = 0.5, line = 0) 

explanation:

par(mar = c(low, left, top, right)) - margins of the graph area.  title("text" - title text       adj  = from left (0) to right (1) with anything in between: 0.1, 0.2, etc...       line = positive values move title text up, negative - down) 

enter image description here

like image 45
Denis Rasulev Avatar answered Sep 21 '22 04:09

Denis Rasulev