Day 2B Practice
Question 1
Load the
txhousing
tibble (included in {tidyverse}). This contains the number of homes listed (listings
) and the typical home sales price (median
) in multiple Texas cities and months. Create a summary tibble that contains the average number of listings and the average typical home sales price across all cities and months. Note: For these descriptive purposes, you can ignore missing values.Modify your answer to Question 1a to generate the same summary statistics but per city this time (still averaging across all months).
Click here for the answer key
Answer (a)
|> txhousing summarize( listings = mean(listings, na.rm = TRUE), median = mean(median, na.rm = TRUE) )
Answer (b)
|> txhousing group_by(city) |> summarize( listings = mean(listings, na.rm = TRUE), median = mean(median, na.rm = TRUE) )
Question 2
For each of the following code snippets, find and fix the error(s).
- “The Scatterbrained Scatterplot”
ggplot(mpg, x = displ, y = hwy) +
geom_point()
- “Not-so-smooth Smoothing”
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point()
geom_smooth() +
scale_y_continuous("Highway MPG") +
scale_x_discrete("Engine Size")
- “A Leaky Pipeline”
ggplot(economics, aes(x = date, y = unemploy)) |>
geom_line() |>
geom_point()
Click here for the answer key
Answer (a)
The code forgot to put the mappings inside
aes()
.ggplot(mpg, aes(x = displ, y = hwy)) + # fix 1 geom_point()
Answer (b)
The code forgot a
+
and set the x-axis to discrete instead of continuous.ggplot(mpg, aes(x = displ, y = hwy)) + geom_point() + # fix 1 geom_smooth() + scale_y_continuous("Highway MPG") + scale_x_continuous("Engine Size") # fix 2 #> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
Answer (c)
The code used pipes instead of plus signs to connect ggplot2 commands.
ggplot(economics, aes(x = date, y = unemploy)) + # fix 1 geom_line() + # fix 2 geom_point()
Question 3
Install and load the {palmerpenguins} package.
Recreate the following graphic as closely as you can from the
penguins
tibble in that package (don’t worry about getting the geom settings exactly right).Clean up the x and y axis titles so that they are more readable (e.g., “Flipper length (mm)” instead of “flipper_length_mm”). Bonus: See if you can figure out how to also rename the legend title.
Click here for the answer key
Answer (a)
# Extra pane > Packages tab > Install button > palmerpenguins > Install library(palmerpenguins)
Answer (b)
ggplot(penguins, aes(x = flipper_length_mm, y = bill_length_mm, color = species, shape = species)) + geom_point(size = 3, alpha = 0.75)
Answer (c)
<- ggplot(penguins, aes(x = flipper_length_mm, y = bill_length_mm, p color = species, shape = species)) + geom_point(size = 3, alpha = 0.75) + scale_x_continuous(name = "Flipper length (mm)") + scale_y_continuous(name = "Body mass (g)") + scale_color_discrete(name = "Penguin species") + scale_shape_discrete(name = "Penguin species") p
Or, use this shortcut:
<- ggplot(penguins, aes(x = flipper_length_mm, y = bill_length_mm, p color = species, shape = species)) + geom_point(size = 3, alpha = 0.75) + labs( x = "Flipper length (mm)", y = "Body mass (g)", color = "Penguin species", shape_ = "Penguin species" ) p
Warning: Removed 2 rows containing missing values (geom_point).
Question 4
Use your graphic from Question 3(c) a starting graphic object. Apply the “classic” complete theme to it and move the legend to the top. Bonus: Also make the axis titles italic (you may need to check a cheatsheet or do some searching to find the right element).
Export this graphic as an image file to include in a paper. Make it 6.5 inches wide and 4 inches high. Bonus: Open Microsoft Word or Google Docs and insert the image.
Click here for the answer key
Answer (a)
<- p2 + p theme_classic() + theme( legend.position = "top", axis.title = element_text(face = "italic") # bonus ) p2
Answer (b)
ggsave("practice.png", plot = p2, width = 6.5, height = 4, units = "in")
Resources
R4DS Chapter 5 (§5.6–§5.7): Read more about grouped summaries and mutates
R4DS Chapter 12: Read more about reshaping and tidying data in R
R4DS Chapter 3: Read an introduction to visualization
GGP2 Chapter 11: Read more about color in ggplot2
GGP2 Chapter 18: Read more about themes in ggplot2
Fun Stuff
Let’s Enhance
If you want to be able to do this, save your images as .pdf
or .svg
…
Don McMillan’s Greatest Charts
Would these be more or less funny if he had used ggplot2… ?