Histogram Plots in R
Histogram Plots in R
Introduction
A histogram is a graphical representation of the frequency distribution of continuous numerical data. It divides the data into intervals called bins or classes and displays the number of observations in each interval using adjacent bars.
Unlike bar charts, the bars in a histogram touch each other, indicating that the data is continuous.
Histograms are useful for:
- Understanding data distribution.
- Identifying skewness.
- Detecting outliers.
- Examining spread and central tendency.
- Visualizing frequency distributions.
Syntax
hist(x,
breaks,
main,
xlab,
ylab,
col,
border,
freq)
where
- x : numeric vector.
- breaks : number of bins or class intervals.
- main : title.
- xlab : x-axis label.
- ylab : y-axis label.
- col : bar color.
- border : border color.
- freq : TRUE (frequency), FALSE (density).
Example 1: Simple Histogram
Suppose the marks of students are:
marks <- c(45,50,55,60,62,65,67,70,
72,75,78,80,82,85,90)
hist(marks)
This produces a histogram showing the frequency distribution of marks.
Example 2: Adding Title and Labels
hist(marks,
main="Distribution of Marks",
xlab="Marks",
ylab="Frequency")
Example 3: Changing Color
hist(marks,
col="lightblue")
Example 4: Changing Border Color
Example 5: Specifying Number of Bins
hist(marks,
breaks=5,
col="green")
Here, the data is divided into 5 intervals.
Example 6: Explicit Class Intervals
hist(marks,
breaks=c(40,50,60,70,80,90,100),
col="orange")
Intervals:
Example 7: Random Normal Data
x <- rnorm(1000)
hist(x,
col="skyblue",
main="Normal Distribution")
This approximates a bell-shaped curve.
Example 8: Density Histogram
x <- rnorm(1000)
hist(x,
probability=TRUE,
col="lightgreen")
or
hist(x,
freq=FALSE)
The y-axis represents probability density instead of frequency.
Example 9: Overlaying Density Curve
x <- rnorm(1000)
hist(x,
probability=TRUE,
col="lightblue")
lines(density(x),
col="red",
lwd=3)
This superimposes a smooth density curve.
Example 10: Overlaying Normal Curve
Example 11: Monthly Rainfall Distribution
rainfall <- c(120,150,90,80,70,200,
250,220,180,160,140,110)
hist(rainfall,
col="cyan",
main="Rainfall Distribution",
xlab="Rainfall")
Example 12: Histogram from Data Frame
student <- data.frame(
Marks=c(55,60,65,70,75,80,85,90,95)
)
hist(student$Marks,
col="pink")
Example 13: Add Mean Line
x <- rnorm(1000)
hist(x,
col="yellow")
abline(v=mean(x),
col="red",
lwd=3)
The vertical red line represents the mean.
Example 14: Compare Different Bin Sizes
5 bins
20 bins
More bins reveal finer details of the distribution.
Example 15: Histogram with Rug Plot
x <- rnorm(100)
hist(x,
col="lightblue")
rug(x)
The rug plot shows individual observations along the x-axis.
Example: Distribution of Student Marks
marks <- c(45,55,60,62,65,68,70,
72,74,76,78,80,82,85,88,90)
Histogram of Normal Distribution
x <- rnorm(1000)
hist(x,
breaks=30,
col="lightgreen",
main="Normal Distribution")
The histogram resembles a bell-shaped curve.
Important Parameters
| Parameter | Purpose |
|---|---|
breaks | Number of bins |
main | Title |
xlab | x-axis label |
ylab | y-axis label |
col | Color |
border | Border color |
freq | Frequency scale |
probability | Density scale |
Common Distributions
Normal Distribution
Symmetric bell shape.
***
*******
***********
*******
***
Right Skewed Distribution
Long tail to the right.
*******
*****
***
**
*
Left Skewed Distribution
Long tail to the left.
*
**
***
*****
*******
Uniform Distribution
Almost equal frequencies.
******
******
******
******
Bimodal Distribution
Two peaks.
*** ***
***** *****
*************
Difference Between Histogram and Bar Plot
| Histogram | Bar Plot |
|---|---|
| Continuous data | Categorical data |
| Bars touch each other | Bars separated |
| Shows distribution | Shows comparison |
| x-axis contains intervals | x-axis contains categories |
Uses hist() | Uses barplot() |
Applications of Histograms
- Examining marks distribution.
- Population studies.
- Rainfall analysis.
- Quality control.
- Machine learning data exploration.
- Statistical analysis.
- Detecting skewness and outliers.
- Understanding probability distributions.
Useful Functions with Histograms
Density Curve
lines(density(x))
Normal Curve
curve(dnorm(x,mean(x),sd(x)), add=TRUE)
Mean Line
abline(v=mean(x))
Rug Plot
rug(x)
Conclusion
A histogram is one of the most important statistical plots used to understand the distribution of continuous numerical data. The hist() function in R provides numerous options for customizing bins, colors, labels, and probability scales. Histograms are fundamental tools in statistics, machine learning, and exploratory data analysis.
Comments
Post a Comment