0

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.

enter image description here

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)

enter image description here

#### 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)

enter image description here

Zoe
  • 27,060
  • 21
  • 118
  • 148
glaucon
  • 238
  • 3
  • 10
  • lol...you didn't read this very closely...that's not the issue. I only want some words a specific color. And not a simple crayon color, either – glaucon Feb 13 '19 at 00:05
  • For non-`glue` alternatives, see [ggplot2: color individual words in title](https://stackoverflow.com/questions/49735290/ggplot2-color-individual-words-in-title) and links therein. – Henrik Feb 13 '19 at 00:10
  • yes, thank you...I am specifically interested in glue & crayon as they provide a cleaner implementation in my view. – glaucon Feb 13 '19 at 00:11
  • 2
    `crayon` functions are used to "add color to _terminal_ output". Thus, I doubt they play well with `ggplot` titles. – Henrik Feb 13 '19 at 00:33
  • indeed. I am curious 1) if crayon can be integrated into the graphics device and 2) why this happens from a coding standpoint. – glaucon Feb 13 '19 at 00:41
  • 1
    As far as I know, `crayon` has nothing to do with graphics devices at all, and there is not expectation that this should do anything for plotting. – Axeman Feb 13 '19 at 04:00

0 Answers0