Skip to content

Instantly share code, notes, and snippets.

@padpadpadpad
Created August 1, 2022 14:03
Show Gist options
  • Save padpadpadpad/54ec4d6cb76fb5eced5f305d043638af to your computer and use it in GitHub Desktop.
Save padpadpadpad/54ec4d6cb76fb5eced5f305d043638af to your computer and use it in GitHub Desktop.
# quick and dirty example of not italicising the whole label.
# Italicise certain words only in facet labels in ggplot2
library(tidyverse)
# create random data frame
species <- c('Chlorella sp.', 'Buteo buteo')
d <- tibble(species = species) %>%
group_by(species) %>%
do(data.frame(x = 1:5, y = 1:5)) %>%
ungroup()
ggplot(d, aes(x, y)) +
geom_point() +
facet_wrap(~species) +
theme(strip.text = element_text(face = "italic"))
# want only certain word to be italics
# add expression to be parsed into the dataframe
# use case_when for each facet label in the dataframe.
d <- mutate(d, facet_lab = case_when(species == 'Chlorella sp.' ~ 'expression(italic("Chlorella")~sp.)',
species == 'Buteo buteo' ~ 'expression(italic("Buteo buteo"))'))
d <- mutate(d, facet_lab = case_when(species == 'Chlorella sp.' ~ 'italic("Chlorella")~sp.',
species == 'Buteo buteo' ~ 'italic("Buteo buteo")'))
# plot
ggplot(d, aes(x, y)) +
geom_point() +
facet_wrap(~facet_lab, labeller = label_parsed)
# https://gist.github.com/54ec4d6cb76fb5eced5f305d043638af
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment