Two-Way Tables and Graphs for Two Categorical Variables
How a two-way table organizes two categorical variables, how side-by-side bar charts, segmented bar charts and mosaic plots compare one variable across the other, and how to tell from them whether the variables are associated.
When each observational unit is sorted by two categorical variables, a two-way table shows the counts for every combination. The question is usually whether the variables are related: does one variable’s distribution change from one category of the other to the next? Graphs that compare those distributions group by group, using percents rather than counts, make the answer visible.
1Two-way tables
A survey asked 400 students two questions: “Do you use your phone in bed after lights out?” and “Do you feel rested most mornings?” (yes, sometimes, or no).
| Rested: yes | Sometimes | No | Total | |
|---|---|---|---|---|
| Phone in bed | 48 | 96 | 96 | 240 |
| No phone in bed | 72 | 56 | 32 | 160 |
| Total | 120 | 152 | 128 | 400 |
A two-way table, also called a contingency table, summarizes data for two categorical variables. One variable’s categories label the rows and the other’s label the columns. The entries in the cells can be frequencies (counts) or relative frequencies (proportions).
Phones and sleep
Use the two-way table of 400 students. (a) How many students use a phone in bed? (b) How many feel rested most mornings? (c) How many use a phone in bed and do not feel rested? (d) What percent of all 400 students keep the phone out of bed and feel rested?
- 1(a) The row total for phone in bed: 240 students.
- 2(b) The column total for rested yes: 120 students.
- 3(c) The cell where the phone-in-bed row meets the rested-no column: 96 students.
- 4(d) The no-phone, rested-yes cell is 72 students, and \(72 \div 400 = 0.18\), or 18% of all students.
2Side-by-side bar charts
Graphs of two categorical variables show, for each category of one variable, the frequency or relative frequency of each category of the other. A side-by-side bar chart puts a cluster of bars for each group next to each other.
Phone users who do not feel rested (96) outnumber non-users who feel rested (72), but there are 240 phone users and only 160 non-users. Bars of counts mix up “how common” with “how big the group is.” Use percents within each group.
3Segmented bar charts
A segmented bar chart gives each group one bar that reaches 100%, cut into segments for the categories of the other variable. Because every bar has the same height, the segments can be compared directly.
From counts to percents
Describe how to turn the table into the segmented bar chart in Figure 3.
- 1Decide which variable makes the bars. To compare phone users with non-users, use one bar per phone group.
- 2Within each phone group, divide each cell by that group’s row total. Phone in bed: \(48 \div 240 = 0.20\), \(96 \div 240 = 0.40\), \(96 \div 240 = 0.40\).
- 3No phone in bed: \(72 \div 160 = 0.45\), \(56 \div 160 = 0.35\), \(32 \div 160 = 0.20\).
- 4Draw each bar to 100% and stack the pieces in the same order in every bar, with a key.
Each bar’s segments must be percents of that bar’s group, so they add to 100%. If you divide by the grand total (400) or by a column total instead, the bars will not reach 100% and the comparison falls apart.
4Mosaic plots
A mosaic plot is a segmented bar chart whose bar widths are proportional to the size of each group. The heights show the split within each group, and the widths show how big each group is, so it displays both variables’ distributions in one picture.
Most students use a phone in bed
A claim says: “Most of the students surveyed use a phone in bed after lights out.” Use Figure 4 to decide whether the data support it.
- 1In a mosaic plot, the width of each column shows the size of that group.
- 2The phone-in-bed column takes 60% of the width: 240 of the 400 students.
- 360% is more than half, so “most” is justified.
In a mosaic plot the width carries information. A wide column is a big group. The area of each tile is proportional to its cell count: the large “phone in bed, rested: no” tile is 96 students out of 400.
5Association
Two categorical variables are associated if knowing the category of one changes the distribution of the other. Graphically: if the bars for the groups are split differently, the variables are associated. If every group is split about the same way, there is no association.
Use the graph
Based on Figure 3, is using a phone in bed associated with feeling rested for these students? Back up your answer.
- 1If the variables were not associated, the two bars would be split about the same way.
- 2They are not: 45% of students who keep the phone out of bed feel rested most mornings, compared with 20% of those who use it in bed.
- 3And 40% of phone-in-bed students do not feel rested, compared with 20% of the others.
Cause or not?
A student writes: “The survey proves that using a phone in bed makes students tired.” What is wrong with the claim?
- 1The data come from a survey, an observational study. Nobody was assigned to use or avoid the phone.
- 2Other variables could explain the link. Students with more homework or more stress might both use the phone late and sleep worse.
- 3An association in observational data does not show cause and effect.
Compare the same category across groups with numbers from the graph: “Students who kept the phone out of bed were more likely to feel rested most mornings than students who used it in bed (45% compared with 20%), so phone use and feeling rested are associated for these students.”
A survey can show that two categorical variables are associated, not that one causes the other. Write “were more likely to,” not “makes” or “causes,” unless the data come from an experiment with random assignment.
Quick reference
| Display | What it shows best |
|---|---|
| Two-way table | Exact counts for every combination, plus row and column totals |
| Side-by-side bar chart | Each category compared across groups, bar by bar |
| Segmented bar chart | How each group splits, on bars of equal height (100%) |
| Mosaic plot | How each group splits, plus how big each group is (bar widths) |