- 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 define the breaks for a histogram using ggplot2 in R?
To manually define the breaks for a histogram using ggplot2, we can use breaks argument in the geom_histogram function. While creating the number of breaks we must be careful about the starting point and the difference between values for breaks. This will define the number of bars for histogram so it should be taken seriously and should be according to the distribution of the data.
Consider the below data frame −
Example
x<-rnorm(5000,525,30.24) df<-data.frame(x) head(df,20)
Output
x 1 524.0964 2 490.5952 3 518.6243 4 544.0018 5 480.8306 6 461.2975 7 464.0870 8 516.5240 9 517.3936 10 506.0277 11 480.3274 12 505.6415 13 440.9464 14 532.0064 15 482.7700 16 517.3608 17 536.5500 18 518.7121 19 598.5776 20 506.3834
Loading ggplot2 package and creating a histogram of x −
Example
library(ggplot2) ggplot(df,aes(x))+geom_histogram(bins=30)
Output
Creating histogram of x with manually defined breaks −
Example
ggplot(df,aes(x))+geom_histogram(bins=30,breaks=c(400,420,440,460,480,500,520,540,560))
Output
- Related Articles
- How to define the number of breaks in base R histogram?
- How to create a transparent histogram using ggplot2 in R?
- How to create a step histogram using ggplot2 in R?
- How to display mean in a histogram using ggplot2 in R?
- Change the outline color for histogram bars using ggplot2 in R.
- How to display the curve on the histogram using ggplot2 in R?
- How to create histogram with varying binwidth using ggplot2 in R?
- How to fill histogram bars using ggplot2 in R with different colors?
- How to create a histogram with Y-axis values as count using ggplot2 in R?
- How to set the X-axis labels in histogram using ggplot2 at the center in R?
- How to create a histogram using weights in R?
- How to create a histogram for uniform data in R?
- How to create boxplot for list elements using ggplot2 in R?
- How to change legend for multiple histograms using ggplot2 in R?
- How to create a point chart for cumulative sums using ggplot2 in R?

Advertisements