- 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 display the curve on the histogram using ggplot2 in R?
Mostly, we use histogram to understand the distribution of a variable but if we have an overlay line on the histogram that will make the chart smoother, thus understanding the variation will become easy. To display the curve on the histogram using ggplot2, we can make use of geom_density function in which the counts will be multiplied with the binwidth of the histogram so that the density line will be appropriately created.
Example
Consider the below data frame:
> x<-rpois(200,5) > df<-data.frame(x) > head(df,20)
Output
x 1 4 2 5 3 6 4 4 5 9 6 2 7 1 8 5 9 5 10 7 11 6 12 9 13 5 14 2 15 12 16 4 17 8 18 8 19 4 20 3
Loading ggplot2 package and creating a histogram:
Example
> library(ggplot2) > ggplot(df,aes(x))+geom_histogram(binwidth=1.1)
Output:
Creating the histogram with curve on the graph:
Example
> ggplot(df,aes(x))+geom_histogram(binwidth=1.1)+geom_density(aes(y=1.1*..count..))
Output:
- Related Articles
- How to display mean in a histogram using ggplot2 in R?
- How to display legend on top using ggplot2 in R?
- How to define the breaks for a histogram using ggplot2 in R?
- How to create a transparent histogram using ggplot2 in R?
- How to create a step histogram using ggplot2 in R?
- How to create histogram with varying binwidth using ggplot2 in R?
- How to set the X-axis labels in histogram using ggplot2 at the center in R?
- Change the outline color for histogram bars using ggplot2 in R.
- How to fill histogram bars using ggplot2 in R with different colors?
- How to display count on Y-axis for line chart using ggplot2 in R?
- How to display two equal signs in R using ggplot2?
- How to display text in bar plot in the middle using ggplot2 in R?
- How to display 0 at Y-axis using ggplot2 in R?
- How to display axes ticks and labels inside the plot using ggplot2 in R?
- How to create a histogram with Y-axis values as count using ggplot2 in R?

Advertisements