I'd like to create a subtitle with a few custom colored words in ggplot2. We can use crayon::make_style, glue::glue, and glue::glue_col to make this happen in the console (see below). However, when implementing in the graphics device, the color doesn't render. I looked through the source code for a hint but failed to find one. Any thoughts?
library(glue)
library(crayon)
library(tidyverse)
# create data frame
myDat <- tibble(alpha = rnorm(50),
omega = runif(50))
# use crayon to create our custom colors
slate_g <- crayon::make_style("slategrey")
light_s <- make_style("lightsalmon")
# create custom subtitle NOTE it prints cleanly in the console
cust_subt <- glue("
{str_wrap(\"It is a wonder that this subtitle is so dangerously long. Good thing we can wrap it in glue.\", 75)}
Really makes me wonder
{glue_col(\'{bold({slate_g(\"NEVER\")})} vs. {bold({light_s(\"ENOUGH\")})}\')}
")
Here is how the cust_subt
object prints in the console.
Once we try to plot, though, it fails.
# custom subtitle DOES NOT work in subtitle
ggplot(myDat, aes(alpha, omega)) +
geom_point() +
labs(subtitle = cust_subt)
#### regular glue works just fine though
cust_subt_no_col <- glue("
{str_wrap(\"It is a wonder that this subtitle is so dangerously long. Good thing we can wrap it in glue.\", 75)}
Really makes me wonder
")
ggplot(myDat, aes(alpha, omega)) +
geom_point() +
labs(subtitle = cust_subt_no_col)