Skip to content

Instantly share code, notes, and snippets.

@francisvolh
Forked from z3tt/penguins_rainclouds.R
Last active October 29, 2024 19:41
Show Gist options
  • Save francisvolh/f69e3c90ec33fe30c9a8ea7346224664 to your computer and use it in GitHub Desktop.
Save francisvolh/f69e3c90ec33fe30c9a8ea7346224664 to your computer and use it in GitHub Desktop.
Polished raincloud plot using the Palmer penguins data
url <- "https://raw.githubusercontent.com/allisonhorst/palmerpenguins/master/man/figures/lter_penguins.png"
img <- magick::image_read((url))
pic <- grid::rasterGrob(img, interpolate = TRUE)
pal <- c("#FF8C00", "#A034F0", "#159090")
add_sample <- function(x) {
return(c(y = max(x) + .025,
label = length(x)))
}
data(penguins, package = "palmerpenguins")
penguins |>
dplyr::group_by(species) |>
dplyr::mutate(bill_ratio = bill_length_mm / bill_depth_mm) |>
dplyr::filter(!is.na(bill_ratio)) |>
ggplot2::ggplot(ggplot2::aes(x = forcats::fct_rev(species), y = bill_ratio)) +
ggdist::stat_halfeye(
ggplot2::aes(color = species,
fill = ggplot2::after_scale(colorspace::lighten(color, .5))),
adjust = .5,
width = .75,
.width = 0,
justification = -.4,
point_color = NA
) +
ggplot2::geom_boxplot(
ggplot2::aes(color = #ggplot2::stage(
species
#, after_scale = colorspace::darken(color, .1, space = "HLS"))
,
fill = ggplot2::after_scale(colorspace::desaturate(colorspace::lighten(color, .8), .4))),
width = .42,
outlier.shape = NA
) +
ggplot2::geom_point(
ggplot2::aes(color = #ggplot2::stage(
species #, after_scale = colorspace::darken(color, .1, space = "HLS"))
),
fill = "white",
shape = 21,
stroke = .4,
size = 2,
position = ggplot2::position_jitter(seed = 1, width = .12)
) +
ggplot2::geom_point(
ggplot2::aes(fill = species),
color = "transparent",
shape = 21,
stroke = .4,
size = 2,
alpha = .3,
position = ggplot2::position_jitter(seed = 1, width = .12)
) +
ggplot2::stat_summary(
geom = "text",
fun = "median",
ggplot2::aes(label = round(ggplot2::after_stat(y), 2),
color = #ggplot2::stage(
species#,
#after_scale = colorspace::darken(color, .1, space = "HLS"))
),
family = "Roboto Mono",
fontface = "bold",
size = 4.5,
vjust = -3.5
) +
ggplot2::stat_summary(
geom = "text",
fun.data = add_sample,
ggplot2::aes(label = paste("n =", ggplot2::after_stat(label)),
color = #ggplot2::stage(
species,
#after_scale = colorspace::darken(color, .1, space = "HLS"))
),
family = "Roboto Condensed",
size = 4,
hjust = 0
) +
ggplot2::coord_flip(xlim = c(1.2, NA), clip = "off") +
ggplot2::annotation_custom(pic, ymin = 2.9, ymax = 3.85, xmin = 2.7, xmax = 4.7) +
ggplot2::scale_y_continuous(
limits = c(1.57, 3.8),
breaks = seq(1.6, 3.8, by = .2),
expand = c(.001, .001)
) +
ggplot2::scale_color_manual(values = pal, guide = "none") +
ggplot2::scale_fill_manual(values = pal, guide = "none") +
ggplot2::labs(
x = NULL,
y = "Bill ratio",
title = "Bill Ratios of Brush–Tailed Penguins (*Pygoscelis* spec.)",
subtitle = "Distribution of bill ratios, estimated as bill length divided by bill depth.",
caption = "Gorman, Williams & Fraser (2014) *PLoS ONE* DOI: 10.1371/journal.pone.0090081<br>Visualization: Cédric Scherer &bull; Illustration: Allison Horst"
) +
ggplot2::theme_minimal(base_family = "Zilla Slab", base_size = 15) +
ggplot2::theme(
panel.grid.minor = ggplot2::element_blank(),
panel.grid.major.y = ggplot2::element_blank(),
axis.ticks = ggplot2::element_blank(),
axis.text.x = ggplot2::element_text(family = "Roboto Mono"),
axis.text.y = ggplot2::element_text(
color = rev(colorspace::darken(pal, .1, space = "HLS")),
size = 18
),
axis.title.x = ggplot2::element_text(margin = ggplot2::margin(t = 10),
size = 16),
plot.title = ggtext::element_markdown(face = "bold", size = 21),
plot.subtitle = ggplot2::element_text(
color = "grey40", hjust = 0,
margin = ggplot2::margin(0, 0, 20, 0)
),
plot.title.position = "plot",
plot.caption = ggtext::element_markdown(
color = "grey40", lineheight = 1.2,
margin = ggplot2::margin(20, 0, 0, 0)),
plot.margin = ggplot2::margin(15, 15, 10, 15)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment