---
title: "Bill dimensions across Antarctic penguin species"
author: "Your name"
format: html
---

```{r}
#| label: setup
#| message: false
library(dplyr)
library(ggplot2)
library(gt)
library(ggokabeito)   # color-blind-safe (Okabe-Ito) scale

data(penguins)
penguins <- penguins |> filter(!is.na(bill_len), !is.na(bill_dep))
```

## The data

We measured **`{r} nrow(penguins)`** penguins of `{r} length(unique(penguins$species))` species
(`{r} knitr::combine_words(levels(penguins$species))`), collected at Palmer Station, Antarctica. The
data are available through the **palmerpenguins** package and are now also included in base R's
`datasets` package.

The species are not evenly represented:

```{r}
#| label: counts
#| column: margin
penguins |>
  count(species, name = "n") |>
  knitr::kable()
```

## Bill shape separates the species

@fig-bill shows that within a species the bill has a characteristic shape, so the species form
clusters.

```{r}
#| label: fig-bill
#| fig-width: 5
#| warning: false
#| fig-cap: "Bill length versus depth, colored by species."
#| fig-alt: >-
#|   Scatter plot of bill depth against bill length for penguins of three species, each drawn in a
#|   distinct color and shape. The three species form largely separate clusters.
ggplot(penguins, aes(bill_len, bill_dep, color = species, shape = species)) +
  geom_point(alpha = 0.8) +
  scale_color_okabe_ito() +
  labs(x = "Bill length (mm)", y = "Bill depth (mm)", color = "Species", shape = "Species") +
  theme_minimal(base_size = 12)
```

A compact descriptor of bill shape is the length-to-depth ratio (@eq-ratio):

$$
\text{ratio} = \frac{\text{bill\_len}}{\text{bill\_dep}}
$$ {#eq-ratio}

## Mean measurements

@tbl-summary reports the mean bill measurements and body mass per species.

```{r}
#| label: tbl-summary
#| tbl-cap: "Mean measurements per species."
penguins |>
  summarise(
    n = n(),
    bill_len = mean(bill_len),
    bill_dep = mean(bill_dep),
    body_mass = mean(body_mass, na.rm = TRUE),
    .by = species
  ) |>
  gt() |>
  cols_label(species = "Species", n = "N", bill_len = "Bill length (mm)",
             bill_dep = "Bill depth (mm)", body_mass = "Body mass (g)") |>
  fmt_number(columns = c(bill_len, bill_dep), decimals = 1) |>
  fmt_number(columns = body_mass, decimals = 0)
```

## Session {.appendix .unnumbered}

<details>
<summary>Session info</summary>

```{r}
#| label: session
sessionInfo()
```

</details>
