The Wayback Machine - https://web.archive.org/web/20240113195106/https://www.geeksforgeeks.org/how-to-avoid-overlapping-labels-in-ggplot2-in-r/
Open In App
Related Articles

How to Avoid Overlapping Labels in ggplot2 in R?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we are going to see how to avoid overlapping labels in ggplot2 in R Programming Language.

To avoid overlapping labels in ggplot2, we use guide_axis() within scale_x_discrete().

Syntax: plot+scale_x_discrete(guide = guide_axis(<type>))

In the place of we can use the following properties:

  • n.dodge: It makes overlapping labels shift a step-down.
  • check.overlap: This removes the overlapping labels and displays only those which do not overlap

Creating the basic barplot with overlapping labels

R




# Create sample data
set.seed(5642)                            
sample_data <- data.frame(name = c("Geeksforgeeks1",
                                   "Geeksforgeeks2",
                                   "Geeksforgeeks3",
                                   "Geeksforgeeks4",
                                   "Geeeksforgeeks5") ,
                          value = c(31,12,15,28,45))
# Load ggplot2 package
library("ggplot2")
 
# Create bar plot
plot<-ggplot(sample_data, aes(name,value, fill=name)) +
geom_bar(stat = "identity")
plot


 

 

Output:

 

Avoid overlapping labels

 

To avoid overlapping by shifting labels downward we use n.dodge parameter of guide_axis() function:

 

R




# Create sample data
set.seed(5642)                            
sample_data <- data.frame(name = c("Geeksforgeeks1",
                                   "Geeksforgeeks2",
                                   "Geeksforgeeks3",
                                   "Geeksforgeeks4",
                                   "Geeeksforgeeks5") ,
                          value = c(31,12,15,28,45))
# Load ggplot2 package
library("ggplot2")
 
# Create bar plot without overlapping labels
plot<-ggplot(sample_data, aes(name,value, fill=name)) +
geom_bar(stat = "identity") +
scale_x_discrete(guide = guide_axis(n.dodge=2))
plot


 

 

Output:

 

Remove overlapping

 

To remove overlapping labels we use check.overlap parameter of guide_axis() function:

 

R




# Create sample data
set.seed(5642)                            
sample_data <- data.frame(name = c("Geeksforgeeks1",
                                   "Geeksforgeeks2",
                                   "Geeksforgeeks3",
                                   "Geeksforgeeks4",
                                   "Geeeksforgeeks5") ,
                          value = c(31,12,15,28,45))
# Load ggplot2 package
library("ggplot2")
 
# Create bar plot without overlapping labels
plot<-ggplot(sample_data, aes(name,value, fill=name)) +
geom_bar(stat = "identity") +
scale_x_discrete(guide = guide_axis(check.overlap = TRUE))
plot


 

 

Output:

 

 


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

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!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 18 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials