- 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 set the alignment of labels in horizontal bar plot to left in R?
When we create a horizontal bar plot using ggplot2 package, the labels of the categorical variable are aligned to the right-side of the axis and if the size of these labels are different then it looks a little ambiguous. Therefore, we might want to set the alignment of the labels to left-side and this can be done by using theme function of ggplot2 package.
Example
Consider the below data frame:
> df<-data.frame(x,y) > df
Output
x y 1 India 14 2 UK 15 3 Russia 12 4 United States of America 18
Loading ggplot2 package and creating a horizontal bar plot:
Example
> library(ggplot2) > ggplot(df,aes(x,y))+geom_bar(stat="identity")+coord_flip()
Output:
Creating the horizontal bar plot with categorical variable labels aligned to left-side:
Example
> ggplot(df,aes(x,y))+geom_bar(stat="identity")+coord_flip()+theme(axis.text.y=element_text(hjust=0))
Output:
- Related Articles
- How to create horizontal lines for each bar in a bar plot of base R?
- How to create the stacked bar plot using ggplot2 in R with labels on the plot?
- How to manually set the color of each bar in base R bar plot?
- How to create a horizontal bar plot using barplot function in R?
- How to set the horizontal alignment of text with JavaScript?
- How to set the horizontal alignment of an element with JavaScript?
- How to create a horizontal bar chart using ggplot2 with labels at inside end of the bars in R?
- How to create a bar graph using ggplot2 without horizontal gridlines and Y-axes labels in R?
- Java Program to set Horizontal Alignment of content in a JTextField
- How to make a broken horizontal bar plot in Matplotlib?
- How to rotate X-axis tick labels in Pandas bar plot?
- How to plot a Bar Chart with multiple labels in Matplotlib?
- How to plot the X-axis labels on the upper-side of the plot in R?
- How to change the axes labels using plot function in R?
- How to plot time series data with labels in R?

Advertisements