set_geom_fonts: Sets the default font for 'ggplot2' text and label geoms

View source: R/set-geom-fonts.R

set_geom_fontsR Documentation

Sets the default font for ggplot2 text and label geoms

Description

This is a helper function to quickly set the geom_text() and geom_label default fonts to the given family. There is also the option to apply the font to geom_text_repel() and geom_label_repel() if using the ggrepel package.

Usage

set_geom_fonts(family = NULL, ggrepel = FALSE)

Arguments

family

the font family to use. If no family is supplied, it will take the default font from the current ggplot theme.

ggrepel

a logical indicating whether to apply the font to text and label geoms from the ggrepel package.

Examples

library(ggplot2)
library(dplyr)
library(glue)
library(palmerpenguins)

# Load the default font (Roboto Condensed)
extrafont::loadfonts(quiet = TRUE)

p <- penguins %>%
  count(species, island, name = "n_penguins") %>%
  ggplot(aes(y = species, x = n_penguins)) +
  geom_col(aes(fill = island)) +
  geom_label(
    aes(label = glue("{island}: n = {n_penguins}")),
    position = "stack", hjust = 1
  ) +
  scale_fill_brewer(palette = "Set1") +
  scale_x_continuous(expand = expansion(mult = c(0, 0.1))) +
  labs(
    title = "Number of penguins per island",
    subtitle = "Different font",
    x = "Number of penguins", y = NULL,
    caption = "Data from the palmerpenguins package"
  ) +
  theme(legend.position = "none")

# Either set the font explicitly
set_geom_fonts(family = "Roboto Condensed")
# Or set the theme, then set_geom_fonts will use that base_family
theme_set(theme_td())
set_geom_fonts()

p + theme_td()

taylordunn/dunnr documentation built on Aug. 8, 2022, 12:28 p.m.