In ggplot2, how to put the plot title at the bottom of the plot.
qplot(rnorm(100)) + ggtitle("My Title")
puts the title at middle and top of plot, while I want it to be at middle and bottom of the plot.
To add a title to your plot, add the code +ggtitle("Your Title Here") to your line of basic ggplot code. Ensure you have quotation marks at the start and end of your title. If you have a particulary long title that would work better on two lines, use \n for a new line. Make sure to use the correct slash.
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.
To add a break in the title, simply write '\n' in the text.
Here is a solution using grid.text
:
library("grid")
qplot(rnorm(100)) + theme(plot.margin=unit(c(0.5, 1, 2, 0.5), "lines"))
grid.text("My Title", x = unit(0.5, "npc"), y = unit(0, "npc"),
vjust = -0.5, gp = gpar(cex=1.2))
With the dev version you can use the caption argument,
ggplot() +
labs(caption="Bottom Title") +
theme(plot.caption = element_text(hjust=0.5, size=rel(1.2)))
Alternatively, wrap the plot in grid.arrange(),
gridExtra::grid.arrange(ggplot(), bottom="Bottom Title")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With