Box Plot in R
Box Plot in R
Introduction
A box plot (or box-and-whisker plot) is a graphical representation of the distribution of numerical data using five important statistics:
- Minimum value
- First Quartile (Q1)
- Median (Q2)
- Third Quartile (Q3)
- Maximum value
A box plot is useful for:
- Understanding data distribution.
- Comparing multiple datasets.
- Detecting outliers.
- Measuring spread and skewness.
- Identifying median and quartiles.
Components of a Box Plot
A box plot consists of:
Minimum Q1 Median Q3 Maximum
|-------|======|======|-------|
<----BOX---->
Box
Represents the interval from Q1 to Q3.
Median Line
The line inside the box represents the median.
Whiskers
Extend to the smallest and largest values that are not outliers.
Outliers
Displayed as individual points.
Five Number Summary
Suppose the data are:
x <- c(10,12,14,15,18,20,22,25,28)
Then
summary(x)
Output
Min. = 10
1st Qu.= 13
Median = 18
Mean = 18.22
3rd Qu.= 23.5
Max. = 28
These values determine the box plot.
Syntax
boxplot(x,
main,
xlab,
ylab,
col,
horizontal,
notch)
where
- x : data vector.
- main : title.
- xlab : x-axis label.
- ylab : y-axis label.
- col : box color.
- horizontal : horizontal box plot.
- notch : display confidence interval around median.
Example 1: Simple Box Plot
marks <- c(45,50,55,60,65,70,75,80,85,90)
boxplot(marks)
This produces a simple box plot.
Example 2: Add Title and Labels
boxplot(marks,
main="Student Marks",
ylab="Marks")
Example 3: Changing Color
Example 4: Horizontal Box Plot
Example 5: Displaying Notches
boxplot(marks,
notch=TRUE,
col="green")
The notch indicates an approximate confidence interval around the median.
Example 6: Detecting Outliers
x <- c(20,22,24,25,26,28,30,32,35,100)
boxplot(x,
col="pink")
The value 100 appears as an outlier.
Example 7: Multiple Box Plots
Suppose marks of three classes are:
Example 8: Label Multiple Box Plots
Example 9: Using Data Frames
student <- data.frame(
Marks=c(45,50,60,70,80,90)
)
boxplot(student$Marks,
col="orange")
Example 10: Box Plot Grouped by Factor
marks <- c(80,85,75,90,60,
70,95,88,76,65)
gender <- factor(c("Male","Male","Male","Male","Male",
"Female","Female","Female","Female","Female"))
boxplot(marks ~ gender,
col=c("blue","pink"),
main="Marks by Gender")
Here:
marks ~ gender
means "draw box plots of marks grouped by gender".
Example 11: Random Normal Data
Example 12: Comparing Departments
Example 13: Showing Means
boxplot(marks,
col="lightgreen")
points(1,
mean(marks),
col="red",
pch=18,
cex=2)
The red diamond represents the mean.
Example 14: Box Plot with Width Proportional to Sample Size
boxplot(classA,
classB,
classC,
varwidth=TRUE)
Example 15: Box Plot with Outline Disabled
boxplot(x,
outline=FALSE)
Outliers are not displayed.
Understanding Outliers
The Interquartile Range (IQR) is:
IQR = Q3 - Q1
Values outside:
Q1 - 1.5 × IQR
or
Q3 + 1.5 × IQR
are considered outliers.
Finding Outliers
boxplot.stats(x)$out
Example:
x <- c(20,22,25,28,30,35,100)
boxplot.stats(x)$out
boxplot(x)
Output
Important Parameters
| Parameter | Purpose |
|---|---|
main | Title |
xlab | X-axis label |
ylab | Y-axis label |
col | Color |
horizontal | Horizontal plot |
notch | Confidence interval |
outline | Display outliers |
varwidth | Variable width boxes |
names | Labels |
Useful Functions
Five Number Summary
summary(x)
Quartiles
quantile(x)
Interquartile Range
IQR(x)
Outliers
boxplot.stats(x)$out
Applications of Box Plots
- Comparing groups.
- Detecting outliers.
- Understanding skewness.
- Statistical analysis.
- Exploratory Data Analysis (EDA).
- Machine learning preprocessing.
- Quality control.
- Medical and biological studies.
Difference Between Histogram and Box Plot
| Histogram | Box Plot |
|---|---|
| Shows distribution shape | Shows five-number summary |
| Frequency-based | Quartile-based |
| Reveals modes | Reveals outliers |
| Bars touch each other | Box and whiskers |
Uses hist() | Uses boxplot() |
Difference Between Bar Plot and Box Plot
| Bar Plot | Box Plot |
|---|---|
| Displays categories | Displays numerical distributions |
| Heights represent values | Box represents quartiles |
| No outlier information | Shows outliers |
Uses barplot() | Uses boxplot() |
Conclusion
A box plot is one of the most powerful graphical tools in statistics for visualizing the spread, central tendency, and outliers of numerical data. The boxplot() function in R allows easy comparison of multiple datasets and plays an important role in exploratory data analysis, machine learning, and statistical modeling.
Comments
Post a Comment