Open In App
Related Articles

How to adjust Space Between ggplot2 Axis Labels and Plot Area in R ?

Improve Article
Improve
Save Article
Save
Like Article
Like

While plotting the graphs we can make different changes in order to make them much more appealing and clear to the observer. One of the ways is to adjust the spacing between the labels and plot area. In this article, we will study how to adjust space between ggplot2 Axis Labels and plot area in R Programming Language.

To add customizations to our plot we can use the theme() function.

Method 1: Adjusting vertical spaces

To adjust the vertical spacing we use vjust in the element_text function to vertically adjust the plotting

Example

R




rm(list=ls())
  
data <- data.frame(name = c("Aditya", "Ritika", "Pulkit",
                            "Vishesh", "Kaif"), 
                     
                   marks = c(58, 82, 79, 42, 66))           
                               
library("ggplot2")   
  
ggp <- ggplot(data, aes(name, marks, fill = name)) +            
  geom_bar(stat = "identity")
  
ggp + theme(axis.text.x = element_text(vjust = -12))       


Output:

vertically adjusted plot

Method 2: Adjust horizontal spacing

We can adjust the horizontal spacing in the similar way using hjust in element_text

Example

R




rm(list=ls())
  
data <- data.frame(Name = c("Aditya", "Ritika", "Pulkit",
                            "Vishesh", "Kaif"), 
                     
                   Marks = c(58, 82, 79, 42, 66))           
  
library("ggplot2")                                          
  
ggp <- ggplot(data, aes(Name, Marks, fill = Name)) +            
  geom_bar(stat = "identity")                        
  
ggp + theme(axis.text.x = element_text(hjust = -1))   


Output:

horizontally adjusted plot


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 21 Apr, 2021
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials