── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.2.1 ✔ readr 2.2.0
✔ forcats 1.0.1 ✔ stringr 1.6.0
✔ ggplot2 4.0.3 ✔ tibble 3.3.1
✔ lubridate 1.9.5 ✔ tidyr 1.3.2
✔ purrr 1.2.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
AE 02: Penguin peekaboo
Suggested answers
For this application exercise, we’ll use the tidyverse and palmerpenguins packages.
We will again be working with the penguins dataset for this application exercise. Let’s glimpse() at the dataset to remind ourselves of what data we have at our fingertips.
-
Your turn: Replace
# add code herewith the code for “glimpse”ing at the datapenguinsdata frame –glimpse(penguins).
glimpse(penguins)Rows: 344
Columns: 8
$ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adel…
$ island <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgerse…
$ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, …
$ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, …
$ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186…
$ body_mass_g <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, …
$ sex <fct> male, female, female, NA, female, male, female, male…
$ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007…
Task 1
In the code chunk below, plot a histogram of the body mass of the penguins. Let’s also facet by island:
All plots created in this course must be properly labeled (i.e., human readable labels)! This means both x- and y-axes (when appropriate) and an informative but concise title! Double check your rendered document to ensure your plot labels are fully visible.
ggplot(penguins, aes(x = body_mass_g)) +
geom_histogram() +
labs(x = "Body Mass (g)", title = "Body Mass (g) of Penguins Across Different Islands") +
facet_wrap(~island)`stat_bin()` using `bins = 30`. Pick better value `binwidth`.
Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_bin()`).
Task 2
In the code chunk below, plot a scatterplot with body mass on the horizontal and flipper length on the vertical, colored by species:
ggplot(penguins, aes(x = body_mass_g, y = flipper_length_mm, color = species)) +
geom_point() +
labs(x = "Body Mass (g)",
y = "Flipper Length (mm)",
col = "Species",
title = "Flipper Length by Body Mass of Different Species of Penguins")Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).