rggplot2ggtext

Can I outline a single word in a title of a ggplot?


I would like to highlight a word in a title of a ggplot. I have been working with the ggtext package. With {ggtext} I can set other aspects of the style but I can't figure out how to get a highlight/outline to work.

Here is what I have tried:

library(ggplot2)
library(ggtext)

ggplot() +
  labs(title = "This is my 
       <span style='color:pink text-shadow: -1px -1px 0 black, 1px -1px 0 black, 
                    -1px 1px 0 black, 1px 1px 0 black'
       >first</span> try") +
  theme_minimal() +
  theme(
    plot.title = element_markdown(size = 16)
  )

and

ggplot() +
  geom_point() +
  labs(title = "This is my <span style='color: green; 
                                        -webkit-text-stroke: 10px black;
                                        text-stroke: 1px black'>second</span> try") +
  theme_minimal() +
  theme(
    plot.title = element_markdown(size = 16)
  )

Is there a way to outline a single word with {ggtext} or some other package?


Solution

  • ggtext has only a limited support of CSS rules (see here):

    The CSS properties color, font-size, and font-family are currently supported.

    Additionally, I'm not aware of an option to add an outline to a single word using {ggtext}. But a more recent option to achieve this would be to use the {marquee} package:

    library(ggplot2)
    library(marquee)
    
    .title <- "This is my {.my_style first} try"
    
    title_style <- modify_style(
      classic_style(),
      "my_style",
      color = "pink",
      border = "black",
      border_size = trbl(em(0.1)),
      padding = trbl(em(0.15))
    )
    
    ggplot() +
      labs(title = .title) +
      theme_minimal() +
      theme(
        plot.title = element_marquee(
          size = 16,
          style = title_style
        )
      )
    

    enter image description here