- 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 create a bar plot using ggplot2 with percentage on Y-axis in R?
Mostly, the bar plot is created with frequency or count on the Y-axis in any way, whether it is manual or by using any software or programming language but sometimes we want to use percentages. It can be done by using scales package in R, that gives us the option labels=percent_format() to change the labels to percentage.
Example
Consider the below data frame −
> x<-sample(1:4,20,replace=TRUE) > df<-data.frame(x) > df
Output
x 1 2 2 3 3 3 4 1 5 2 6 4 7 4 8 4 9 2 10 3 11 3 12 4 13 3 14 4 15 4 16 1 17 3 18 1 19 4 20 1
Loading ggplot2 package and creating bar plot −
> library(ggplot2) > ggplot(df,aes(x))+geom_bar()
Output
Loading scales package and creating the bar plot with percentage on Y-axis −
Example
> library(scales) > ggplot(df,aes(x))+geom_bar(aes(y=(..count..)/sum(..count..)))+scale_y_continuous(labels =percent_format())
Output
- Related Articles
- Create stacked bar chart with percentages on Y-axis using ggplot2 in R.
- How to change the Y-axis values in a bar plot using ggplot2 in R?
- How to create a bar plot with ggplot2 using stat_summary in R?
- How to create the stacked bar plot using ggplot2 in R with labels on the plot?
- How to create bar plot with log values using ggplot2 in R?
- How to create bar plot with gradient colors using ggplot2 in R?
- How to create transparent bar plot using ggplot2 in R?
- How to create a bar plot using ggplot2 with one bar having black border in R?
- How to create a stacked bar plot with vertical bars in R using ggplot2?
- How to create cumulative sum chart with count on Y-axis in R using ggplot2?
- How to create bar plot in base R with different limits for Y-axis?
- How to create a bar plot in R with label of bars on top of the bars using ggplot2?
- How to create a histogram with Y-axis values as count using ggplot2 in R?
- Create ggplot2 graph with reversed Y-axis and X-axis on top in R.
- How to create bar plot using ggplot2 with structure data frame?

Advertisements