Typo fixes (#1204)

* Grammatical edits

* Grammatical edits

* Grammatical edits

* Remove ref since fn not mentioned earlier

* Typo fixes, closes #1191

* Add palmerpenguins, closes #1192

* Grammatical edits

* More grammatical edits

* Omit warning, closes #1193

* Fix link, closes #1197

* Grammatical edits

* Code style + clarify labs() args, closes #1199

* Fix year, closes #1200

* Use penguins instead, closes #1201
This commit is contained in:
Mine Cetinkaya-Rundel
2023-01-03 02:06:27 -05:00
committed by GitHub
parent 26a20c586a
commit e68098f193
7 changed files with 304 additions and 267 deletions

View File

@@ -3,6 +3,7 @@
```{r}
#| results: "asis"
#| echo: false
source("_common.R")
status("complete")
```
@@ -45,7 +46,7 @@ library(tidyverse)
You only need to install a package once, but you need to reload it every time you start a new session.
In addition to tidyverse, we will also use the **palmerpenguins** package, which includes the `penguins` dataset containing body measurements for penguins in three islands in the Palmer Archipelago.
In addition to tidyverse, we will also use the **palmerpenguins** package, which includes the `penguins` dataset containing body measurements for penguins on three islands in the Palmer Archipelago.
```{r}
library(palmerpenguins)
@@ -158,10 +159,14 @@ The following plots show the result of adding these mappings, one at a time.
#| also shows body mass on the y-axis. The values range from 3000 to
#| 6000.
ggplot(data = penguins,
mapping = aes(x = flipper_length_mm))
ggplot(data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g))
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm)
)
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
)
```
Our empty canvas now has more structure -- it's clear where flipper lengths will be displayed (on the x-axis) and where body masses will be displayed (on the y-axis).
@@ -184,8 +189,10 @@ You'll learn a whole bunch of geoms throughout the book, particularly in @sec-la
#| displays a positive, linear, relatively strong relationship between
#| these two variables.
ggplot(data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)) +
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_point()
```
@@ -229,9 +236,10 @@ Throughout the book you will make many more ggplots and have many more opportuni
#| between these two variables. Species (Adelie, Chinstrap, and Gentoo)
#| are represented with different colors.
ggplot(data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g,
color = species)) +
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)
) +
geom_point()
```
@@ -252,9 +260,10 @@ Since this is a new geometric object representing our data, we will add a new ge
#| Chinstrap, and Gentoo). Different penguin species are plotted in
#| different colors for the points and the smooth curves.
ggplot(data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g,
color = species)) +
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g, color = species)
) +
geom_point() +
geom_smooth()
```
@@ -274,8 +283,10 @@ Since we want points to be colored based on species but don't want the smooth cu
#| Chinstrap, and Gentoo). Different penguin species are plotted in
#| different colors for the points only.
ggplot(data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)) +
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_point(mapping = aes(color = species)) +
geom_smooth()
```
@@ -296,8 +307,10 @@ Therefore, in addition to color, we can also map `species` to the `shape` aesthe
#| Chinstrap, and Gentoo). Different penguin species are plotted in
#| different colors and shapes for the points only.
ggplot(data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)) +
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_point(mapping = aes(color = species, shape = species)) +
geom_smooth()
```
@@ -305,6 +318,8 @@ ggplot(data = penguins,
Note that the legend is automatically updated to reflect the different shapes of the points as well.
And finally, we can improve the labels of our plot using the `labs()` function in a new layer.
Some of the arguments to `labs()` might be self explanatory: `title` adds a title and `subtitle` adds a subtitle to the plot.
Other arguments match the aesthetic mappings, `x` is the x-axis label, `y` is the y-axis label, and `color` and `shape` define the label for the legend.
```{r}
#| warning: false
@@ -318,16 +333,18 @@ And finally, we can improve the labels of our plot using the `labs()` function i
#| roughly the same for these three species, and Gentoo penguins are
#| larger than penguins from the other two species.
ggplot(penguins,
aes(x = flipper_length_mm, y = body_mass_g)) +
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_point(aes(color = species, shape = species)) +
geom_smooth() +
labs(
title = "Body mass and flipper length",
subtitle = "Dimensions for Adelie, Chinstrap, and Gentoo Penguins",
x = "Flipper length (mm)",
x = "Flipper length (mm)",
y = "Body mass (g)",
color = "Species",
color = "Species",
shape = "Species"
)
```
@@ -342,7 +359,7 @@ We finally have a plot that perfectly matches our "ultimate goal"!
2. What does the `bill_depth_mm` variable in the `penguins` data frame describe?
Read the help for `?penguins` to find out.
3. Make a scatterplot of `bill_depth_mm` vs `bill_length_mm`.
3. Make a scatterplot of `bill_depth_mm` vs. `bill_length_mm`.
Describe the relationship between these two variables.
4. What happens if you make a scatterplot of `species` vs `bill_depth_mm`?
@@ -369,27 +386,32 @@ We finally have a plot that perfectly matches our "ultimate goal"!
```{r}
#| echo: false
#| warning: false
#| fig-alt: >
#| A scatterplot of body mass vs. flipper length of penguins, colored
#| by bill depth. A smooth curve of the relationship between body mass
#| and flipper length is overlaid. The relationship is positive,
#| fairly linear, and moderately strong.
ggplot(data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)) +
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_point(aes(color = bill_depth_mm)) +
geom_smooth()
```
9 .
Run this code in your head and predict what the output will look like.
Then, run the code in R and check your predictions.
9. Run this code in your head and predict what the output will look like.
Then, run the code in R and check your predictions.
```{r}
#| eval: false
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) +
geom_point() +
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g, color = island)
) +
geom_point() +
geom_smooth(se = FALSE)
```
@@ -399,13 +421,22 @@ Then, run the code in R and check your predictions.
```{r}
#| eval: false
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() +
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_point() +
geom_smooth()
ggplot() +
geom_point(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_smooth(data = mpg, mapping = aes(x = displ, y = hwy))
ggplot() +
geom_point(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_smooth(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
)
```
## ggplot2 calls
@@ -416,8 +447,10 @@ So far we've been very explicit, which is helpful when you are learning:
```{r}
#| eval: false
ggplot(data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)) +
ggplot(
data = penguins,
mapping = aes(x = flipper_length_mm, y = body_mass_g)
) +
geom_point()
```
@@ -759,9 +792,13 @@ You will learn about many other geoms for visualizing distributions of variables
#| one labelled "species" which shows the shape scale and the other
#| that shows the color scale.
ggplot(data = penguins,
mapping = aes(x = bill_length_mm, y = bill_depth_mm,
color = species, shape = species)) +
ggplot(
data = penguins,
mapping = aes(
x = bill_length_mm, y = bill_depth_mm,
color = species, shape = species
)
) +
geom_point() +
labs(color = "Species")
```
@@ -785,7 +822,7 @@ ggsave(filename = "my-plot.png")
file.remove("my-plot.png")
```
This will save your plot to your working directory, a concept you'll learn more about in @sec-workflow-scripts.
This will save your plot to your working directory, a concept you'll learn more about in @sec-workflow-scripts-projects.
If you don't specify the `width` and `height` they will be taken from the dimensions of the current plotting device.
For reproducible code, you'll want to specify them.