Skip to content

Instantly share code, notes, and snippets.

@AlbertRapp
Created January 28, 2023 11:00
Show Gist options
  • Save AlbertRapp/bcc97b006d1ea3014417c3cec29981da to your computer and use it in GitHub Desktop.
Save AlbertRapp/bcc97b006d1ea3014417c3cec29981da to your computer and use it in GitHub Desktop.
after_scale_demo.R
library(ggplot2)
library(ggtext)
library(showtext)
font_add_google('Fira Sans', 'firasans')
font_add_google('Roboto Mono', 'robotomono')
font_add('fa-brands', '00_fonts/Font Awesome 6 Brands-Regular-400.otf')
showtext_auto()
update_geom_defaults("violin", list(ymin = NULL))
palmerpenguins::penguins |>
dplyr::filter(!is.na(sex)) |>
ggplot(aes(x = body_mass_g, y = species, fill = species)) +
geom_violin(aes(ymin = after_scale(y))) +
theme_minimal(base_size = 16) +
theme(
text = element_text(family = 'firasans', color = 'grey20'),
legend.position = 'none',
plot.title.position = 'plot',
plot.title = element_text(size = 24),
plot.subtitle = element_markdown(),
plot.caption = element_markdown()
) +
scale_fill_manual(values = thematic::okabe_ito(3)) +
labs(
x = 'Body weight (in g)',
y = element_blank(),
title = 'Penguin weights (by species)',
subtitle = paste0(
'This is a demo to include half-geoms with',
' <span style="font-family:robotomono;color:#0072B2;">**after_scale()**</span>',
' which I learned from',
' <span style="font-family:fa-brands;color:#0072B2;">&#xf09b;</span>',
'<span style="font-family:robotomono;color:#0072B2;">**teunbrand/ggplot_tricks**</span>'
),
caption = paste0(
'Data: palmerpenguins R Package | Visualization: ',
'<span style="font-family:fa-brands;color:#0072B2;">&#xf099;</span> rappa753'
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment