ggplot2 plot area margins?

RGgplot2Data Visualization

R Problem Overview


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

R Solutions


Solution 1 - R

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKT.View Question on Stackoverflow
Solution 1 - RjubaView Answer on Stackoverflow