Adjust plot title (main) position
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.
Solution 1:
We can use title()
function with negative line
value to bring down the title.
See this example:
plot(1, 1)
title("Title", line = -2)
Solution 2:
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)
Solution 3:
Try this:
par(adj = 0)
plot(1, 1, main = "Title")
or equivalent:
plot(1, 1, main = "Title", adj = 0)
adj = 0
produces left-justified text, 0.5 (the default) centered text and 1 right-justified text. Any value in [0, 1]
is allowed.
However, the issue is that this will also change the position of the label of the x-axis and y-axis.