Tutorials › Exam PA - Predictive Analytics › Bar Charts for Discrete Counts

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

Bar Charts for Discrete Counts

Use bar charts to summarize discrete count values in R and distinguish real zeros, sparse upper tails, and unusual high counts.

Beginner 9 min read

What You'll Learn

  • Decide when a count variable is appropriately displayed with a bar chart.
  • Build and label frequency bar charts in R using `table()` and `barplot()`.
  • Preserve count levels with zero observed frequency by specifying factor levels.
  • Distinguish a recorded zero from a missing value and select the correct denominator.
  • Interpret right-tailed count distributions and isolated high counts cautiously.
  • Avoid reading bar-chart spacing or height as a continuous measurement or a rate.

Why Use a Bar Chart for Discrete Counts?

A count records how many times something occurred: the number of service requests, incidents, or items purchased during a defined period. Counts are numerical, but they can take only nonnegative integer values. When the question is how often each count value occurs, a bar chart gives each possible count value its own bar and shows its frequency.

This differs from a histogram. A histogram groups measurements into intervals, and its bars represent those intervals. A bar chart for counts represents individual values such as 0, 1, and 2. The bars are separated to signal that each value is a distinct category; the height indicates how many observations had that value. The count values have a numerical order, but a bar chart is not a picture of measurements varying continuously between those values.

Definition: For \(n\) observed, nonmissing counts \(x_1,\ldots,x_n\), the frequency of count value \(j\) is the number of observations equal to \(j\). A frequency bar chart places each displayed count value on the horizontal axis and its frequency on the vertical axis. If frequency is divided by \(n\), the bar heights are relative frequencies, or observed proportions.

A bar chart is most useful when the count values and their frequencies can be read without overcrowding the display. Before plotting, identify the observational unit and the period or opportunity over which the count was measured. For example, “number of incidents per account during one year” is more informative than “incidents” alone.

Frequencies and Relative Frequencies

Let \(n_j\) be the number of observations equal to count value \(j\). The relative frequency is \(n_j/n\). Each observation contributes to exactly one count value, so the frequencies sum to the number of nonmissing observations, and the relative frequencies sum to one.

$$ n_j=\sum_{i=1}^{n} I(x_i=j), \qquad \widehat{p}_j=\frac{n_j}{n}, \qquad \sum_j n_j=n, \qquad \sum_j \widehat{p}_j=1. $$

Here \(I(x_i=j)\) equals 1 when observation \(i\) has count \(j\), and 0 otherwise. The justification for the totals is direct: for each observation, exactly one indicator across the count values is 1. Adding those indicators over observations therefore counts each observation once. Dividing each frequency by \(n\) makes the total relative frequency \(n/n=1\). These are descriptive identities; they do not require a probability model.

In R, `table(x)` counts the observed values of a vector. `barplot()` plots the resulting table. For integer counts, explicitly setting factor levels is useful when a count value in the range was not observed: the value then remains visible with frequency zero rather than disappearing from the display.

Building a Count Bar Chart in R

1
Check the variable and observations.
Confirm that each value is a count for the same defined unit and period. Identify missing values separately; do not silently convert them to zero.
2
Tabulate the values.
Use `table()` for observed frequencies, or convert to a factor with specified levels if zero-frequency count values should appear.
3
Plot and label.
Use count values as the horizontal-axis labels and frequencies or relative frequencies as the vertical-axis scale. State which scale is shown.
4
Interpret the shape in context.
Describe common values, zeros, the upper tail, and any isolated high value. A plot alone does not establish why a value occurred or whether it is erroneous.

Worked Example: Frequencies of Repeat Service Contacts

A hypothetical service team records the number of repeat contacts made by each of 20 accounts during a year. The observed counts are 0 for six accounts, 1 for five, 2 for four, 3 for three, 4 for one, and 5 for one. Construct and interpret a frequency bar chart. The method is to tabulate each distinct integer count and check that the frequencies account for all 20 accounts; this avoids treating the counts as measurements to be grouped into histogram intervals.

Step 1: Verify the sample size. The stated frequencies sum to \(6+5+4+3+1+1=20\), matching the number of accounts.

Step 2: Calculate relative frequencies. Divide each frequency by 20: for counts 0 through 5, the proportions are \(6/20=0.3000\), \(5/20=0.2500\), \(4/20=0.2000\), \(3/20=0.1500\), \(1/20=0.0500\), and \(1/20=0.0500\). They sum to \(0.3000+0.2500+0.2000+0.1500+0.0500+0.0500=1.0000\).

Step 3: Plot in R. A data vector matching the stated counts is `x <- c(0,0,0,0,0,0,1,1,1,1,1,2,2,2,2,3,3,3,4,5)`. Then use `freq <- table(factor(x, levels=0:5))` and `barplot(freq, xlab="Repeat contacts per account in one year", ylab="Number of accounts", main="Repeat contacts")`. The bars for 0 through 5 have heights 6, 5, 4, 3, 1, and 1.

Step 4: Interpret. Zero repeat contacts is the most frequent value, occurring for 6 of 20 accounts. The frequency decreases across most of the displayed values, while counts 4 and 5 occur once each. The chart describes the observed accounts; it does not show that repeat contacts caused any account outcome.

Check: The plotted bar heights sum to \(6+5+4+3+1+1=20\), so no account is omitted or counted twice.

Zero Counts, Missing Values, and Absent Levels

A recorded zero is a valid count: it means the event did not occur for that observational unit during the defined period. A missing value means the count was not recorded or is unavailable. These have different meanings and must not be combined. A zero-frequency count level is different again: it is a possible count value for which no observed unit had that count.

For example, if the observed counts run from 0 to 7 but none equals 4, a table of the raw numeric vector may omit 4 entirely. Setting factor levels from 0 through 7 includes a bar for 4 with height zero. This is useful when the range is small and the analyst wants to make the gap explicit. If the range is very large, displaying every integer level may make the chart difficult to read; choose a display that does not conceal which values were omitted.

Worked Example: Keeping Missing Values Separate From Zeros

A hypothetical team has records for 12 scheduled inspections. The recorded numbers of findings are \(0,0,0,1,1,2,2,2,3,7\), and two inspections have no recorded result. Construct a bar chart for count values 0 through 7 and state the denominator for relative frequencies. The method is to exclude missing results from the frequency table while retaining the actual zeros; this avoids treating unrecorded inspections as inspections with no findings.

Step 1: Count observed results. There are 10 nonmissing values and 2 missing values. The observed frequency of 0 is 3, of 1 is 2, of 2 is 3, of 3 is 1, and of 7 is 1. Counts 4, 5, and 6 each have frequency 0. The frequencies total \(3+2+3+1+1=10\).

Step 2: Calculate proportions among observed results. Use \(n=10\), not 12, because the two missing results do not supply count values. The proportions for counts 0, 1, 2, 3, 4, 5, 6, and 7 are respectively \(0.3000, 0.2000, 0.3000, 0.1000, 0.0000, 0.0000, 0.0000,\) and \(0.1000\). These sum to 1.0000.

Step 3: Construct the table and plot. In R, enter `x <- c(0,0,0,1,1,2,2,2,3,7,NA,NA)`, then use `freq <- table(factor(x[!is.na(x)], levels=0:7))` and `barplot(freq, xlab="Findings per inspection", ylab="Number of inspections", main="Recorded inspection counts")`. The resulting bar heights are 3, 2, 3, 1, 0, 0, 0, and 1.

Interpretation and check. The chart includes three inspections with zero findings and three count levels with no observed inspections. The two missing records are not bars. The plotted frequencies sum to 10, which equals the number of recorded results, while the full schedule contained 12 inspections. The common mistake of using 12 as the relative-frequency denominator would make the displayed proportions sum to \(10/12\), not 1.

Reading a Right Tail and an Isolated High Count

A count distribution can have many small values and a few much larger ones. On a bar chart, this may appear as high bars at low counts and a sequence of shorter bars extending toward larger counts. An isolated high count is a value separated from the other observed count values by a gap. The chart can identify that feature, but it cannot determine by itself whether the value is a recording error, a genuine rare event, or the result of a different process.

Keep the interpretation descriptive. State the observed value and its frequency, then check the underlying record and the context before drawing a conclusion. Do not remove a high count merely because it is far from the others. Also distinguish frequency from rate: a bar height is the number of units with a particular count, not the chance of an event for an individual unless a relevant probability model and assumptions are supplied.

Worked Example: An Isolated High Number of Incidents

A hypothetical maintenance group records incidents per device over one quarter for 16 devices. The counts are seven devices with 0 incidents, four with 1, two with 2, one with 3, one with 4, and one with 12. Make a frequency chart and describe its upper tail. The method is to preserve the integer levels from 0 through 12 so the gap is visible; this avoids collapsing distinct count values into continuous intervals or assuming that the high value is an error.

Step 1: Check the total. The frequencies sum to \(7+4+2+1+1+1=16\).

Step 2: Tabulate levels 0 through 12. The frequency vector, in order, is \(7,4,2,1,1,0,0,0,0,0,0,0,1\). The zero entries correspond to count values 5 through 11.

Step 3: Plot in R. Use `x <- c(rep(0,7), rep(1,4), rep(2,2), 3, 4, 12)`, followed by `freq <- table(factor(x, levels=0:12))` and `barplot(freq, xlab="Incidents per device in one quarter", ylab="Number of devices", main="Quarterly incident counts")`. The bar at 12 has height 1; the bars at 5 through 11 have height 0.

Step 4: Calculate relative frequencies and interpret. The proportions at counts 0, 1, 2, 3, 4, and 12 are \(7/16=0.4375\), \(4/16=0.2500\), \(2/16=0.1250\), \(1/16=0.0625\), \(1/16=0.0625\), and \(1/16=0.0625\). Their sum is 1.0000. Most devices had 0 or 1 incident, while one device had 12; no device had a count from 5 to 11. That isolated value warrants checking its record and context, not automatic deletion.

Check: The frequency vector sums to \(7+4+2+1+1+0+0+0+0+0+0+0+1=16\), matching the number of devices.

Common Interpretation Traps

  • Using a histogram when the task is to compare exact count values. A histogram combines values into intervals. For a small range of integer counts, separate bars make the frequency at each count directly visible.
  • Confusing a zero count with missing data. Zero is an observed value; missing means no value is available. Report the denominator used when calculating relative frequencies.
  • Omitting unobserved count levels without noticing. A table of observed values may not show a possible count with frequency zero. Specify factor levels when those gaps matter and the range is manageable.
  • Calling an isolated high value an error from the plot alone. A plot identifies a value to investigate; it does not establish its cause or validity.
  • Calling bar height a rate or individual probability. Frequency counts observational units. Comparisons can be misleading if the groups have different numbers of units or different observation periods.
Key takeaway: For a discrete count variable, use separated bars to show how many observations have each count value. Keep zeros, missing results, and zero-frequency levels distinct; check that the bars sum to the number of observed values; and treat a right tail or isolated high count as a feature to investigate, not a verdict.

Check Your Understanding

For each problem, identify the frequency or plotting choice and check the denominator or total before interpreting the chart.

  1. A hypothetical survey records the number of online orders made last month by 12 households: \(0,0,0,1,1,1,2,2,2,2,4,4\). Give the frequencies for counts 0 through 4, including any zero-frequency level, and the proportion with exactly 2 orders. (Answer: Frequencies \(3,3,4,0,2\); proportion \(4/12=0.3333\).)
  2. A hypothetical inspection file has eight recorded counts \(0,0,1,1,1,3,5,5\) and two missing results. What are the frequencies for counts 0 through 5, and what denominator should be used for relative frequencies among recorded results? (Answer: Frequencies \(2,3,0,1,0,2\); denominator 8.)
  3. For 15 hypothetical devices, the count frequencies are 0: 6, 1: 4, 2: 2, 3: 1, and 9: 2, with no devices at counts 4 through 8. What are the frequencies across all levels 0 through 9, and what proportion of devices had 9 events? (Answer: \(6,4,2,1,0,0,0,0,0,2\); \(2/15=0.1333\).)
  4. A hypothetical dataset contains 18 complete records, with counts 0: 5, 1: 6, 2: 4, 3: 2, and 8: 1. An analyst divides the frequency at count 8 by 20 because the original file had 20 rows, including two missing results. What is the correct relative frequency among observed counts, and what mistake was made? (Answer: \(1/18=0.0556\); the analyst used the number of rows including missing results instead of the 18 nonmissing observations.)
  5. A hypothetical call center records 14 counts: 0 occurs 5 times, 1 occurs 4 times, 2 occurs 2 times, 3 occurs once, and 10 occurs twice. What frequency should a bar at count 10 have, what are the frequencies at counts 4 through 9, and what can the plot establish about count 10? (Answer: The bar at 10 has height 2; counts 4 through 9 each have frequency 0; the plot establishes that two observed units had count 10, but not whether those values are errors.)
Clinical Biostats