library(tidyverse)Classwork 11
Distribution of a Numeric Variable Across Groups
Complete the questions and submit your responses on the corresponding Brightspace page.
Complete the code in an R script and keep your brief written interpretations with it. This page contains its own setup and data definitions; no earlier classwork is required.
Setup and data
The built-in mpg data contains 234 vehicle model configurations from model years 1999 and 2008. Each row represents a configuration, not an individual driver or purchase.
| Variable | Meaning |
|---|---|
cty |
City fuel economy in miles per gallon; higher means farther per gallon |
drv |
Drive type: f = front-wheel, r = rear-wheel, 4 = four-wheel |
Dataset reference: ggplot2 · mpg.
1. Histograms in separate panels
Make a separate histogram for each drive type. Use a bin width of 2 mpg in every panel and keep the default shared axis scales.
ggplot(data = mpg,
mapping = aes(x = cty)) +
geom_histogram(binwidth = 2) +
____(~ ____)Use facet_wrap() and drv in the blanks. Describe one difference in typical fuel economy or spread between two drive types.
2. Counts and group sizes
These groups contain different numbers of configurations. Explain why a taller count bar alone does not establish that a larger proportion of that group falls in the bin.
3. A boxplot comparison
Compare city fuel economy across drive types.
ggplot(data = mpg,
mapping = aes(x = ____,
y = ____,
fill = drv)) +
____()Describe a difference in typical fuel economy or spread. If a point appears beyond a whisker, explain why it deserves investigation rather than automatic deletion.
4. Choose a useful comparison
The histogram shows detailed distribution shape; the boxplot provides a compact summary. Which graph better supports the comparison you described, and why? Do not treat the two graphs as interchangeable.
