# Grammar of graphics II

Author

Jeffrey R. Stevens

Published

April 3, 2023

1. Using the `mtcars` data, create a scatterplot of the fuel efficiency as a function of weight.
``library(tidyverse)``
``````── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.0     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors``````
``````mtcars |>
ggplot(aes(x = wt, y = mpg)) +
geom_point()``````
1. Repeat the scatterplot, but make the points violet open squares of size 5.
``````mtcars |>
ggplot(aes(x = wt, y = mpg)) +
geom_point(color = "violet", shape = 0, size = 5)``````
1. Repeat the scatterplot but with separate colors for cylinder levels.
``````mtcars |>
ggplot(aes(x = wt, y = mpg, color = cyl)) +
geom_point()``````
1. Why does the legend look like that? Fix it so there are separate colors for cylinder levels.
``````mtcars |>
ggplot(aes(x = wt, y = mpg, color = as.factor(cyl))) +
geom_point()``````
1. Overlay separate regression lines for each cylinder level.
``````mtcars |>
ggplot(aes(x = wt, y = mpg, color = as.factor(cyl))) +
geom_point() +
geom_smooth(method = "lm")``````
```geom_smooth()` using formula = 'y ~ x'``
1. Overlay a single firebrick regression line over the points with a firebrick1-colored confidence band.
``````mtcars |>
ggplot(aes(x = wt, y = mpg)) +
geom_point(aes(color = as.factor(cyl))) +
geom_smooth(method = "lm", color = "firebrick", fill = "firebrick1")``````
```geom_smooth()` using formula = 'y ~ x'``
1. Plot the mean and standard error of the mean of fuel efficiency for each level of cylinder and color them sienna.
``````mtcars |>
ggplot(aes(x = as.factor(cyl), y = mpg)) +
stat_summary(color = "sienna")``````
``No summary function supplied, defaulting to `mean_se()```