- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to align the bars of a barplot with the X-axis using ggplot2 in R?
The bar plot is created with geom_bar function but there always exist some space between the bars and the X-axis labels. If we want to reduce that space or completely remove it we need to use scale_y_continuous function by defining expand argument for former and scale_y_continuous(expand=c(0,0)) for latter.
Example
Consider the below data frame −
set.seed(888) x<-c("S1","S2","S3","S4") y<-c(24,27,25,28) df<-data.frame(x,y) df
Output
x y 1 S1 24 2 S2 27 3 S3 25 4 S4 28
Loading ggplot2 package and creating bar plot of y −
library(ggplot2) ggplot(df,aes(x,y))+geom_bar(stat="identity")
Output
Creating the bar plot without space between X-axis labels and the bars −
ggplot(df,aes(x,y))+geom_bar(stat="identity")+scale_y_continuous(expand=c(0,0))
Output
- Related Articles
- How to display negative labels below bars in barplot using ggplot2 in R?
- How to create a bar plot in R with label of bars on top of the bars using ggplot2?
- How to change the color of X-axis label using ggplot2 in R?
- How to create a barplot with one of the bars having different color in R?
- How to change the color of bars in base R barplot?
- How to X-axis labels to the top of the plot using ggplot2 in R?
- How to change the space between bars in a barplot in R?
- How to reverse the bars of a bar plot a using ggplot2 in R?
- How to increase the X-axis labels font size using ggplot2 in R?
- How to fill histogram bars using ggplot2 in R with different colors?
- How to change the adjustment of the plot title using ggplot2 to align it above the Y-axis labels in R?
- How to change the automatic sorting of X-axis of a bar plot using ggplot2 in R?
- How to reverse the X-axis labels of scatterplot created by using ggplot2 in R?
- How to change the color of bars of a bar plot using ggplot2 in R?
- How to create a stacked bar plot with vertical bars in R using ggplot2?

Advertisements