0

我想在 ggplot2 中创建一个带有一些自定义彩色单词的字幕。我们可以使用 crayon::make_style、glue::glue 和glue::glue_col 在控制台中实现这一点(见下文)。但是,在图形设备中实现时,颜色不会呈现。我查看了源代码以获取提示,但没有找到。有什么想法吗?

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\")})}\')}
")

这是cust_subt对象在控制台中的打印方式。

在此处输入图像描述

但是,一旦我们尝试绘图,它就会失败。

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

在此处输入图像描述

4

0 回答 0