Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot title at bottom of plot using ggplot2

Tags:

plot

r

ggplot2

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.

like image 387
d.putto Avatar asked May 04 '15 09:05

d.putto


People also ask

How can you add a title to a plot created using ggplot?

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.

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.

How do you break a title in R?

To add a break in the title, simply write '\n' in the text.


2 Answers

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))

plot

like image 97
rcs Avatar answered Sep 21 '22 08:09

rcs


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")

enter image description here

like image 39
baptiste Avatar answered Sep 21 '22 08:09

baptiste