library(tidyverse)
mpg

Your Turn 1

Run the code on the slide to make a graph. Pay strict attention to spelling, capitalization, and parentheses!

ggplot(data = mpg) +
  geom_point(mapping = aes(x = displ, y = hwy))

Your Turn 2

Add color, size, alpha, and shape aesthetics to your graph. Experiment.

mpg %>% 
  ggplot() +
  geom_point(mapping = aes(x = displ, y = hwy,
                           color = class,
                           size = cyl,
                           shape = drv,
                           alpha = hwy))

Your Turn 3

Replace this scatterplot with one that draws boxplots. Use the cheatsheet. Try your best guess.

mpg %>% 
  ggplot() + 
  geom_point(aes(class, hwy))

mpg %>% 
  ggplot() + 
  geom_boxplot(aes(class, hwy))

Your Turn 4

Make a histogram of the hwy variable from mpg.

mpg %>% 
  ggplot() +
  geom_histogram(aes(hwy))
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

mpg %>% 
  ggplot() +
  geom_histogram(aes(hwy), binwidth = 2)

Your Turn 5

Make a density plot of hwy colored by class.

mpg %>% 
  ggplot() +
    geom_density(mapping = aes(x = hwy, color = class))

Your Turn 6

Make a bar chart hwy colored by class.

mpg %>% 
  ggplot() +
  geom_bar(mapping = aes(x = class, color = class))

mpg %>% 
  ggplot() +
  geom_bar(mapping = aes(x = class, fill = class))

Your Turn 7

Predict what this code will do. Then run it.

mpg %>% 
  ggplot() + 
  geom_point(aes(displ, hwy)) +
  geom_smooth(aes(displ, hwy))
`geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Your Turn 8

Save the last plot.

ggsave("mylastplot.png")
# or right-click the image