- 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 manage top and bottom spaces of a bar plot using ggplot2 in R?
A bar plot is one of the most commonly used plots for categorical data and it can be easily done in R with the help of ggplot2. When we create a bar plot using ggplot2, there exists some space between bars and the X-axis and the largest bar and top area of the plot. This can be reduced or increased by using scale_y_continuous function.
Example
Consider the below data frame −
x <-c("X1","X2","X3","X4") Frequency <-c(41,56,45,67) df<-data.frame(x,Frequency) library(ggplot2)
Creating a simple bar plot −
ggplot(df,aes(x,Frequency))+geom_bar(stat="identity")
Output
Creating a simple bar plot with zero space between bars and X-axis and a decreased top area −
Example
ggplot(df,aes(x,Frequency))+geom_bar(stat="identity")+ + scale_y_continuous(expand=c(0,0),limits=c(0,67.5))
Output
Creating a simple bar plot with zero space between bars and X-axis and an increased top area −
Example
ggplot(df,aes(x,Frequency))+geom_bar(stat="identity")+ + scale_y_continuous(expand=c(0,0),limits=c(0,75))
Output
- Related Articles
- How to create transparent bar plot 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 a bar plot with ggplot2 using stat_summary in R?
- How to reverse the bars of a bar plot a using ggplot2 in R?
- How to change the color of bars of a 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 bar plot with log values using ggplot2 in R?
- How to increase the space between bars of a bar plot using ggplot2 in R?
- How to create a stacked bar plot with vertical bars in R using ggplot2?
- How to create bar plot with gradient colors using ggplot2 in R?\n
- How to change legend values in a bar plot created by using ggplot2 in R?
- How to change the Y-axis values in a bar plot using ggplot2 in R?
- How to display text in bar plot in the middle using ggplot2 in R?
- How to create the stacked bar plot using ggplot2 in R with labels on the plot?
- How to create a bar plot using ggplot2 with percentage on Y-axis in R?

Advertisements