Tutorials › Exam PA - Predictive Analytics › Aligned Histograms for Grouped Distributions

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

Aligned Histograms for Grouped Distributions

Use shared histogram breaks and scales to make fair, interpretable comparisons between group distributions.

Beginner 11 min read

What You'll Learn

  • Set identical bin boundaries and horizontal limits for each group histogram
  • Distinguish frequency counts from relative frequency and density heights
  • Choose a vertical scale that matches the comparison question and group sizes
  • Compare location, spread, skewness, and tails while noting binning limits
  • Use base R histograms with common breaks and aligned axes

Why Group Histograms Need Shared Scales

A histogram can reveal a distribution's concentration, spread, skewness, and tail behavior. But when two or more groups are shown separately, those features are only fairly comparable if the plots use the same bin boundaries and the same axes. Otherwise, a difference in the drawing may come from the plotting choices rather than from the data.

For example, if one group’s horizontal axis covers 0 to 50 and another’s covers only 10 to 30, equal-looking horizontal distances do not represent the same values. If the vertical axes use different limits, equally tall bars do not necessarily represent equal counts or proportions. Aligned histograms prevent these scale changes from disguising group differences. This visualization task is aligned with the SOA Exam PA (Predictive Analytics) syllabus.

Definition: A histogram divides a quantitative variable's range into intervals, or bins, and represents the observations in each bin with a bar. For a bin from \(b_j\) to \(b_{j+1}\), its width is \(w_j=b_{j+1}-b_j\). A set of common breaks means that every group uses the same bin boundaries. Aligned axes means that corresponding plots use the same horizontal and vertical scales.

For a histogram based on counts, the height of a bar is the number of observations in its bin. If group sizes differ, count heights mix two facts: the shape of the distribution and the number of observations collected. A density histogram instead scales each bar so that its area represents the proportion of observations in that bin.

Density histogram: If group \(g\) has \(n_g\) observations and \(c_{gj}\) fall in bin \(j\), the density height for that bin is \(c_{gj}/(n_gw_j)\). The bin's area is therefore \(c_{gj}/n_g\), the proportion of that group in the bin. With all observations included, the total bar area is 1.

Worked Example: Comparing Two Groups on Common Bins

Worked Example: Comparing Two Groups on Common Bins

A hypothetical service team compares resolution times, in days, for two groups of 14 cases each. Group A has times 2, 4, 7, 9, 11, 14, 16, 18, 22, 25, 28, 31, 37, and 42. Group B has times 5, 8, 12, 15, 19, 21, 24, 27, 29, 33, 35, 38, 44, and 48. Construct the bin counts using the common breaks 0, 10, 20, 30, 40, and 50, then state what the comparison can and cannot show.

Method. Use the five intervals \([0,10)\), \([10,20)\), \([20,30)\), \([30,40)\), and \([40,50]\). The final bin includes 50, so all endpoints in the stated data range are covered.

Group A. The first bin contains 2, 4, 7, and 9, giving a count of 4. The second contains 11, 14, 16, and 18, also giving 4. The third contains 22, 25, and 28, giving 3. The fourth contains 31 and 37, giving 2. The final bin contains 42, giving 1. The total is \(4+4+3+2+1=14\).

Group B. The first bin contains 5 and 8, giving 2. The second contains 12, 15, and 19, giving 3. The third contains 21, 24, 27, and 29, giving 4. The fourth contains 33, 35, and 38, giving 3. The final bin contains 44 and 48, giving 2. The total is \(2+3+4+3+2=14\).

Resolution time (days)Group A countGroup B count
0 to less than 1042
10 to less than 2043
20 to less than 3034
30 to less than 4023
40 to 50, inclusive12

Because the sample sizes are equal, counts are directly comparable. Group A has more cases in the first two bins, while Group B has more in the last three. The plotted distributions suggest that Group B tends to have longer resolution times and a slightly greater share in the upper tail. The common bins make the comparison meaningful; they do not make a histogram an exact summary of individual values. The first bin alone, for example, does not reveal whether its observations are near 0 or near 10.

Check. Each column sums to 14, matching the stated size of each group. This avoids accidentally omitting an observation or counting a boundary value in two bins.

Counts, Proportions, and Density Heights

The correct vertical scale depends on the question. Counts are appropriate when the number of observations in a bin matters, such as comparing workload volume across groups. For comparing distributional shape when sample sizes differ, use relative frequencies or density heights. Relative frequency gives the proportion in each bin; density height divides that proportion by bin width.

When all groups use equal-width bins, relative-frequency bar heights are proportional to density heights. Density is especially useful when bin widths differ, because the area, not the height alone, then represents the proportion. For an aligned comparison, use the same breaks across groups; changing widths from one group to another undermines a direct visual comparison.

Worked Example: Unequal Group Sizes and Density Heights

Two hypothetical groups have values in four common bins: \([0,10)\), \([10,20)\), \([20,30)\), and \([30,40]\). Group A has 8 observations with counts 4, 2, 1, and 1. Group B has 16 observations with counts 4, 6, 4, and 2. Find the density heights and compare the first bin fairly.

Method. Every bin has width \(w=10\). For each group, divide a bin's count by the group size and then by the bin width.

For Group A, the first height is \(4/(8\cdot10)=0.05\). The remaining heights are \(2/(8\cdot10)=0.025\), \(1/(8\cdot10)=0.0125\), and \(1/(8\cdot10)=0.0125\). Their areas are \(0.50\), \(0.25\), \(0.125\), and \(0.125\), which sum to 1.

For Group B, the heights are \(4/(16\cdot10)=0.025\), \(6/(16\cdot10)=0.0375\), \(4/(16\cdot10)=0.025\), and \(2/(16\cdot10)=0.0125\). Their areas are \(0.25\), \(0.375\), \(0.25\), and \(0.125\), which also sum to 1.

Both groups have a count of 4 in the first bin, but those counts represent different proportions: \(4/8=0.5\) for Group A and \(4/16=0.25\) for Group B. The density heights correctly show that the first bin contains twice the share of Group A, despite the equal raw counts. The density values have units of inverse days; multiplying a height by its 10-day bin width gives a dimensionless proportion.

Check. The area in each histogram is 1, as required. The comparison uses the same bin width and a common vertical scale, so its bar heights can be read against one another.

A Short Derivation: Why Histogram Area Is Proportion

The density-height formula is chosen so the bar area has a direct interpretation. Suppose \(c_{gj}\) of the \(n_g\) observations in group \(g\) fall in a bin of width \(w_j\). Set the height to the bin's relative frequency divided by its width. Then:

$$ \text{height}_{gj} = \frac{c_{gj}/n_g}{w_j} = \frac{c_{gj}}{n_gw_j}, \qquad \text{area}_{gj} = w_j\text{height}_{gj} = \frac{c_{gj}}{n_g}. $$

The final expression is exactly the fraction of that group's observations in the bin. Summing the areas across bins gives \(\sum_j c_{gj}/n_g=n_g/n_g=1\), provided every observation is represented in exactly one bin. This interpretation does not mean the bar height itself is a probability; it is the bar's area that gives the bin's proportion.

Constructing Aligned Histograms in R

In base R, supply the same numeric vector of breaks to every call to hist. For example, define breaks as 0, 10, 20, 30, 40, and 50 and use them for both groups. Choose the same horizontal limits, such as 0 to 50, for every panel. Decide whether to plot counts or densities, then set a common vertical range as well.

For density histograms, a practical approach is to calculate each histogram without drawing it, inspect the largest density value, and use a single vertical limit that accommodates every group. In R, the outline is: create each histogram with hist(x, breaks=breaks, right=FALSE, plot=FALSE); find the largest value among the resulting density vectors; then draw each with hist using probability=TRUE, the common breaks, and the same xlim and ylim. For count histograms, use the counts to set a shared vertical limit and draw with a count scale instead. A layout such as par(mfrow=c(1,2)) places the panels side by side; the plotting limits still need to be specified consistently.

With right=FALSE, bins follow a left-closed, right-open convention, such as \([0,10)\), so an observation on a shared boundary is assigned to one bin rather than counted twice. Check the chosen range and boundary convention: observations outside the plotted range should not silently disappear from the comparison.

Worked Example: Repairing a Misleading R Comparison

Two hypothetical groups each contain 20 service durations. On common bins from 0 to 50, their counts are Group C: 2, 8, 6, 3, 1 and Group D: 6, 6, 4, 3, 1. An initial side-by-side count plot gives each panel its own automatic vertical scale. Explain the problem and specify a corrected plot setup.

Method. First check the bin counts and sample sizes. Group C sums to \(2+8+6+3+1=20\); Group D sums to \(6+6+4+3+1=20\). Their largest bin counts are 8 and 6, respectively.

If the plotting software scales each panel separately, the tallest bar in each panel may reach the top of its own axis. That visual equality would conceal that the tallest bin has 8 observations in Group C and 6 in Group D. Use the same breaks, horizontal limits 0 to 50, and vertical count limits 0 to 9 for both plots. In base R, draw both histograms with breaks=c(0,10,20,30,40,50), xlim=c(0,50), and ylim=c(0,9), leaving the count scale in use.

If the goal changes to comparing proportions, the groups have equal sizes, so the density heights for the first bin are \(2/(20\cdot10)=0.01\) for C and \(6/(20\cdot10)=0.03\) for D. The second-bin heights are \(8/(20\cdot10)=0.04\) and \(6/(20\cdot10)=0.03\). The common density axis must be high enough for the largest height, 0.04. Do not put counts on one panel and density on the other.

Check. The count axis reaches 9, above both observed maxima, and the density axis accommodates 0.04. The identical bins and limits allow corresponding bar heights to be compared directly.

Reading Aligned Histograms Carefully

Once the scales are aligned, use the plots to compare several features, while describing them as visual evidence rather than exact numerical results.

  • Location: identify which group tends to occupy higher or lower bins. A shift in the concentration of bars suggests a location difference, but binning limits how precisely it can be read.
  • Spread: compare the horizontal range of the bulk of observations and how quickly bar heights taper away from the center. A single extreme observation can extend the full range without making the central spread large.
  • Skewness: look for asymmetry in the distribution's tail. A longer, thinner tail on the high-value side suggests right skew; a longer tail on the low-value side suggests left skew. The shared breaks help ensure that apparent asymmetry is not caused by unequal binning.
  • Tail behavior: compare the proportions in bins far from the main concentration. For unequal group sizes, use proportions or density areas rather than raw counts to compare how much of each group lies in the tail.

A histogram cannot show the exact position of observations within a bin, and changing the bin width can change the apparent shape. Keep the comparison focused on features that persist under a reasonable, common binning choice. Do not infer a cause for group differences from a descriptive plot alone.

Common Mistakes

  • Using different breaks. The same-looking bar can represent a different value range in each panel. Reuse exactly the same break vector.
  • Letting panels choose their own axes. Independently scaled axes can make unequal heights look equal, or make similar distributions look very different. Set common horizontal and vertical limits.
  • Comparing counts when group sizes differ. A larger group can have taller bars simply because it contains more observations. Use proportions or density heights when comparing distributional shape.
  • Reading density height as a proportion. Density height has units of inverse measurement units. Multiply by bin width to obtain the bin's proportion.
  • Ignoring endpoint rules or omitted observations. Specify how boundaries are handled and make sure all values fall within the displayed range.
Key takeaway: A fair group-histogram comparison uses identical breaks and aligned axes. Choose counts when volume is the target; choose relative frequency or density when comparing distributions, especially when group sizes differ. Describe visible patterns with appropriate caution about binning.

Check Your Understanding

For each problem, assume the stated bins include every observation and use the same boundaries for all groups. Focus on what the plotted comparison supports.

  1. Two groups each have 12 observations. On common bins, Group P has counts 3, 6, 2, 1 and Group Q has counts 1, 3, 5, 3. Which group has more observations in the final two bins, and what are the respective proportions? (Answer: Group Q; P has \(3/12=0.25\), while Q has \(8/12\approx0.6667\).)
  2. A group has 30 observations in a bin of width 5, and 9 observations fall in that bin. Find the density height and the bin's area. (Answer: Height \(9/(30\cdot5)=0.06\); area \(5\cdot0.06=0.30\).)
  3. Group A has 10 observations and Group B has 40. Both have 8 observations in the same bin. What are their bin proportions, and why are equal count-bar heights misleading for comparing shape? (Answer: A has \(8/10=0.80\); B has \(8/40=0.20\). Equal counts represent unequal proportions because the group sizes differ.)
  4. A histogram comparison uses breaks 0, 10, 20, 30 in one panel and 0, 5, 10, 15, 20, 25, 30 in another. State the main correction needed and one reason. (Answer: Use the same break vector for both; otherwise corresponding bars cover different ranges and their shapes are not directly comparable.)
  5. Two groups use common bins of width 4. A density bar has height 0.025 in one group and 0.05 in the other. Find the proportions in that bin and their difference. (Answer: The proportions are \(4(0.025)=0.10\) and \(4(0.05)=0.20\); the second exceeds the first by 0.10, or 10 percentage points.)
Clinical Biostats