Classwork 13

Distribution of a Categorical Variable Across Groups

Author

Byeong-Hak Choe

Published

October 21, 2026

NoteNote

Complete the questions and submit your responses on the corresponding Brightspace page.

Fill each ____ with the appropriate variable, function, or value. Keep your completed code and brief interpretations together in an R script. This page has its own setup; no earlier classwork is required.

Setup and data

library(tidyverse)
library(ggthemes)

flights <- nycflights13::flights
flight_months <- flights |>
  filter(month == 1 | month == 7)

flight_months contains 56,429 historical flight records from January and July 2013, across all airlines in the data. Some records have no actual departure time, so these are flight-record counts rather than counts of completed departures. They are not passenger counts.

Variable Meaning
origin Departure airport: EWR = Newark, JFK = John F. Kennedy, LGA = LaGuardia
month 1 = January, 7 = July

factor(month) treats January and July as two categories. To read the full variable descriptions in R, run ?nycflights13::flights.

1. Compare monthly totals with stacked bars

Complete the plot with one stacked bar for each month and a different color for each origin airport.

ggplot(data = flight_months,
       mapping = aes(x = factor(____),
                     fill = ____)) +
  geom_bar() +
  scale_fill_tableau()

Which month has more flight records across all three airports? What does each colored segment count, and what does the full bar height count? Why is the legend useful?

2. Find each airport’s increase

Run this code. count(origin, month) creates a six-row table of flight-record counts for each airport and month.

airport_month_counts <- flight_months |>
  count(origin, month)
origin month n
EWR 1 9893
EWR 7 10475
JFK 1 9161
JFK 7 10023
LGA 1 7950
LGA 7 8927

Complete a side-by-side bar chart so the January and July counts at each airport start from the same baseline.

ggplot(data = flight_months,
       mapping = aes(x = origin,
                     fill = factor(month))) +
  geom_bar(position = "____") +
  scale_fill_tableau()

Using the table, calculate July minus January for each airport. Which airport has the largest increase in flight-record count?

3. Read airport shares within a month

Complete a 100% stacked bar chart with one bar for January and one for July. Each bar shows the airports’ shares within that month.

ggplot(data = flight_months,
       mapping = aes(x = factor(____),
                     fill = ____)) +
  geom_bar(position = "____") +
  scale_fill_tableau()

For the LGA section of the July bar, give the numerator, denominator, and approximate percentage using the count table. Do the equal total heights of the two bars mean January and July had equal flight-record counts? Explain.

4. Reverse the grouping

Now place airports on the horizontal axis and use color for month. Complete another 100% stacked bar chart.

ggplot(data = flight_months,
       mapping = aes(x = ____,
                     fill = factor(____))) +
  geom_bar(position = "____") +
  scale_fill_tableau()

For the July section of the LGA bar, give the numerator, denominator, and approximate percentage. Why is this percentage different from the LGA July percentage in Question 3, even though both use the same LGA July records? Which plot answers “What share of July records came from LGA?” Which answers “What share of LGA’s January-and-July records occurred in July?”

Back to top