Hi Stephanie,
Have you thought about trying the ggtext
package? That might be the easiest and clearest way to achieve what you're after. It lets you write markdown syntax rather than have to faff on with expressions!
library(tidyverse)
library(glue)
library(ggtext)
mpg_alt <- mpg %>%
mutate(
BoldLabel = str_ends(class, "t"),
StyledClass = if_else(BoldLabel, glue("<b>{class}</b>"), glue("<i>{class}</i>"))
)
mpg_alt %>%
ggplot(aes(StyledClass)) +
geom_bar() +
theme(axis.text.x = ggtext::element_markdown())
Created on 2022-01-11 by the reprex package (v2.0.1)