View source: R/set-geom-fonts.R
| set_geom_fonts | R Documentation |
ggplot2 text and label geomsThis 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.
set_geom_fonts(family = NULL, ggrepel = FALSE)
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 |
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()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.