- 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 create bars with gap among them if there are more categories using ggplot2 in R?
When the number of categories is large in numbers for a variable and we want to create a bar plot then the display of the bar plot becomes a little ambiguous because the bars are plotted very close to each other. To make the bars clearly visible, we can reduce the width of the bars and set different colors for them to make them visually attractive.
geom_bar(width=0.2,color="red")
Consider the below data frame −
x<-sample(1:100,300,replace=TRUE) df<-data.frame(x)
Loading ggplot2 package and creating the bar plot of x −
Example
library(ggplot2) ggplot(df,aes(x))+geom_bar()
Output
Creating the same bar plot with gap between bars −
Example
ggplot(df,aes(x))+geom_bar(width=0.2,color="red")
Output
- Related Articles
- How to create multiple bar plots for varying categories with same width bars using ggplot2 in R?
- How to create scatterplot for categories with grey color palette using ggplot2 in R?
- How to create boxplot for categories with grey color palette using ggplot2 in R?
- How to create a stacked bar plot with vertical bars in R using ggplot2?
- How to create violin plot for categories with grey color palette using ggplot2 in R?
- How to create line chart for categories with grey color palette 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 create boxplot for categories with grey color palette in reverse order using ggplot2 in R?
- How to fill histogram bars using ggplot2 in R with different colors?
- How to create bar plot of means with error bars of standard deviations using ggplot2 in R?
- How to create violin plot for categories with grey color palette in reverse order using ggplot2 in R?
- How to create a horizontal bar chart using ggplot2 with labels at inside end of the bars in R?
- How to create stacked plot with density using ggplot2 in R?
- How to create a boxplot using ggplot2 with aes_string in R?
- How to create histogram with varying binwidth using ggplot2 in R?

Advertisements