Summarizing Data with R - Extra Practice
- You do not have to do all of these. Choose the datasets and plots that give you useful practice.
- Try each problem before opening its answer. Every code cell runs directly in your browser.
- In every dataset, the unit of observation means what a single row represents.
- The plots below are starting points. Add useful axis labels, legend titles, and other improvements when you can.
Mammal sleep
The msleep data frame comes with ggplot2 (and is loaded with the tidyverse).
1. Meet the data. Open the help page for msleep. Identify the unit of observation—that is, what a single row represents—and briefly summarize the kinds of data recorded. Then inspect the data frame itself.
Show answer
Each row represents one mammal species. The variables describe taxonomy, diet, conservation status, sleep, and body measurements.
2. One categorical variable. Make a basic bar plot of vore, which records eating behavior. Which category is most common among species with a recorded value? Note how missing values appear.
Show answer
Herbivores are the most common recorded group. Missing values get their own bar.
3. One numerical variable. Make a histogram of sleep_total using a bin width of 2 hours. Describe its shape, center, and spread.
Show answer
The distribution is broadly unimodal with substantial spread; most species sleep about 6–14 hours, with some much lower and higher values.
4. Numerical by categorical. Use violin plots to compare the distribution of sleep_total across levels of vore. Put vore on the y-axis.
Show answer
5. Interpret the relationship. Write one sentence summarizing the relationship between eating behavior and total sleep. Mention at least one difference or similarity between the distributions.
Show answer
For example: insectivores and herbivores tend to sleep more than omnivores, although the distributions overlap considerably and some groups contain few species.
6. Two categorical variables. Make a normalized bar plot with vore on the x-axis and conservation mapped to fill.
Show answer
7. Interpret the relationship. Write one sentence summarizing the relationship between eating behavior and conservation status. State whether the conditional proportions suggest an association, interpreting cautiously because several combinations have few species or missing values.
Show answer
The conservation-status proportions vary across eating-behavior groups, suggesting an association in these data, but missing values and small group sizes make strong conclusions inappropriate.
Diamonds
The diamonds data frame also comes with ggplot2.
1. Meet the data. Open the help page for diamonds. Identify the unit of observation—what a single row represents—and summarize the kinds of measurements recorded. Then display the data frame itself.
Show answer
Each row represents one diamond. The variables describe price and physical properties including carat, cut, color, clarity, and dimensions.
2. One categorical variable. Make a basic bar plot of clarity. Are the levels displayed in a meaningful order? Use the description of clarity on the dataset’s help page to help answer.
Show answer
clarity is an ordered factor, and the bars follow the order described on the help page.
3. One numerical variable. Make a density plot of price. Describe its modality and skew.
Show answer
Diamond price is strongly right-skewed, with most prices toward the low end and a long tail of expensive diamonds.
4. Numerical by categorical. Use boxplots to compare price across levels of cut. Put cut on the y-axis.
Show answer
5. Interpret the relationship. Write one sentence summarizing the relationship between cut quality and price. Refer to typical prices, spread, or unusual observations as appropriate.
Show answer
For example: prices overlap greatly across cut categories, and the typical price does not simply rise with cut quality; every group is right-skewed and contains expensive observations.
6. Two categorical variables. Make a normalized bar plot with color on the x-axis and cut mapped to fill. Explain what the chart conditions on.
Show answer
The chart conditions on color.
7. Interpret the relationship. Write one sentence summarizing the relationship between color and cut quality. State whether the conditional proportions suggest an association.
Show answer
The proportions of cut quality vary somewhat among color grades, suggesting an association between color and cut in these data.
Guinea pig tooth growth
ToothGrowth is a dataset built into R. The variable dose uses numbers, but those numbers identify three experimental dose groups. For plotting categories, use factor(dose).
1. Meet the data. Open the help page for ToothGrowth. Identify the unit of observation—what a single row represents—and summarize the experiment and its variables. Inspect the data frame.
Show answer
Each row represents one guinea pig assigned a vitamin C supplement method and dose, with its tooth length recorded.
2. One categorical variable. Make a basic bar plot of the dose groups. Map factor(dose) to the x-axis. How many observations are in each group?
Show answer
There are 20 observations at each dose.
3. One numerical variable. Make a dot plot of tooth length len. Choose a reasonable binwidth, trying several values if needed.
Show answer
4. Numerical by categorical. Use violin plots to compare len across the three dose groups.
Show answer
5. Interpret the relationship. Write one sentence summarizing the relationship between dose and tooth length. Refer to the center and spread of the distributions as appropriate.
Show answer
Typical tooth length increases as dose increases, while the distributions retain noticeable spread within every dose group.
6. Two categorical variables. Make a dodged bar plot comparing supp across dose groups. Then decide whether a normalized plot would lead to the same conclusion.
Show answer
A normalized chart leads to the same conclusion because all dose groups have the same total count.
7. Interpret the relationship. Write one sentence summarizing the relationship between supplement method and dose group. State whether the variables appear associated and explain how the bar heights support your conclusion.
Show answer
Supplement method and dose group are not associated in the design: at every dose, the OJ and VC bars have equal heights of 10.
Motor Trend cars
mtcars is built into R. Treat cyl and am as categorical by wrapping them in factor() when plotting. For am, 0 means automatic and 1 means manual.
1. Meet the data. Open the help page for mtcars. Identify the unit of observation—what a single row represents—and summarize the kinds of automobile characteristics recorded. Inspect the data.
Show answer
Each row represents one automobile model. The variables describe fuel economy and design and performance characteristics.
2. One categorical variable. Make a basic bar plot of the number of cars with 4, 6, and 8 cylinders.
Show answer
3. One numerical variable. Make a histogram of fuel economy mpg. Select a bin width that reveals the overall shape without too much noise.
Show answer
4. Numerical by categorical. Use boxplots to compare mpg across cylinder groups.
Show answer
5. Interpret the relationship. Write one sentence summarizing the relationship between cylinder count and fuel economy. Refer to typical values and spread as appropriate.
Show answer
Cars with more cylinders tend to have lower fuel economy, with the typical mpg decreasing from four- to six- to eight-cylinder cars.
6. Two categorical variables. Make a normalized bar plot with cylinder group on the x-axis and transmission type am mapped to fill.
Show answer
7. Interpret the relationship. Write one sentence summarizing the relationship between cylinder count and transmission type. State whether the conditional proportions suggest an association, and mention the important limitation arising from this dataset’s size and selection of cars.
Show answer
Transmission proportions differ across cylinder groups, suggesting an association, but these 32 selected cars are not a random sample of all cars.
Carbon dioxide uptake in grass plants
CO2 is built into R. It contains repeated measurements: the same plant appears in multiple rows at different carbon dioxide concentrations.
1. Meet the data. Open the help page for CO2. Identify the unit of observation—what a single row represents—and summarize the experiment and variables. Be more precise than saying that one row is “a plant.”
Show answer
Each row represents an uptake measurement on one plant at one carbon dioxide concentration. Each of the 12 plants appears in seven rows.
2. One categorical variable. Make a basic bar plot of Treatment. What do the bar heights count, and why are they not counts of distinct plants?
Show answer
The bars count measurement rows, not distinct plants, because every plant was measured at seven concentrations.
3. One numerical variable. Make a density plot of uptake. Describe its shape and approximate range.
Show answer
Uptake ranges from roughly 7 to 46, with much of the distribution concentrated between about 15 and 40.
4. Numerical by categorical. Use violin plots to compare uptake between chilled and nonchilled treatments.
Show answer
5. Interpret the relationship. Write one sentence summarizing the relationship between treatment and carbon dioxide uptake. Mention at least one difference or similarity between the distributions.
Show answer
Nonchilled measurements tend to have greater carbon dioxide uptake than chilled measurements, although the distributions overlap.
6. Two categorical variables. Make a normalized bar plot with Type on the x-axis and Treatment mapped to fill. Remember what each row represents when interpreting the bar heights.
Show answer
7. Interpret the relationship. Write one sentence summarizing the relationship between plant type and treatment. State whether the conditional proportions suggest an association in the experimental design.
Show answer
Treatment proportions are identical within the Quebec and Mississippi plant types, so Type and Treatment show no association in the balanced design.