117

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

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
KT.
  • 10,815
  • 4
  • 47
  • 71

1 Answers1

162

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.

Jaap
  • 81,064
  • 34
  • 182
  • 193
juba
  • 47,631
  • 14
  • 113
  • 118
  • 5
    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`. – Paul 'Joey' McMurdie Sep 19 '12 at 19:39
  • 62
    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. – mightypile May 08 '17 at 21:21
  • 36
    @generic_user: maybe easier to remember as noted [here](https://ggplot2.tidyverse.org/reference/element.html): `t, r, b, l (To remember order, think trouble).` – Tung Sep 05 '18 at 19:16
  • @PaulMcMurdie Could you please tell what exactly you entered for the ```units``` argument? I cannot make it work. Thanks! – Mehdi.K Jan 11 '19 at 11:26
  • 9
    Alternatively to remember... it's just clockwise from the top: `top`, `right`, `bottom`, `left`. – spops Jun 03 '19 at 20:16
  • 5
    also: `margin(t, r, l, b)` – Brian D Dec 12 '19 at 17:45
  • 1
    @Mehdi.K I believe that the syntax used to be "plot.margin = grid::unit(t, r, b, l)) Best I can tell ggplot is not using 'units' anymore? I've been using plot.margin = margin(0,0,0,0, unit = 'cm') as suggested by BrianD – Kodiakflds Jun 10 '20 at 23:58
  • 1
    And the default plot margins for the theme you are using can be found by extracting `plot.margin` from the theme you are using as in `theme_grey()$plot.margin` (copied from the answer in this question: https://stackoverflow.com/questions/32146846/what-are-the-default-ggplot2-plot-margins) – ESELIA Dec 31 '21 at 23:16
  • There are a lot of options for different units (other than `"cm"`). A few options are `"inches"`, `"points"`, `"lines"`. [More options here](https://www.rdocumentation.org/packages/grid/versions/3.6.2/topics/unit). – filups21 Dec 02 '22 at 13:17
  • Reminds me of that particular hat the 'trilby' (t, r, l, b) – Markm0705 Feb 04 '23 at 09:06