130

Is there an easy way to increase the space between the plot title and plot area below it (i.e., the box with the data). Similarly, I'd prefer to have some space between the axis title and axis labels.

In other words, is there a way to "move the title a bit up, the y axis title a bit left, and the x axis title a bit down"?

1
  • 2
    You can always paste some "\n" characters to the titles to force new lines. Commented May 31, 2012 at 18:04

1 Answer 1

175

You can adjust the plot margins with plot.margin in theme() and then move your axis labels and title with the vjust argument of element_text(). For example :

library(ggplot2)
library(grid)
qplot(rnorm(100)) +
    ggtitle("Title") +
    theme(axis.title.x=element_text(vjust=-2)) +
    theme(axis.title.y=element_text(angle=90, vjust=-0.5)) +
    theme(plot.title=element_text(size=15, vjust=3)) +
    theme(plot.margin = unit(c(1,1,1,1), "cm"))

will give you something like this :

enter image description here

If you want more informations about the different theme() parameters and their arguments, you can just enter ?theme at the R prompt.

Sign up to request clarification or add additional context in comments.

10 Comments

Thanks! I wasn't sure what to provide to grid::units to make this work for the plot.margin argument. Turns out you have to provide a length-4 numeric to units. Too bad the x argument to units isn't recycled in some way. Also, you probably know this already, but worth noting/updating that opts is now deprecated in the latest version of ggplot2 (0.9.2+), replaced by theme, as is theme_text now replaced by element_text.
The order of edges for plot.margin is unit(c(top, right, bottom, left), units) if anyone else wants to save the time looking that up.
@generic_user: maybe easier to remember as noted here: t, r, b, l (To remember order, think trouble).
Alternatively to remember... it's just clockwise from the top: top, right, bottom, left.
also: margin(t, r, l, b)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.