Slides 📊

Open the Chapter 2 slides in a new tab

The original PowerPoint slides remain available as a legacy download:

Download Chapter 2 slides (PPTX)

2.3. Tools for Numerical (Quantitative) Data

Numerical data offers richer visualization possibilities than categorical data because it contains information about the distances between values. We are now interested in the overall shape of the distribution, where the numbers are clustered, and how far they spread. A histogram answers all three at a glance.

Road Map 🧭

  • Visualize numerical variables with histograms.

  • Understand the impact of choosing different numbers of classes/bins for a histogram.

  • Recognize the difference between a bar graph and a histogram.

2.3.1. Building Your First Histogram

A histogram first divides the number line spanning the range of a variable into adjacent intervals of equal width. Over each interval, a bar is drawn whose height equals the count (or relative frequency) of the datapoints belonging to the interval. Each interval is called a bin, and it is up to the user to select how many bins are used in a histogram.

Let us begin our exploration of histograms with a simple example based on a built-in data set in R, InsectSprays. First import the data set by running:

library(ggplot2)
data(InsectSprays)
View(InsectSprays)

The code will open a separate window of the complete table. The first few rows are:

(index)

count

spray

1

10

A

2

7

A

\(\vdots\)

\(\vdots\)

\(\vdots\)

This data set reports the insect counts in agricultural experimental units treated with six different insecticides. There are 72 rows in the data set.

Use ggplot2 to print a histogram:

# rule of thumb: max(round(sqrt(n)) + 2,5) bins
n_obs  <- nrow(InsectSprays) #72
n_bins <- max(round(sqrt(n_obs)) + 2,5)

ggplot(InsectSprays, aes(x = count)) +
  geom_histogram(bins = n_bins, colour = "black", fill = "skyblue", linewidth = 1.2) +
  labs(title = "Distribution of insect counts (Beall, 1942)",
       x = "Number of insects", y = "Density")
A histogram of InsectSprays dataset

Fig. 2.13 Histogram of InsectSprays dataset

Describe this figure in detail

Histogram titled “Distribution of insect counts (Beall, 1942)”, with sky-blue bars. The horizontal axis is Number of insects, from about -1.5 to 27.5, with ticks at 0, 10 and 20; the vertical axis is Frequency, from 0 to 16. There are 10 bars of equal width, about 2.9 insects each; the first bar is centered at 0 and the last at about 26.

From left to right the bar heights are 8, 16, 13, 4, 9, 6, 6, 5, 3 and 2, which add up to the 72 rows of the data set. The tallest bar is the second, centered near 3 insects. After a drop to 4 in the fourth bar, centered near 9 insects, the height rises to 9 in the fifth bar and then decreases steadily toward the right.

We make a number of observations:

  • Each bar represents the number of observations falling within a range of insect counts.

  • This histogram uses 10 bins. This means that the histogram divided the data’s range into 10 intervals of equal length.

  • The histogram does a good job of describing the overall distribution of the data, while not being overly detailed.

2.3.2. Determining the Number of Bins

How Does the Bin Count Change a Histogram?

Although we are free to choose any number of bins for a histogram, this choice significantly affects the quality of data representation. To illustrate this impact, we plot the same data set four times using different bin counts. The data used in this example can be downloaded here: furnace.csv (if a new browser tab opens instead, press Ctrl/Cmd+S to save the file).

library(ggplot2)
furnace <- read.csv("furnace.csv")   # replace "furnace.csv" with your file location

for (bins in c(6, 10, 15, 30)) {
  p <- ggplot(furnace, aes(Consumption)) +
    geom_histogram(bins = bins, colour = "black", fill = "darkgreen") +
    labs(title = paste(bins, "bins"), x = "BTU", y = "Frequency")
  print(p)
}
Furnace BTU histograms: 6, 10, 15 and 30 bins

Fig. 2.14 Furnace BTU histograms with different numbers of bins

Describe this figure in detail

Four histograms of the same furnace energy consumption data, side by side, titled “6 bins”, “10 bins”, “15 bins” and “30 bins”. Each has BTU on the horizontal axis, from about 2 to 20, and Frequency on the vertical axis. The frequency scale gets shorter as the bins get narrower.

6 bins: bars about 3 BTU wide, from about 1.5 to 19.5. Heights from left to right are about 2, 17, 39, 24, 6 and 2. The tallest bar spans about 7.5 to 10.5 BTU.

10 bins: bars from about 2.5 to 19.5 BTU, with heights of about 2, 3, 14, 17, 23, 15, 11, 3, 1 and 1. The tallest bar spans about 9.3 to 11 BTU, with four bars to its left and five to its right.

15 bins: heights of about 1, 1, 4, 7, 11, 12, 18, 10, 7, 11, 3, 2, 2, 0 and 1. The tallest bar, 18, spans about 9.3 to 10.4 BTU. A second, lower peak of 11 appears at about 12.6 to 13.7 BTU, after a drop to 7. An empty bin at about 16.7 to 17.8 BTU separates the last bar from the rest.

30 bins: the vertical axis runs only from 0 to 10. Neighboring bars jump up and down. The two tallest are 10 near 10 BTU and 9 near 8.4 BTU, with a bar of only 2 between them near 8.9 BTU. Empty bins break up both ends: single-observation bars near 3.2 and 4.2 BTU on the left, and near 16.8 and 18.3 BTU on the right, stand apart with empty bins between them.

We observe a clear trend in Fig. 2.14:

  1. 6 bins: Oversimplifies the data, hiding important features

  2. 10 bins: Balances detail and clarity, revealing the general, slightly right-skewed shape

  3. 15 bins: Shows more granular structure and begins to display some potentially random fluctuation

  4. 30 bins: Too detailed, resulting in a jagged appearance dominated by sampling variability

In this case, ten to fifteen bins reveal the overall trend without drowning the eye in high-frequency jitter.

The Rule of Thumb

Bin count is a Goldilocks choice: if fewer than necessary, the histogram hides detail, and if too many, it is difficult to observe trends due to noise. In the meantime, there is no single correct choice. We usually test a few candidate values in a reasonable range, then make a final choice based on how the resulting histograms look.

The rule of thumb for a starting point (not the final correct answer) is as follows:

  1. Find how many rows there are in the data set. Denote the row count with \(n\).

  2. Compute

    \[b = \operatorname{max}(\operatorname{round}(\sqrt{n})+ 2,5).\]

    Here, \(\operatorname{round}(\cdot)\) denotes rounding to the nearest integer.

  3. If \(b > 30\), start your test with a value between \(20\) and \(30\). That is, a number of bins over the \(20-30\) range is often too high, even if the data set is fairly large.

  4. If \(b \leq 30\), start your tests at \(b\).

  5. Find the “best-looking” histogram among your candidates. You may use the furnace example (Fig. 2.14) as a guideline.

Example 💡: Do the Previous Examples Follow the Rule of Thumb?

The InsectSprays data set has \(n=72\) rows. By the rule of thumb, \(b=\operatorname{max}(\operatorname{round}(\sqrt{72})+2,5) = 10\), which is exactly how many bins were used in Fig. 2.13.

There are \(n=90\) observations in the furnace dataset (Fig. 2.14). This gives \(b=\operatorname{max}(\operatorname{round}(\sqrt{90}) + 2,5) = 11\). This result agrees with our previous conclusion that using 10 or 15 bins best represent the data.

2.3.3. Enhancing Histograms – Density & Normal Overlay

Normality

We will learn in later chapters that normality is a desirable characteristic in a data set which allows us to use a range of useful theoretical tools. Normality is usually characterized by a bell-shape in the data distribution: unimodal, symmetric, and with tails that taper off at an appropriate rate.

Assessing Normality

Whenever we graph a histogram, it is one of our interests to read how normal the data is. For this purpose, we use two additional assisting tools.

  1. A smooth curve outlining the data’s shape, also called the kernel density (red)

  2. A true normal curve that shares the same center and width as the data (blue)

Download furnace.csv from the course data page.

In the code below, replace "furnace.csv" with the path to your downloaded file. If you saved the file in your R working directory, you can leave the filename as written.

library(ggplot2)
furnace <- read.csv("furnace.csv")   # replace with your path
xbar <- mean(furnace$Consumption)
s    <- sd(furnace$Consumption)
bins <- round(sqrt(nrow(furnace))) + 2

ggplot(furnace, aes(Consumption)) +
  geom_histogram(aes(y = after_stat(density)), bins = bins,
                fill = "lightblue", colour = "black") +
  geom_density(colour = "red",  linewidth = 1.2) +
  stat_function(fun = dnorm, args = list(mean = xbar, sd = s),
                colour = "blue", linewidth = 1.2) +
  labs(title = "Residential furnace energy consumption",
       y = "Density", x = "BTU")
Histogram with kernel density (red) and normal curve (blue)

Fig. 2.15 Histogram with a kernel density (red) and a normal curve (blue)

Describe this figure in detail

Histogram titled “Residential furnace energy consumption”, drawn on a density scale with light blue bars. The horizontal axis is BTU, from about 2.5 to 19, with ticks at 5, 10 and 15; the vertical axis is Density, from 0 to about 0.14. From left to right the 11 bar heights are about 0.007, 0.015, 0.065, 0.11, 0.137, 0.131, 0.087, 0.065, 0.015, 0.015 and 0.007 on the density axis. The tallest bar spans about 8.4 to 10 BTU.

The red curve, the kernel density estimate, peaks at about 0.134 near 9.6 BTU. The blue curve, a normal density with the sample mean and standard deviation, peaks at about 0.138 near 10 BTU. Both curves run from about 3 to 18 BTU. The red curve lies above the blue one between about 5.5 and 9.7 BTU. It drops below the blue curve between about 9.7 and 13 BTU, with a slight shoulder near 12.3 BTU. The two curves nearly coincide from about 13 to 14.5 BTU, and the red curve lies just above the blue one beyond about 14.5 BTU.

Based on how close the two curves are, we assess whether the data is sufficiently normal or deviates from it. Although they are not part of a histogram by definition, we will always use them in this course, whenever a histogram is drawn.

2.3.4. Bar Graph or Histogram?

For discrete numerical data with few unique values, a bar graph may not present any major visual difference from a histogram. In some cases, the data can even be treated as an ordinal categorical data and displayed with a bar graph. Fig. 2.16 is one such example:

Bar graph of number of children per family

Fig. 2.16 Numerical variable with a small set of possible values and its bar graph

Describe this figure in detail

Two parts side by side. On the left is a frequency table with columns Kids, # of Couples and Rel. Freq. Rows: 0 kids, 11 couples, 0.11; 1, 22, 0.22; 2, 24, 0.24; 3, 30, 0.30; 4, 11, 0.11; 5, 1, 0.01; 6, 0, 0.00; 7, 1, 0.01. The total row gives 100 couples and 1.00.

On the right is a bar graph of the same relative frequencies. The horizontal axis is Number Kids, with one bar centered on each whole number from 0 to 7; the vertical axis is Relative Frequency, from 0 to 0.3. The bar heights match the table: 0.11, 0.22, 0.24, 0.30, 0.11, 0.01, 0 and 0.01. The bar at 6 has zero height and shows only as a line on the axis. Narrow gaps separate the bars.

In general, however, bar graphs and histograms differ clearly in their usage and appearance.

Comparison of Bar Graphs and Histograms

Feature

Bar graphs

Histograms

Variable type

Categorical variables; numerical variables with few possible values are sometimes converted to a categorical variable

Numerical variables, especially with many different possible values

Marks on \(x\)-axis

All points contributing to a bar has the same value. The center of the bar is marked with this value.

All points contributing to a bar belongs to the same interval but may have different values. Either,

  • The two ends of each bar is marked, or

  • The bars are positioned along a continuous number line.

Gaps between bars

There are no in-between values among categories. There may be gaps between the bars to reflect this.

The intervals are always adjacent. Gaps indicate absence of data points in the corresponding interval(s).

2.3.5. Bringing It All Together

Key Takeaways 📝

  1. Histograms turn numbers into a shape.

  2. Use \(\max(\operatorname{round}(\sqrt{n})+2,\; 5)\) bins as a starting point, then adjust by eye.

  3. Overlay a normal curve and a smooth trend line to easily assess deviation from normality.

2.3.6. Exercises

These exercises develop your skills in visualizing numerical data using histograms, selecting appropriate bin counts, and assessing normality.

Exercise 1: Determining the Number of Bins

For each dataset below, apply the rule of thumb to determine the starting point for the number of bins in a histogram.

Rule of Thumb Reminder:

\[b = \max(\operatorname{round}(\sqrt{n}) + 2, 5)\]

If \(b > 30\), start testing with values in the 20-30 range.

  1. A dataset of CPU temperatures with n = 49 observations.

  2. A dataset of response times with n = 200 observations.

  3. A dataset of tensile strength measurements with n = 25 observations.

  4. A dataset of network latency values with n = 1000 observations.

  5. A dataset of battery voltages with n = 16 observations.

  6. For each dataset above, if your initial histogram looked too “jagged” (noisy), would you increase or decrease the number of bins? Why?

Solution

Part (a): n = 49

\(b = \max(\operatorname{round}(\sqrt{49}) + 2, 5) = \max(7 + 2, 5) = \max(9, 5) = \mathbf{9}\) bins

Part (b): n = 200

\(\sqrt{200} \approx 14.14\), so \(\operatorname{round}(\sqrt{200}) = 14\)

\(b = \max(14 + 2, 5) = \max(16, 5) = \mathbf{16}\) bins

Part (c): n = 25

\(b = \max(\operatorname{round}(\sqrt{25}) + 2, 5) = \max(5 + 2, 5) = \max(7, 5) = \mathbf{7}\) bins

Part (d): n = 1000

\(\sqrt{1000} \approx 31.62\), so \(\operatorname{round}(\sqrt{1000}) = 32\)

\(b = \max(32 + 2, 5) = \max(34, 5) = 34\)

Since \(b > 30\), we apply the guideline: start testing with 20-30 bins (e.g., start at 25).

Part (e): n = 16

\(b = \max(\operatorname{round}(\sqrt{16}) + 2, 5) = \max(4 + 2, 5) = \max(6, 5) = \mathbf{6}\) bins

Part (f): Adjusting for Jagged Histograms

If the histogram looks too jagged (noisy, with erratic up-and-down patterns), you should decrease the number of bins.

Reasoning:

  • Too many bins means each bin contains fewer observations

  • Small counts per bin are more susceptible to random variation

  • Wider bins (fewer bins) smooth out this noise by aggregating more data points

  • The goal is to reveal the overall shape/trend without being distracted by sampling variability


Exercise 2: Interpreting Histogram Shapes

A manufacturing engineer collects data on the thickness of semiconductor wafers (in micrometers). Four different histograms are created using different bin counts from the same dataset of n = 100 measurements.

Wafer thickness histograms with 5, 12, 25, and 50 bins

Fig. 2.17 Effect of bin count on histogram appearance (same data, different bins)

Describe this figure in detail

Four histograms of the same 100 wafer thickness measurements in a 2-by-2 grid, titled “5 Bins” (top left), “12 Bins” (top right), “25 Bins” (bottom left) and “50 Bins” (bottom right). Each has Wafer Thickness (μm) on the horizontal axis, from about 420 to 565 with ticks at 440, 480, 520 and 560, and Density on the vertical axis. Every panel has the same two curves. The red curve, the kernel density estimate, peaks at about 0.0185 near 492 μm. The blue curve, the normal curve, peaks at about 0.017 near 500 μm. The red curve lies above the blue one between about 477 and 499 μm and beyond about 537 μm, and below it between about 499 and 537 μm.

5 Bins: five wide bars with heights of about 0.0003 (at about 419 to 448 μm, barely visible), 0.0045, 0.0166, 0.0097 and 0.0034. The tallest bar spans about 477 to 506 μm.

12 Bins: heights from left to right of about 0.001, 0.002, 0.0048, 0.0057, 0.017, 0.020, 0.0143, 0.0124, 0.0067, 0.0067, 0.002 and 0.003. The tallest bar spans about 488 to 499 μm. To its left the heights drop to about 0.006 by 478 μm. To its right they step down more gradually, and the last bar is a little taller than the one before it.

25 Bins: the overall rise and fall is still visible, but neighboring bars jump up and down. The tallest bar, about 0.023, spans about 494 to 498 μm. The next bar to the right drops to about 0.010, and the one after that rises to about 0.019. A short bar of about 0.002 sits between two bars of about 0.008 near 472 μm. Two bins are empty, at about 450 to 455 μm and at about 546 to 551 μm.

50 Bins: many narrow bars of very uneven height, from about 0.004 to about 0.030 on the density axis. The two tallest, about 0.030, are near 488 and 495 μm. Empty bins, shown as gaps down to the axis, are scattered through both tails, below about 475 μm and above about 525 μm.

Table 2.4 Histogram Descriptions

Bins

Description

5

Shows a single, very wide bar dominating the center with small bars on each end

12

Shows a clear bell-shaped pattern, symmetric around 500 μm, with smooth transitions

25

Shows a generally bell-shaped pattern but with noticeable irregularities and some empty bins

50

Shows an extremely jagged pattern with many bars of height 0, 1, or 2; no clear overall shape

  1. Which bin count best represents the data? Justify your answer.

  2. What does the rule of thumb suggest for n = 100?

  3. The 5-bin histogram hides important information. What might we miss by using too few bins?

  4. The 50-bin histogram shows many empty bins. Why is this problematic for understanding the distribution?

  5. Based on the 12-bin description, what can you conclude about the normality of the wafer thickness data?

Solution

Part (a): Best Bin Count

12 bins best represents the data.

Justification:

  • Shows a clear, interpretable shape (bell-shaped, symmetric)

  • Smooth transitions between adjacent bins suggest genuine pattern, not noise

  • Neither oversimplified (like 5 bins) nor dominated by random variation (like 50 bins)

  • Reveals the center (~500 μm) and spread clearly

Part (b): Rule of Thumb for n = 100

\(b = \max(\operatorname{round}(\sqrt{100}) + 2, 5) = \max(10 + 2, 5) = \mathbf{12}\) bins

This matches the optimal choice from Part (a)!

Part (c): What Too Few Bins Hides

With only 5 bins, we might miss:

  • The symmetry of the distribution (hard to assess with so few bars)

  • Any secondary modes or unusual features in the distribution

  • The precise location of the center

  • The shape of the tails—whether they taper gradually or drop off sharply

  • Any outliers that might be present

The distribution might appear normal when it actually has skewness or other features.

Part (d): Problems with Many Empty Bins

Empty bins in a 50-bin histogram are problematic because:

  • They suggest gaps in the data that don’t truly exist—the underlying distribution is continuous

  • The jagged appearance is caused by sampling variability, not real features

  • It’s difficult to perceive the overall shape when the eye is distracted by noise

  • With n = 100 and 50 bins, the average count per bin is only 2—far too few for stable estimates

Part (e): Normality Assessment

Based on the 12-bin description (“clear bell-shaped pattern, symmetric around 500 μm, with smooth transitions”), the wafer thickness data appears to be approximately normal.

Evidence of normality:

  • Bell-shaped: Characteristic of normal distributions

  • Symmetric: Normal distributions are perfectly symmetric

  • Smooth transitions: Suggests the distribution follows a regular pattern without unusual features

A formal assessment would compare the histogram to a normal overlay curve.


Exercise 3: Histogram vs. Bar Graph

Determine whether a histogram or bar graph would be more appropriate for each variable. Justify your choice.

  1. The number of cores in servers at a data center (1, 2, 4, 8, 16, 32, or 64 cores).

  2. The execution time of a sorting algorithm measured in milliseconds (continuous range from 0.5 to 150 ms).

  3. Customer satisfaction ratings (1, 2, 3, 4, or 5 stars).

  4. The weight of packages processed by a shipping facility (ranging from 0.1 kg to 50 kg).

  5. The number of defects found per circuit board (0, 1, 2, 3, … up to 12 in the dataset).

  6. A quality engineer has data on the exact diameter of 500 ball bearings, measured to 0.001 mm precision. Should they use a histogram or bar graph? Explain.

Solution

Part (a): Server Cores

Bar graph is more appropriate.

  • Only 7 possible values (1, 2, 4, 8, 16, 32, 64)

  • The values are discrete and represent specific configurations

  • No meaningful “in-between” values exist (you can’t have 3 cores in this context)

  • The gaps between values are not uniform (jumps are powers of 2)

Part (b): Sorting Algorithm Execution Time

Histogram is appropriate.

  • Time is a continuous numerical variable

  • Many different possible values across a continuous range

  • We want to see the shape of the distribution (e.g., is it skewed? bimodal?)

  • Binning into intervals reveals patterns in the overall distribution

Part (c): Customer Satisfaction Ratings

Bar graph is more appropriate.

  • Only 5 possible values (1, 2, 3, 4, 5)

  • These are ordinal categories (the intervals between ratings may not be equal)

  • As discussed in Chapter 2.1, star ratings are typically treated as categorical ordinal

  • Each bar represents a specific rating level, not an interval

Part (d): Package Weight

Histogram is appropriate.

  • Weight is a continuous numerical variable

  • Wide range of possible values (0.1 to 50 kg)

  • We want to understand the distribution shape (e.g., are most packages light or heavy?)

  • Binning allows us to see patterns across the continuous range

Part (e): Defects per Circuit Board

Either could work, but context matters.

  • Bar graph: If we want to show the exact count at each defect level (0, 1, 2, …, 12)

  • Histogram: If we want to emphasize the overall shape of the distribution

With 13 possible values (0 through 12), this is borderline. A bar graph preserves exact information; a histogram would treat adjacent values as potentially groupable.

Recommendation: Use a bar graph if precision matters; use a histogram if visualizing the overall shape is more important.

Part (f): Ball Bearing Diameters

Histogram is the clear choice.

  • Diameter is a continuous measurement

  • With 0.001 mm precision and 500 bearings, there could be hundreds of unique values

  • A bar graph with hundreds of bars would be unreadable

  • Histograms bin the continuous values into manageable intervals

  • This reveals the distribution shape, center, and spread—essential for quality control


Exercise 4: Reading Histograms with Overlays

A biomedical engineer measures the response time of a blood glucose sensor (in seconds). The data for n = 64 measurements is provided below.

# Blood glucose sensor response times (seconds)
response_time <- c(2.8, 3.1, 3.2, 3.3, 3.4, 3.5, 3.5, 3.6, 3.6, 3.7,
                   3.7, 3.8, 3.8, 3.8, 3.9, 3.9, 3.9, 4.0, 4.0, 4.0,
                   4.0, 4.1, 4.1, 4.1, 4.1, 4.2, 4.2, 4.2, 4.3, 4.3,
                   4.3, 4.4, 4.4, 4.5, 4.5, 4.5, 4.6, 4.6, 4.7, 4.7,
                   4.8, 4.9, 5.0, 5.0, 5.1, 5.2, 5.3, 5.4, 5.5, 5.6,
                   5.7, 5.8, 5.9, 6.0, 6.1, 6.2, 6.3, 6.4, 6.5, 6.6,
                   6.7, 6.8, 6.9, 7.0)
sensor_data <- data.frame(response_time)
  1. Create a histogram with the standard course format: density scaling, kernel density curve (red), and normal curve overlay (blue). Use the rule of thumb for the number of bins.

  2. What is the approximate center (typical value) of the response times? What is the approximate range?

  3. Does the data appear to follow a normal distribution? Explain by comparing the red and blue curves in your histogram.

  4. In what way does the actual distribution differ from a normal distribution?

  5. Why do we use aes(y = after_stat(density)) instead of just counting frequencies when adding these overlay curves?

Solution

Part (a): R Code for Histogram

library(ggplot2)

# Calculate summary statistics
xbar <- mean(sensor_data$response_time)
s <- sd(sensor_data$response_time)
bins <- max(round(sqrt(nrow(sensor_data))) + 2, 5)  # = 10

# Create histogram with overlays
ggplot(sensor_data, aes(x = response_time)) +
  geom_histogram(aes(y = after_stat(density)),
                 bins = bins, fill = "grey", col = "black") +
  geom_density(col = "red", linewidth = 1) +
  stat_function(fun = dnorm,
                args = list(mean = xbar, sd = s),
                col = "blue", linewidth = 1) +
  ggtitle("Blood Glucose Sensor Response Times") +
  xlab("Response Time (seconds)") +
  ylab("Density") +
  theme_minimal()

Number of bins: \(b = \max(\operatorname{round}(\sqrt{64}) + 2, 5) = \max(8 + 2, 5) = 10\)

Blood glucose sensor response times histogram

Fig. 2.18 Histogram of sensor response times showing right-skewed distribution

Describe this figure in detail

Histogram titled “Blood Glucose Sensor Response Times”, drawn on a density scale with gray bars. The horizontal axis is Response Time (seconds), from about 2.6 to 7.2, with ticks at 3 through 7; the vertical axis is Density, from 0 to about 0.55. From left to right the 10 bar heights are about 0.03, 0.20, 0.335, 0.535, 0.30, 0.17, 0.17, 0.17, 0.13 and 0.10 on the density axis. The tallest bar spans about 4.0 to 4.4 seconds. To its left the heights drop to about 0.03 within three bars. To its right they level off near 0.17 for three bars before tapering to about 0.10 at the right end.

The red curve, the kernel density estimate, peaks at about 0.415 near 4.1 seconds, flattens at about 0.16 to 0.17 between about 5.4 and 6.3 seconds, and ends at about 0.09 at 7.0 seconds. The blue curve, a normal density with the sample mean and standard deviation, peaks lower and farther right, at about 0.37 near 4.7 seconds, and ends at about 0.04 at 7.0 seconds. The red curve lies above the blue one between about 3.1 and 4.4 seconds and beyond about 6.0 seconds, and below it between about 4.4 and 6.0 seconds.

Part (b): Center and Range

  • Center: The approximate center is around 4.0-4.5 seconds (where the histogram bars are tallest). The calculated mean is approximately 4.7 seconds.

  • Range: The range is 7.0 - 2.8 = 4.2 seconds (from minimum to maximum value).

Part (c): Normality Assessment

No, the data does NOT appear to follow a normal distribution.

Evidence from comparing the curves:

  • The red curve (kernel density, actual data shape) is asymmetric—it peaks to the left of center and has a longer right tail

  • The blue curve (normal distribution) is symmetric

  • The two curves diverge noticeably, especially in the tails

  • The red curve extends further right than the blue curve would predict

If the data were normal, the red and blue curves would closely overlap.

Part (d): How the Distribution Differs from Normal

The distribution is right-skewed (positively skewed).

Specific differences:

  • Right tail is longer: The red curve shows more density in the 5.5-7.0 second range than the blue normal curve

  • Left tail is shorter: Fewer observations on the low end than a normal distribution would predict

  • Peak is shifted left: The mode (most frequent values) is around 4.0-4.2 seconds, while the mean is pulled higher (~4.7 seconds) by the right tail

Interpretation: While most sensors respond quickly (around 4 seconds), some have unusually long response times, creating the right-skewed pattern.

Part (e): Why Use Density Scaling

We use aes(y = after_stat(density)) to put the histogram on a density scale rather than a frequency (count) scale.

Reasons:

  • Overlay compatibility: The kernel density (red) and normal curve (blue) are probability density functions—their y-axis is density, not counts

  • Scale matching: If the histogram showed raw counts (e.g., bars with heights 5, 10, 15), the overlay curves would appear as flat lines near zero (densities are typically small numbers like 0.1, 0.3)

  • Comparability: Density scaling ensures the histogram bars and overlay curves use the same y-axis scale

  • Area interpretation: On a density scale, the total area under the histogram equals 1, matching probability density functions


Exercise 5: Constructing Histograms in R

A mechanical engineer tests the fuel efficiency (in miles per gallon) of 81 vehicles. Write R code to create a properly formatted histogram following course standards.

The data is stored in a data frame called vehicles with a column named mpg.

  1. Calculate the appropriate starting number of bins using the rule of thumb.

  2. Write the complete R code to create a histogram with:

    • Density scaling on the y-axis

    • A kernel density curve in red

    • A normal curve overlay in blue

    • Appropriate title and axis labels

  3. The engineer runs your code and observes that the red curve has two distinct peaks (bimodal) while the blue curve has only one peak. What does this tell us about the data?

  4. Suggest a possible explanation for why fuel efficiency data might be bimodal. How might the engineer investigate further?

Solution

Part (a): Number of Bins

\(n = 81\)

\(b = \max(\operatorname{round}(\sqrt{81}) + 2, 5) = \max(9 + 2, 5) = \max(11, 5) = \mathbf{11}\) bins

Part (b): Complete R Code

library(ggplot2)

# Calculate summary statistics
xbar <- mean(vehicles$mpg)
s <- sd(vehicles$mpg)
bins <- max(round(sqrt(nrow(vehicles))) + 2, 5)  # = 11

# Create histogram with overlays
ggplot(vehicles, aes(x = mpg)) +
  geom_histogram(aes(y = after_stat(density)),
                 bins = bins, fill = "grey", col = "black") +
  geom_density(col = "red", linewidth = 1) +
  stat_function(fun = dnorm,
                args = list(mean = xbar, sd = s),
                col = "blue", linewidth = 1) +
  ggtitle("Distribution of Vehicle Fuel Efficiency") +
  xlab("Fuel Efficiency (mpg)") +
  ylab("Density") +
  theme_minimal()
Bimodal fuel efficiency histogram

Fig. 2.19 Bimodal distribution reveals two subgroups in the data

Describe this figure in detail

Histogram titled “Vehicle Fuel Efficiency (Combined)”, drawn on a density scale with gray bars. The horizontal axis is Fuel Efficiency (mpg), from about 12 to 42, with ticks at 20, 30 and 40; the vertical axis is Density, from 0 to about 0.075. From left to right the 11 bar heights are about 0.018, 0.051, 0.060, 0.032, 0.005, 0.018, 0.074, 0.051, 0.046, 0.014 and 0.005 on the density axis. The bars form two clusters. The left cluster’s tallest bar spans about 17.4 to 20 mpg. The right cluster’s tallest bar, the tallest overall, spans about 28 to 30.7 mpg. Between them, the bar at about 22.7 to 25.4 mpg is nearly empty.

The red curve, the kernel density estimate, has two peaks: about 0.048 near 17.7 mpg and about 0.055 near 31.4 mpg, with a low point of about 0.020 near 24.4 mpg. The blue curve, a normal density with the sample mean and standard deviation, has a single peak of about 0.053 near 25.7 mpg, close to where the red curve is lowest.

Part (c): Interpretation of Bimodal Pattern

The bimodal pattern (two peaks in the red curve, one in the blue) tells us:

  • The data is NOT normally distributed

  • There appear to be two distinct subgroups in the data

  • The normal curve (blue) is a poor fit—it assumes one central tendency

  • Averaging across the two groups produces a misleading “middle” peak

The data likely comes from a mixture of two populations with different typical fuel efficiencies.

Part (d): Possible Explanation and Investigation

Possible explanations for bimodal fuel efficiency:

  • Vehicle type: One group might be sedans/compact cars (higher mpg), another might be trucks/SUVs (lower mpg)

  • Engine type: Hybrid/electric vehicles vs. traditional gasoline engines

  • Transmission: Manual vs. automatic

  • Model year: Older vs. newer vehicles with different efficiency standards

How to investigate:

  1. Create faceted histograms by a categorical variable (e.g., vehicle type):

    ggplot(vehicles, aes(x = mpg)) +
      geom_histogram(aes(y = after_stat(density)), bins = 11) +
      facet_wrap(~ vehicle_type)
    
Fuel efficiency faceted by vehicle type

Fig. 2.20 Faceted histograms reveal the source of bimodality

Describe this figure in detail

Two histograms side by side under the title “Fuel Efficiency by Vehicle Type”, in panels labeled “Sedan” (left) and “Truck/SUV” (right). Both panels share the horizontal axis Fuel Efficiency (mpg), from about 9.5 to 40 with ticks at 10, 20, 30 and 40, and both use bins about 3.8 mpg wide. Each panel has its own Density axis, a red curve (kernel density estimate) and a blue curve (normal curve).

Sedan: the vertical axis runs from 0 to about 0.13. Bars occupy only about 25 to 40 mpg, with heights of about 0.029, 0.122, 0.087 and 0.023; the tallest spans about 28.6 to 32.5 mpg. The red curve peaks at about 0.133 near 30.4 mpg and dips to a shoulder of about 0.107 near 33 mpg; to the left it runs flat along zero from about 12.5 to 22.5 mpg. The blue curve peaks at about 0.136 near 32 mpg.

Truck/SUV: the vertical axis runs from 0 to about 0.18. Bars occupy only about 9.5 to 25 mpg, with heights of about 0.007, 0.087, 0.131 and 0.036; the tallest spans about 17.2 to 21 mpg. The red curve peaks at about 0.187 near 17.5 mpg; to the right it runs flat along zero from about 26 to 39 mpg. The blue curve peaks at about 0.157 near 17.7 mpg.

The two panels’ bars do not overlap: the Truck/SUV bars end at about 25 mpg, where the Sedan bars begin.

  1. Color-code the histogram by group to see if the modes correspond to different categories

  2. If no categorical variable is recorded, consider whether the data should be separated before analysis


Exercise 6: Comprehensive Histogram Analysis

A quality control engineer at an aerospace company measures the tensile strength (in MPa) of 120 titanium alloy specimens. The data is provided below.

# Tensile strength measurements (MPa) - n = 120
tensile_strength <- c(
  867, 879, 885, 891, 893, 896, 899, 901, 903, 905,
  908, 910, 912, 914, 916, 917, 919, 920, 921, 923,
  924, 925, 927, 928, 929, 930, 931, 932, 933, 934,
  935, 936, 937, 938, 939, 940, 941, 941, 942, 943,
  944, 945, 945, 946, 947, 948, 948, 949, 950, 950,
  951, 951, 952, 952, 953, 954, 954, 955, 956, 957,
  957, 958, 959, 960, 960, 961, 962, 963, 964, 965,
  966, 967, 968, 969, 970, 971, 972, 973, 974, 975,
  976, 977, 978, 979, 980, 982, 983, 985, 986, 988,
  989, 991, 993, 995, 997, 999, 1001, 1003, 1006, 1008,
  1010, 1013, 1015, 1017, 1019, 1021, 1023, 1025, 1028, 1030,
  1033, 1036, 1038, 1040, 1042, 1044, 1046, 1048, 1050, 1052
)
titanium_data <- data.frame(tensile_strength)
  1. Create a histogram following course standards (density scaling, red kernel density, blue normal overlay). Use the rule of thumb for bins. What do you observe about the agreement between the red and blue curves?

  2. The engineer notices a few high values above 1040 MPa. Should these automatically be considered outliers? Explain.

  3. Calculate the expected number of bins using the rule of thumb.

  4. If the engineer wanted to determine whether the tensile strength meets the specification of μ = 950 MPa, what type of statistical analysis might be appropriate? (You’ll learn this in later chapters.)

  5. The same engineer tests specimens from a second supplier and creates a faceted histogram (one panel per supplier). What would this visualization allow them to compare?

Solution

Part (a): Histogram and Normality Assessment

library(ggplot2)

# Calculate summary statistics
xbar <- mean(titanium_data$tensile_strength)
s <- sd(titanium_data$tensile_strength)
bins <- max(round(sqrt(nrow(titanium_data))) + 2, 5)  # = 13

# Create histogram with overlays
ggplot(titanium_data, aes(x = tensile_strength)) +
  geom_histogram(aes(y = after_stat(density)),
                 bins = bins, fill = "grey", col = "black") +
  geom_density(col = "red", linewidth = 1) +
  stat_function(fun = dnorm,
                args = list(mean = xbar, sd = s),
                col = "blue", linewidth = 1) +
  ggtitle("Titanium Alloy Tensile Strength") +
  xlab("Tensile Strength (MPa)") +
  ylab("Density") +
  theme_minimal()
Titanium alloy tensile strength histogram

Fig. 2.21 Titanium tensile strength showing approximately normal distribution

Describe this figure in detail

Histogram titled “Titanium Alloy Tensile Strength”, drawn on a density scale with gray bars. The horizontal axis is Tensile Strength (MPa), from about 855 to 1055, with ticks every 50 MPa from 850 to 1050; the vertical axis is Density, from 0 to about 0.0115. From left to right the 13 bar heights are about 0.0005, 0.0011, 0.0027, 0.0043, 0.0065, 0.0103, 0.0114, 0.0081, 0.0054, 0.0038, 0.0043, 0.0032 and 0.0032 on the density axis. The tallest bar spans about 948 to 963 MPa. To its left the heights fall steadily to the smallest bar at the left edge. To its right they fall to about 0.0054 and then stay between about 0.003 and 0.0045 for the last four bars, out to about 1055 MPa.

The red curve, the kernel density estimate, peaks at about 0.0102 near 951 MPa. The blue curve, a normal density with the sample mean and standard deviation, peaks at about 0.0095 near 963 MPa. The two curves nearly coincide from 867 to about 925 MPa. The red curve is above the blue one between about 925 and 963 MPa and below it between about 963 and 1018 MPa. Beyond about 1018 MPa the red curve is above the blue one again: it levels off at about 0.0037 and ends at about 0.002 at 1052 MPa, while the blue curve ends at about 0.001.

Observation: The kernel density (red) and normal curve (blue) show reasonably close agreement, indicating the data is approximately normally distributed. Both curves are roughly symmetric and bell-shaped with similar centers around 950-960 MPa.

Part (b): Are the High Values Outliers?

Not automatically. The values above 1040 MPa should not be assumed to be outliers just because they are in the tail.

Reasons:

  • Normal distributions have tails—some extreme values are expected

  • With n = 120 observations, we expect some values to be 2+ standard deviations from the mean

  • The data was constructed to be approximately normal; extreme values are part of that distribution

To properly assess outliers:

  • Use formal methods like the 1.5×IQR rule (Chapter 3.4)

  • Check if values are more than 3 standard deviations from the mean

  • Investigate whether these specimens had unusual manufacturing conditions

Part (c): Expected Number of Bins

\(n = 120\)

\(\sqrt{120} \approx 10.95\), so \(\operatorname{round}(\sqrt{120}) = 11\)

\(b = \max(11 + 2, 5) = \max(13, 5) = \mathbf{13}\) bins

Part (d): Appropriate Statistical Analysis

To determine if the tensile strength meets the specification μ = 950 MPa, the engineer could use:

  • Confidence interval for the mean (Chapter 9): Construct an interval estimate for the true mean tensile strength and check if 950 MPa falls within it

  • Hypothesis test (Chapter 10): Test H₀: μ = 950 vs. Hₐ: μ ≠ 950

Since the data appears approximately normal, a one-sample t-test or t-confidence interval would be appropriate (assuming σ is unknown).

Part (e): Faceted Histogram Comparisons

A faceted histogram (one panel per supplier) would allow the engineer to compare:

  • Center: Do both suppliers produce specimens with similar average tensile strength?

  • Spread: Is one supplier more consistent (less variable) than the other?

  • Shape: Are both distributions approximately normal, or does one show skewness or bimodality?

  • Range: Do the specimens from both suppliers fall within acceptable limits?

  • Outliers: Does one supplier have more extreme values?

Titanium tensile strength by supplier

Fig. 2.22 Faceted histograms comparing two suppliers

Describe this figure in detail

Two histograms side by side under the title “Tensile Strength by Supplier”, in panels labeled “Supplier A” (left) and “Supplier B” (right). They share the horizontal axis Tensile Strength (MPa), from about 850 to 1055 with ticks every 50 MPa, and the vertical axis Density, from 0 to about 0.014. Each panel has a red curve (kernel density estimate) and a blue curve (normal curve). The Supplier A panel is the same histogram as in Part (a).

Supplier A: bars run from about 856 to 1056 MPa. The tallest, about 0.0114, spans about 948 to 963 MPa, and the last four bars stay between about 0.003 and 0.0045. The red curve peaks at about 0.0102 near 951 MPa and the blue curve at about 0.0095 near 963 MPa. The red curve stays above the blue curve in the right tail beyond about 1018 MPa.

Supplier B: bars run over a narrower range, from about 871 to 1025 MPa, with heights from left to right of about 0.0022, 0.0049, 0.0087, 0.0076, 0.0087, 0.0135, 0.0103, 0.0070, 0.0016 and 0.0005. The tallest bar, the tallest in either panel, spans about 948 to 963 MPa. The blue curve peaks at about 0.0126 near 944 MPa. The red curve has a shoulder of about 0.008 near 918 MPa, peaks at about 0.0113 near 957 MPa, and falls to nearly zero by about 1040 MPa. Supplier B’s bars cover about 155 MPa, against about 200 MPa for Supplier A, and reach higher on the shared density axis.

This visual comparison would inform decisions about supplier quality and consistency before formal statistical tests are performed.


2.3.7. Additional Practice Problems

True/False Questions (1 point each)

  1. A histogram groups data into intervals (bins), while a bar graph displays discrete categories.

    Ⓣ or Ⓕ

  2. If a histogram appears too smooth and oversimplified, the solution is to decrease the number of bins.

    Ⓣ or Ⓕ

  3. The rule of thumb for the number of bins in a histogram is \(\max(\operatorname{round}(\sqrt{n}) + 2, 5)\).

    Ⓣ or Ⓕ

  4. When assessing normality, we want the kernel density curve (red) and the normal curve (blue) to be as different as possible.

    Ⓣ or Ⓕ

  5. In a histogram, gaps between bars always indicate missing data in that range.

    Ⓣ or Ⓕ

  6. Using aes(y = after_stat(density)) in ggplot2 scales the histogram so that the total area of all bars equals 1.

    Ⓣ or Ⓕ

Multiple Choice Questions (2 points each)

  1. A dataset has n = 400 observations. Using the rule of thumb, what is the starting point for the number of histogram bins?

    Ⓐ 18

    Ⓑ 20

    Ⓒ 22

    Ⓓ 25

  2. An engineer creates a histogram with 50 bins for a dataset of 100 observations. The histogram appears very jagged with many bars of height 0 or 1. What should the engineer do?

    Ⓐ Add more bins to show finer detail

    Ⓑ Decrease the number of bins to reduce noise

    Ⓒ Remove the outliers causing the jagged appearance

    Ⓓ Switch to a pie chart for better visualization

  3. A histogram and its red kernel density curve show most observations concentrated at higher values, with a long tail extending toward lower values. The blue normal curve is symmetric and does not match this pattern. Which description best fits the data?

    Ⓐ The data are approximately normal

    Ⓑ The data are right-skewed (positively skewed)

    Ⓒ The data are left-skewed (negatively skewed)

    Ⓓ The wrong number of bins was used

  4. Which of the following variables would be LEAST appropriate for a histogram?

    Ⓐ The weight of packages at a shipping facility (ranging from 0.5 to 75 kg)

    Ⓑ The number of bedrooms in houses (1, 2, 3, 4, or 5)

    Ⓒ The duration of phone calls at a customer service center (ranging from 10 seconds to 45 minutes)

    Ⓓ The temperature readings from a sensor over 24 hours (recorded every minute)

Answers to Practice Problems

True/False Answers:

  1. True — Histograms bin continuous data into intervals; bar graphs display counts for discrete categories. This is a fundamental distinction between the two visualization types.

  2. False — If a histogram appears too smooth/oversimplified, you should increase the number of bins to reveal more detail. Decreasing bins would make it even more simplified.

  3. True — This is the rule of thumb presented in the chapter for determining a starting point for bin selection.

  4. False — When assessing normality, we want the curves to be as similar as possible. Close agreement between the red (actual shape) and blue (theoretical normal) curves indicates the data is approximately normal.

  5. False — In a histogram with bins covering a continuous range, gaps indicate no observations fell within that interval—not that data is “missing.” Unlike bar graphs where gaps may be stylistic, histogram gaps represent genuine absence of data in those ranges.

  6. True — Density scaling ensures the histogram represents a probability density, where the total area under the histogram equals 1. This allows proper comparison with overlay density curves.

Multiple Choice Answers:

  1. Ⓒ — \(\sqrt{400} = 20\), so \(b = \max(20 + 2, 5) = 22\) bins.

  2. Ⓑ — With only 100 observations spread across 50 bins, each bin averages only 2 data points, leading to high sampling variability (jagged appearance). Reducing the number of bins will smooth out this noise and reveal the underlying distribution shape.

  3. Ⓒ — The distribution is left-skewed (negatively skewed) because its longer tail extends toward smaller values. Skewness is named for the direction of the longer tail, not the location of most observations. Comparing the heights of the red and blue curves in one region alone is insufficient to determine skewness.

  4. Ⓑ — The number of bedrooms has only 5 discrete possible values (1, 2, 3, 4, 5), making it better suited for a bar graph. The other options all represent continuous numerical variables with many possible values, ideal for histograms.