pacman::p_load(
tidyverse,
here,
rio
)
hrs_data <- import(here("data", "hrs_data.rds"))Practice 8 Answers
PUBH 523/623
Questions
Question 1: Grammar of ggplot2
In Lesson 31, we saw that every ggplot2 plot is built from the same basic grammar: data, a mapping, and a layer.
Part A: The three required pieces
Answer:
Example for one row, fill in the rest yourself following this style:
| Component | Contribution | Function supplied |
|---|---|---|
| Data | ||
| Mapping | ||
| Layer | Determines how the mapped data actually gets drawn: as points, bars, boxplots, and so on | a geom_*() (or stat_*()) function, added with + |
Part B: Start with just the data
Answer:
Here’s an example to show you the pattern: the rest of your answers should follow this same style, with code chunks and #| ... chunk options as needed.
hrs_01_plot_data <- ggplot(hrs_data)
hrs_01_plot_dataPart C: Add a mapping
Answer:
Not given.
Part D: Add a layer
Answer:
Your plot should look like this:
Part E: Same plot, different code style
Answer:
Not given.
Question 2: Basic plotting and aesthetics
In Lesson 32, we covered different geom_*() functions and how to set or map aesthetics like color, size, and transparency.
Part A: A density plot
Answer:
Not given.
Part B: A boxplot of the same variable
Answer:
Not given.
Part C: A scatterplot of two continuous variables
Answer:
Your plot should look like this:
Part D: A line plot of the same two variables
Answer:
Not given.
Part E: A boxplot of a categorical and a continuous variable
Answer:
Start with something like:
hrs_09_plot_box <- ggplot(hrs_data, aes(x = ______, y = ______)) +
geom_boxplot()Part F: Layer jitter on top
Answer:
Not given.
Part G: Setting an aesthetic
Answer:
Start with something like:
hrs_11_plot_color_set <- ggplot(hrs_data, aes(x = _______, y = ______)) +
geom_jitter(color = ______, alpha = ______)Part H: Mapping an aesthetic
Answer:
Not given.
Question 3: Design choices for labels, facets, and themes
In Lesson 33, we covered how to polish a plot using labs(), facet_wrap()/facet_grid(), theme(), and how to save a finished plot with ggsave().
Part A: Labels
Answer:
Not given.
Part B: Facets
Answer:
Your plot should look like this:
Part C: Color
Answer:
Not given.
Part D: Themes
Answer:
Not given.
Part E: Saving your plot
Answer:
Start with something like:
ggsave(
filename = here(______, ______),
plot = ______,
width = ______,
height = ______,
units = "in",
dpi = ______
)Part F: Insert your saved plot
Answer:
Not given.