Lab 3: Graphics with ggplot2
Map, smooth, fix overplotting, and save a faceted boxplot.
Work in a Quarto document. Number the exercises.
Keep the plots. Do 3.1 through 3.3
first. You will need ggplot2 (from
library(tidyverse)), mpg, iris,
and diamonds.
library(tidyverse)
data("mpg")
data("iris")
data("diamonds")
1. Mappings
- What happens when you map a categorical variable to
sizeoralpha? Try it onmpgwithdrv. Write one sentence about whether the mapping is a good idea. - Look at
head(iris). Name the variables and say whether each is quantitative or categorical. - Scatterplot petal length vs petal width, colored by species. What do you notice?
- What happens when you map a quantitative variable to
shape? Tryshape = ctyon anmpgscatterplot ofdisplvshwy. - On
iris, plot petal length vs petal width, and also map species, sepal length, and sepal width. Is the plot still readable?
2. Smooths and overplotting
- Scatterplot petal length vs petal width. Color the points by
species. Add one linear smooth
(
method = lm,se = FALSE) that does not use species. - Plot
pricevscaratindiamonds. Show the overplotted version and a version that you can actually read.
3. Distributions and boxplots
- Histogram of diamond
price, filled bycut. Trybins = 50. - Boxplot of
pricevscuton a log-yscale. Does anything surprise you? Add a second plot that helps explain it.
4. Facets, theme, and save
- Boxplots of
pricevscut, log-y, faceted bycolor. - Same plot: orange fill,
theme_bw(). - Save the plot from (2) with
ggsave(). Put it in a folder you already have.ggsave()overwrites without asking.