[r] ggplot2 plot area margins?

Is there an easy way to increase the space between the plot title and the plot area below it (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"?

This question is related to r ggplot2 data-visualization

The answer is


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.


Examples related to r

How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R? R : how to simply repeat a command? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium How to show code but hide output in RMarkdown? remove kernel on jupyter notebook Function to calculate R2 (R-squared) in R Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph R multiple conditions in if statement What does "The following object is masked from 'package:xxx'" mean?

Examples related to ggplot2

Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph Saving a high resolution image in R Change bar plot colour in geom_bar with ggplot2 in r Remove legend ggplot 2.2 Remove all of x axis labels in ggplot Changing fonts in ggplot2 Explain ggplot2 warning: "Removed k rows containing missing values" Error: package or namespace load failed for ggplot2 and for data.table In R, dealing with Error: ggplot2 doesn't know how to deal with data of class numeric

Examples related to data-visualization

How can I run Tensorboard on a remote server? How to plot a histogram using Matplotlib in Python with a list of data? Plot correlation matrix using pandas Adding value labels on a matplotlib bar chart ggplot2, change title size How to make IPython notebook matplotlib plot inline Construct a manual legend for a complicated plot 3D Plotting from X, Y, Z Data, Excel or other Tools Moving x-axis to the top of a plot in matplotlib Heatmap in matplotlib with pcolor?