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:

  1. Minimum value
  2. First Quartile (Q1)
  3. Median (Q2)
  4. Third Quartile (Q3)
  5. 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

boxplot(marks,
col="lightblue")




Example 4: Horizontal Box Plot

boxplot(marks,
horizontal=TRUE,
col="yellow")



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:

classA <- c(60,65,70,75,80)
classB <- c(55,60,68,72,85)
classC <- c(50,58,66,78,90)

boxplot(classA,
classB,
classC)




Example 8: Label Multiple Box Plots

boxplot(classA,
classB,
classC,
names=c("A","B","C"),
col=c("red","green","blue"),
main="Class Comparison")




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

x <- rnorm(100)

boxplot(x,
col="cyan")



Example 12: Comparing Departments

cse <- c(80,85,90,88,92)
ece <- c(70,75,78,82,85)
me <- c(65,70,72,76,80)

boxplot(cse,ece,me,
names=c("CSE","ECE","ME"),
col=rainbow(3))




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

100




Important Parameters

ParameterPurpose
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

HistogramBox 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 PlotBox 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

Popular posts from this blog

Statistical Methods Lab ( R Language) PCCBL308 Semester 3 KTU BTech CB and CU 2024 Scheme - Dr Binu V P

Programs in R - using control statements - Assignment 2

Basic R Programs to Try - Assignment 1