Box Plot in R
A box plot (or box-and-whisker plot) is a standardized way of displaying the distribution of data
based on a five-number summary: minimum, first quartile (Q1), median (Q2), third quartile
(Q3), and maximum. This type of plot is useful for identifying outliers, comparing
distributions, and understanding the spread and skewness of the data.
Components of a Box Plot:
1. Box: The box itself represents the interquartile range (IQR), which is the distance
between the first quartile (Q1) and the third quartile (Q3). This range contains the
middle 50% of the data.
2. Median Line: A line inside the box shows the median (Q2), the 50th percentile of the data.
3. Whiskers: The lines extending from the box indicate variability outside the upper
and lower quartiles. They usually extend to 1.5 * IQR from Q1 and Q3. Any data
points outside of this range are considered outliers.
4. Outliers: Points that fall outside the whiskers are considered outliers and are
often plotted as individual dots.
Example of Creating a Box Plot in R
Here’s an example of how to create a box plot in R. Suppose we have a dataset containing
the ages of individuals:
# Sample data
ages <- c(23, 25, 29, 34, 35, 38, 40, 42, 43, 46, 47, 48, 50, 52, 58, 59, 60, 63, 68, 72, 7
5, 78, 80, 85)
# Basic box plot
boxplot(ages, main = "Box Plot of Ages", ylab = "Age", col = "lightblue")
# Adding gridlines for better readability
grid(nx = NULL, ny = NULL, col = "gray", lty = "dotted")
Explanation of the Code:
1. `boxplot(ages, ...)` creates a box plot for the `ages` data.
2. `main = "Box Plot of Ages"` sets the main title of the plot.
3. `ylab = "Age"` labels the y-axis as "Age."
4. `col = "lightblue"` fills the box with a light blue color.
5. `grid(...)` adds a dotted grid to make the plot easier to read.
1/2
This code will produce a box plot showing the spread of ages, with the box representing
the interquartile range, a line at the median, and whiskers extending to the minimum and
maximum values within the
1.5 * IQR range. Outliers will be shown as individual points outside the whiskers.
2/2