Tutorials › Exam PA - Predictive Analytics › Side-By-Side Boxplots for Group Comparisons

Exam PA · Visualization and Anomaly Detection · Tutorial 140 of 966

Side-By-Side Boxplots for Group Comparisons

Use side-by-side boxplots to compare distributions across groups without mistaking visual overlap, outlier flags, or unequal sample sizes for stronger evidence than they provide.

Beginner 11 min read

What You'll Learn

  • Build side-by-side boxplots from a numeric response and a categorical grouping variable in R
  • Compare group medians and interquartile ranges on a common scale
  • Distinguish box overlap from whisker overlap and isolated flagged observations
  • Interpret group sample sizes and use variable-width boxes carefully
  • Avoid treating a boxplot difference or an outlier flag as proof of a causal or statistically significant effect

Why Compare Groups With Boxplots?

A single boxplot summarizes one quantitative variable. Placing several boxplots side by side extends that summary to groups, making it easier to compare typical values, middle-half spreads, and flagged observations on the same measurement scale. In an actuarial analysis, for example, a hypothetical analyst might compare claim amounts across coverage categories or processing channels. The plot can reveal differences worth investigating, but it does not explain their causes.

The key is to compare like with like. Each box represents the middle half of one group's observations, each line inside a box marks that group's median, and the whiskers summarize its non-flagged range under the plotting rule. A boxplot is a compact display, not a substitute for checking group sizes, data quality, or the underlying observations when the pattern matters.

Definition: A side-by-side boxplot displays separate boxplots for levels of a categorical grouping variable, using a shared quantitative axis. The median is the middle observation (or the average of the two middle observations). The interquartile range is \(IQR=Q_3-Q_1\), where \(Q_1\) and \(Q_3\) are the lower and upper quartiles. In a standard Tukey boxplot, whiskers extend to the most extreme observed values within the fences \(Q_1-1.5IQR\) and \(Q_3+1.5IQR\); observations beyond the fences are plotted individually.

Building the Plot in R

To make a comparison, identify the numeric variable to plot and the categorical variable that defines groups. In R's formula interface, a formula such as amount ~ channel says to plot the numeric variable amount separately for each level of channel. For a hypothetical data frame named claims, the basic command is boxplot(amount ~ channel, data = claims).

Before interpreting the result, check that the grouping variable has the intended levels and order. If the category is coded as text, R will generally order its levels alphabetically unless the variable is made a factor with a specified level order. The following code makes that order explicit and reports the group counts:

claims$channel <- factor(claims$channel, levels = c("Desk", "Mobile", "Agent"))
table(claims$channel, useNA = "ifany")
boxplot(amount ~ channel, data = claims, xlab = "Channel", ylab = "Amount ($)", main = "Amount by channel")

Each box uses only the available numeric observations for its group. Missing values can therefore change the effective group sizes; inspect them rather than assuming all records contribute. The count table is useful, but if the plotted variable itself contains missing values, also check the number of non-missing measurements within each group. A plot with a category present but few usable measurements may look as prominent as a plot based on many records.

The box and whisker definitions are consistent across groups, which is what makes their locations and lengths comparable. R's base plotting function uses Tukey-style hinges for the box edges; for some sample sizes, those hinges can differ slightly from quartiles calculated by other conventions. When exact numerical quartiles are required, state the convention used and do not assume every software summary will return identical quartiles.

1
Check the variables.
Confirm that the response is quantitative, the grouping variable is categorical, and the units are the same for every group.
2
Check the group counts.
Note how many usable observations contribute to each box. Unequal counts do not change the definition of a median or IQR, but they affect how much information each group's summary represents.
3
Compare centers and spreads.
Read the median lines and box widths on the common axis. Then compare whiskers and any individually plotted observations.
4
State the limits of the display.
Describe what the groups look like in the observed data. Do not infer causation or statistical significance from visual separation alone.

Reading Medians, Spreads, and Overlap

The median line gives a comparison of group centers that is less affected by extreme observations than the mean. A higher median indicates that the middle observation, or midpoint of the two middle observations, is higher in that group. It does not mean every observation in that group is higher. If a group has a noticeably longer box, its middle half is more spread out, as measured by its IQR. That is one useful comparison, but it does not describe all features of the distribution.

Box overlap means that the observed middle-half intervals share some values on the measurement scale. No box overlap means those particular intervals are separate. Neither observation, by itself, is a test of whether population distributions differ. Whiskers add information about the non-flagged ends of the plotted data, but their lengths can be asymmetric across a group. An isolated point beyond a whisker is flagged by the boxplot rule; the flag does not establish that the value is erroneous or impossible.

The comparison works because all groups share one axis and the same measurement units. If group \(j\) has quartiles \(Q_{1j}\) and \(Q_{3j}\), its box spans an interval of width \(Q_{3j}-Q_{1j}\). Subtracting the endpoints gives the width because the upper endpoint is \(Q_{3j}\) and the lower endpoint is \(Q_{1j}\). Thus, comparing box widths compares the groups' IQRs on a consistent scale. The calculation describes the middle half, not the entire range or the variability of every individual observation.

$$ \operatorname{IQR}_j=Q_{3j}-Q_{1j} $$

Similarly, if two boxes span \([Q_{1A},Q_{3A}]\) and \([Q_{1B},Q_{3B}]\), their intervals overlap precisely when the larger lower endpoint is no greater than the smaller upper endpoint:

$$ \max(Q_{1A},Q_{1B})\leq \min(Q_{3A},Q_{3B}) $$

This is a statement about the displayed middle-half intervals only. It does not say how many observations overlap, because a boxplot does not show the individual values inside the boxes.

Worked Example: Comparing Typical Amounts and Middle-Half Spread

Two hypothetical service channels have boxplot summaries, in dollars, as follows. Channel Desk has median \(42\), \(Q_1=36\), \(Q_3=50\), and whisker endpoints \(30\) and \(58\). Channel Mobile has median \(47\), \(Q_1=43\), \(Q_3=55\), and whisker endpoints \(39\) and \(64\). Compare their medians, IQRs, box overlap, and displayed whisker ranges. Assume no observations are plotted beyond the whiskers.

Method. Read the center from each median line, calculate each IQR by subtracting the lower quartile from the upper quartile, and compare the two box intervals and whisker endpoints.

Median comparison. Mobile's median exceeds Desk's by \(47-42=5\) dollars. This describes a difference in the centers of the observed groups; it does not imply that every Mobile amount exceeds every Desk amount.

Spread comparison. Desk's IQR is \(50-36=14\) dollars. Mobile's IQR is \(55-43=12\) dollars. Desk therefore has the wider middle-half interval by \(14-12=2\) dollars.

Box overlap. The larger lower endpoint is \(\max(36,43)=43\); the smaller upper endpoint is \(\min(50,55)=50\). Since \(43\leq50\), the boxes overlap from \(43\) to \(50\) dollars.

Whiskers and conclusion. The displayed ranges are \(30\) to \(58\) dollars for Desk and \(39\) to \(64\) dollars for Mobile. They overlap from \(39\) to \(58\) dollars. In these observed data, Mobile has the higher median and Desk the wider IQR. Both the boxes and whisker ranges overlap, so the groups are not separated across their displayed values. Boxplot summaries alone do not establish whether a difference is statistically significant or why it occurred.

Common mistake avoided: Calling the groups “non-overlapping” because Mobile's median is above Desk's would confuse a comparison of median lines with a comparison of the full boxes or whisker ranges.

Group Size and Variable-Width Boxes

Standard side-by-side boxplots usually give each group's box the same width, even when the group sizes differ greatly. Equal width is convenient, but it does not mean equal sample size or equal precision. Always read or calculate the counts separately. A box calculated from a small group can be more sensitive to individual observations than one calculated from a large group.

R can scale box widths according to group size with varwidth = TRUE. The widths are then proportional to the square root of the group sizes, scaled for display; they are not proportional to the sizes themselves. Variable width makes relative group size visible, but it does not change the medians, quartiles, whiskers, or flagged points. The data axis remains the basis for comparing those summaries.

Worked Example: Unequal Counts With Overlapping Boxes

A hypothetical review-time study has \(40\) usable observations for Desk and \(10\) for Mobile. The plotted summaries are: Desk has median \(18\) minutes, \(Q_1=12\), \(Q_3=26\); Mobile has median \(20\) minutes, \(Q_1=16\), \(Q_3=24\). Compare their centers, IQRs, box overlap, and group sizes. State what would change if the plot used variable-width boxes.

Method. Calculate each IQR, compare the quartile intervals, then report the counts separately from the distribution summaries.

Centers. The median difference is \(20-18=2\) minutes, with Mobile having the higher observed median.

Spreads. Desk's IQR is \(26-12=14\) minutes. Mobile's IQR is \(24-16=8\) minutes. Desk's middle half spans \(14-8=6\) more minutes.

Overlap. The larger lower quartile is \(\max(12,16)=16\), and the smaller upper quartile is \(\min(26,24)=24\). Since \(16\leq24\), the boxes overlap from \(16\) to \(24\) minutes.

Counts and width. Desk has \(40\) observations and Mobile has \(10\), so Desk has four times as many. With variable widths, the ratio of displayed widths is \(\sqrt{40}/\sqrt{10}=\sqrt{4}=2\), so Desk's box would be twice as wide. The centers, IQRs, and box overlap would remain unchanged. The unequal counts should temper a direct visual comparison, especially for the smaller group, but they do not change the stated arithmetic.

Common mistake avoided: Reading the wider variable-width box as evidence of greater data spread. In this plot, width encodes sample size; vertical box height encodes the IQR.

Flagged Values Across Groups

A flagged observation is plotted separately because it lies beyond the applicable \(1.5IQR\) fence for its own group. The calculation is group-specific: each group has its own quartiles, IQR, and fences. Therefore, the same numerical amount can be flagged in one group and not flagged in another. Check the value against its own group's fence rather than judging it by its distance from another group's median.

A flagged point is a prompt to investigate. It may reflect a valid unusual observation, a data-entry or measurement problem, or a feature of the population. A boxplot does not distinguish among these explanations. Record the finding as an exploratory flag, then check the source data and context before deciding what to do with it.

Worked Example: Applying Each Group's Own Fences

For two hypothetical claim categories, a boxplot gives the following summaries. Category P has \(Q_1=100\), \(Q_3=160\), and an upper whisker endpoint of \(220\); observed values beyond the whisker are \(310\). Category Q has \(Q_1=120\), \(Q_3=150\), and an upper whisker endpoint of \(180\); observed values beyond the whisker are \(205\) and \(260\). Use the upper \(1.5IQR\) fences to verify which listed observations are flagged.

Method. For each category, calculate \(IQR=Q_3-Q_1\), then calculate the upper fence \(Q_3+1.5IQR\). Compare each listed upper observation with that category's fence.

Category P. Its IQR is \(160-100=60\). Its upper fence is \(160+1.5(60)=160+90=250\). The whisker endpoint \(220\) is within the fence. The listed observation \(310\) exceeds \(250\), so it is flagged.

Category Q. Its IQR is \(150-120=30\). Its upper fence is \(150+1.5(30)=150+45=195\). The whisker endpoint \(180\) is within the fence. Both listed observations exceed the fence: \(205>195\) and \(260>195\). Both are flagged.

Conclusion and check. Category P has one listed flagged value, \(310\); Category Q has two, \(205\) and \(260\). In each category, the whisker endpoint is inside its own upper fence, as required by the whisker rule. The flags do not, on their own, show that any of these observations is invalid.

Common mistake avoided: Applying Category P's fence to Category Q. The fences are \(250\) and \(195\), respectively, because the groups have different IQRs.

Common Interpretation Traps

  • Equating overlap with no difference. Overlapping boxes do not prove the group distributions are identical; non-overlapping boxes do not, by themselves, prove a statistically significant difference.
  • Comparing box height with box width. The vertical dimension encodes the numeric values. Horizontal widths are generally decorative unless variable-width boxes are requested.
  • Treating whiskers as fences. Whiskers end at the most extreme observed values within the fences; they do not necessarily end at the fence values.
  • Calling every flagged point an error. A flag identifies a value relative to a rule, not its cause or validity.
  • Ignoring different counts. Equal-width boxes do not communicate sample size. Report group counts and consider how a small group limits the stability of its summary.
  • Making a causal claim from a grouped plot. A difference between observed groups can reflect other features of how those groups were formed. The plot alone does not isolate a cause.
Key takeaway: Compare median lines for observed centers, box heights for IQRs, and whiskers and isolated points for the plotted tails. Check each group's count and interpret overlap or flags as descriptive evidence, not as a test or a causal explanation.

Check Your Understanding

For each problem, show the comparisons needed before deciding what the plot supports.

  1. Group A has \(Q_1=8\), median \(12\), and \(Q_3=18\). Group B has \(Q_1=15\), median \(17\), and \(Q_3=23\). Find both IQRs, decide whether the boxes overlap, and give the median difference. (Answer: IQRs are \(10\) and \(8\); boxes overlap from \(15\) to \(18\); Group B's median is \(5\) units higher.)
  2. In a hypothetical plot, Group Red has \(Q_1=30\), \(Q_3=42\), and an upper whisker at \(55\). Group Blue has \(Q_1=35\), \(Q_3=40\), and an upper whisker at \(49\). Compare the IQRs and determine whether the boxes and whisker ranges overlap. (Answer: IQRs are \(12\) and \(5\); the boxes overlap from \(35\) to \(40\); the whisker ranges overlap from \(35\) to \(49\), assuming their lower whiskers are below \(49\).)
  3. Two service groups have sizes \(25\) and \(100\). If variable-width boxes are used with widths proportional to the square root of sample size, what is the larger-to-smaller width ratio? Does that setting change the medians? (Answer: The ratio is \(\sqrt{100}/\sqrt{25}=2\); no, variable width changes displayed width, not the medians.)
  4. Group M has \(Q_1=50\) and \(Q_3=90\). An observed value is \(155\). Calculate the upper fence and decide whether the value is flagged. (Answer: \(IQR=40\), upper fence \(=90+1.5(40)=150\); \(155\) is flagged.)
  5. A plot shows a flagged value in a group with only \(6\) usable observations and no flagged values in a group with \(80\). Give two cautions before interpreting this difference. (Answer: The flagged value is not automatically an error and should be checked in context; the small group has fewer observations, so its summary may be more sensitive to individual values. The difference does not establish a causal or statistically significant effect.)
Clinical Biostats