Table of Contents
GGplot2 is a popular data visualization package in R that allows users to create high-quality and customizable graphs. One aspect of customization is the ability to modify the margins of a graph, which refers to the space between the plot and the edges of the plotting area. This can be achieved by using the “theme” function in ggplot2 and specifying the desired margin values. By adjusting the margins, users can control the spacing and layout of their graphs to better fit their data and improve the overall visual appeal. This feature in ggplot2 provides users with a more tailored and professional-looking graph for their data analysis and presentation.
Modify the Margins in ggplot2 (With Examples)
You can use the theme() argument in ggplot2 to change the margin areas of a plot:
ggplot(df, aes(x=x)) + geom_histogram() + theme(plot.margin=unit(c(5,1,1,1), 'cm'))
Keep in mind that the order for the plot margins is:
- unit(c(top, right, bottom, left), units)
The following examples shows how change the margin areas of ggplot2 plots in practice.
Example 1: Create Basic Plot
The following code shows how to create a basic plot in ggplot2 without specifying any margin areas:
library(ggplot2)
#make this example reproducible
set.seed(0)
#create data
df <- data.frame(x=rnorm(n=5000))
#create histogram using ggplot2
ggplot(df, aes(x=x)) +
geom_histogram() +
ggtitle('Title of Histogram') +
theme(plot.background=element_rect(fill='#e3fbff'))

Notice how the plot has minimal margins on each side.
Example 2: Modify Margins of the Plot
The following code shows how to add significant margins to the top and bottom of the plot:
library(ggplot2)
#make this example reproducible
set.seed(0)
#create data
df <- data.frame(x=rnorm(n=5000))
#create histogram with significant margins on top and bottom
ggplot(df, aes(x=x)) +
geom_histogram() +
ggtitle('Title of Histogram') +
theme(plot.margin=unit(c(5,1,5,1), 'cm'),
plot.background=element_rect(fill='#e3fbff'))
Notice how there’s a significant amount of space on the top and bottom of the plot.
And the following code shows how to add significant margins to the left and right of the plot:
library(ggplot2)
#make this example reproducible
set.seed(0)
#create data
df <- data.frame(x=rnorm(n=5000))
#create histogram with significant margins on left and right
ggplot(df, aes(x=x)) +
geom_histogram() +
ggtitle('Title of Histogram') +
theme(plot.margin=unit(c(1,5,1,5), 'cm'),
plot.background=element_rect(fill='#e3fbff'))
Notice how there’s a significant amount of space on the left and right of the plot.
The following tutorials explain how to perform other common operations in ggplot2:
Cite this article
stats writer (2024). How can I modify the margins in ggplot2?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-modify-the-margins-in-ggplot2/
stats writer. "How can I modify the margins in ggplot2?." PSYCHOLOGICAL SCALES, 12 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-modify-the-margins-in-ggplot2/.
stats writer. "How can I modify the margins in ggplot2?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-modify-the-margins-in-ggplot2/.
stats writer (2024) 'How can I modify the margins in ggplot2?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-modify-the-margins-in-ggplot2/.
[1] stats writer, "How can I modify the margins in ggplot2?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.
stats writer. How can I modify the margins in ggplot2?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
