- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 text horizontally in a bar plot created by using ggplot2 in R?
To align the text horizontally in a bar plot created by using ggplot2 in R, we can follow the below steps −
First of all, create a data frame.
Then, create the bar plot using ggplot2 with text displayed on each bar.
After that, create the same bar plot with text aligned horizontally.
Create the data frame
Let’s create a data frame as shown below −
Category<-c("First","Second","Third") Count<-c(21,25,27) df<-data.frame(Category,Count) df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
Output
Category Count 1 First 21 2 Second 25 3 Third 27
Create the bar plot with text displayed on each bar
Using annotate function to create the bar plot with text displayed on each bar −
Category<-c("First","Second","Third") Count<-c(21,25,27) df<-data.frame(Category,Count) library(ggplot2) ggplot(df,aes(Category,Count))+geom_bar(stat="identity")+scale_y_continuous(limits=c (0,30))+annotate("text",x=1:3,y=c(21,25,27),label=c("I","II","III"))
Output
Create the bar plot with text displayed on each bar horizontally
Using annotate function to create the bar plot with text displayed on each bar horizontally by setting y values to Inf −
Category<-c("First","Second","Third") Count<-c(21,25,27) df<-data.frame(Category,Count) library(ggplot2) ggplot(df,aes(Category,Count))+geom_bar(stat="identity")+scale_y_continuous(limits=c (0,30))+annotate("text",x=1:3,y=Inf,vjust=1.5,label=c("I","II","III"))
Output
- Related Articles
- How to change legend values in a bar plot created by using ggplot2 in R?
- How to highlight text inside a plot created by ggplot2 using a box in R?
- How to make all text size same in a plot created by using ggplot2 in R?
- How to change the angle of annotated text in plot created by using ggplot2 in R?
- How to display text in bar plot in the middle using ggplot2 in R?
- How to change the thickness of the borders of bars in bar plot created by using ggplot2 in R?
- How to add a citation in a plot created by using ggplot2 in R?
- How to display regression slope using model in a plot created by ggplot2 in R?
- How to display regression intercept using model in a plot created by ggplot2 in R?
- How to extract data from a plot created by ggplot2 in R?
- How to add a horizontal line to the plot created by ggplot2 in R?
- How to create transparent bar plot using ggplot2 in R?
- How to create a bar plot with ggplot2 using stat_summary in R?
- How to write text outside plot using ggplot2 in R?
- How to reverse the bars of a bar plot a using ggplot2 in R?

Advertisements