library(tidyverse)
library(palmerpenguins)
library(ggtext)
penguins_labs <-
penguins %>%
group_by(species) %>%
summarize(across(starts_with("bill"), ~ mean(.x, na.rm = TRUE))) %>%
mutate(
species_lab = case_when(
species == "Adelie" ~ "<b style='font-size:15pt;'>*P. adéliae*</b><br>(Adélie penguin)",
species == "Chinstrap" ~ "<b style='font-size:15pt;'>*P. antarctica*</b><br>(Chinstrap penguin)",
species == "Gentoo" ~ "<b style='font-size:15pt;'>*P. papua*</b><br>(Gentoo penguin)"
)
)
ggplot(
penguins,
aes(x = bill_length_mm, y = bill_depth_mm,
color = species, size = body_mass_g)
) +
geom_point(alpha = .2, stroke = .3) +
geom_point(shape = 1, stroke = .3) +
geom_richtext(
data = penguins_labs,
aes(label = species_lab,
color = species,
color = after_scale(colorspace::darken(color, .4))),
family = "Roboto Condensed",
size = 3, lineheight = .8,
fill = "#ffffffab", ## hex-alpha code
show.legend = FALSE
) +
coord_cartesian(
expand = FALSE,
clip = "off"
) +
scale_x_continuous(
limits = c(30, 60),
breaks = 6:12*5
) +
scale_y_continuous(
limits = c(12.5, 22.5),
breaks = seq(12.5, 22.5, by = 2.5)
) +
scale_color_manual(
guide = "none",
values = c("#FF8C00", "#A034F0", "#159090")
) +
scale_size(
name = "Body mass:",
breaks = 3:6 * 1000,
labels = function(x) paste(x / 1000, "kg"),
range = c(.25, 4.5)
) +
labs(
x = "Bill length *(mm)*",
y = "Bill depth *(mm)*",
title = "Bill dimensions of brush-tailed penguins *Pygoscelis spec.*",
caption = "Horst AM, Hill AP, Gorman KB (2020). <span style='font-family:tabular;'>palmerpenguins</span> R package version 0.1.0"
) +
theme_minimal(
base_size = 10, base_family = "Roboto Condensed"
) +
theme(
plot.title = element_markdown(
face = "bold", size = 16, margin = margin(12, 0, 12, 0)
),
plot.title.position = "plot",
plot.caption = element_markdown(
size = 7, color = "grey50",
margin = margin(12, 0, 6, 0)
),
plot.caption.position = "plot",
axis.text = element_text(family = "Tabular"),
axis.title.x = element_markdown(margin = margin(t = 8)),
axis.title.y = element_markdown(margin = margin(r = 8)),
panel.grid.minor = element_blank(),
legend.text = element_text(color = "grey50"),
plot.margin = margin(0, 14, 0, 12),
plot.background = element_rect(fill = NA, color = "grey50", size = 1)
)