
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 histogram with varying binwidth using ggplot2 in R?
To create histogram with varying binwidth using ggplot2 in R, we can follow the below steps −
First of all, create a data frame.
Then, use geom_histogram function with breaks argument having varying differences.
Example
Create the data frame
Let’s create a data frame as shown below −
x<-rnorm(25) df<-data.frame(x) df
Output
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x 1 -0.51481721 2 -0.15054169 3 -0.47070913 4 0.15570184 5 0.30182716 6 0.27027150 7 -1.13936013 8 1.78922492 9 -1.80448026 10 -0.50952246 11 2.06501828 12 0.18205980 13 -1.09307457 14 -0.69088644 15 -0.01556974 16 -0.18344297 17 0.56017703 18 0.90763553 19 1.63306307 20 0.83391193 21 0.09490660 22 -1.42484122 23 -0.52230854 24 0.77694351 25 -2.17216831
Create histogram with varying binwidth using ggplot2
Using geom_histogram function with breaks argument having varying differences to createthe histogram having different binwidths as shown below −
x<-rnorm(25) df<-data.frame(x) library(ggplot2) ggplot(df,aes(x))+geom_histogram(breaks=c(-3,-2,-1,2))
Output
- Related Questions & Answers
- How to create a transparent histogram using ggplot2 in R?
- How to create a step histogram using ggplot2 in R?
- How to create multiple bar plots for varying categories with same width bars using ggplot2 in R?
- How to create a histogram with Y-axis values as count using ggplot2 in R?
- How to fill histogram bars using ggplot2 in R with different colors?
- How to display mean in a histogram using ggplot2 in R?
- How to deal with warning message `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. in R while creating a histogram?
- How to create a histogram using weights in R?
- How to create histogram with relative frequency in R?
- How to display the curve on the histogram using ggplot2 in R?
- How to define the breaks for a histogram using ggplot2 in R?
- How to create varying width bar chart using barplot function 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 horizontal histogram in R?
Advertisements