0

I have the following ggplot2 chart in R. I'm trying to bold part of the text in annotate, however I'm not having success (tried wrapping it around a double asterisk below but this clearly doesn't work in the annotate function. Does anyone know how I can bold the first sentence in this chart?

library(tidyverse)

tibble(x= 1:100, y= 1:100) %>% 
  ggplot(aes(x, y)) +
  scale_x_continuous(minor_breaks = seq(10, 100, 10)) +
  scale_y_continuous(minor_breaks = seq(10, 100, 10)) +
  geom_rect(xmin = 17, xmax=83, ymin=90, ymax=100, color='black',
            fill='#6d1d26', size=0.25) +
  annotate('text', x= 50, y=95, label= "**This sentence should be bold.** This sentence shouldn't", size=5, col = 'white') 

enter image description here

Tanga94
  • 695
  • 6
  • 27
  • 1
    These things are bit easier using [ggtext](https://github.com/wilkelab/ggtext) . Example https://stackoverflow.com/questions/63540258/text-formatting-in-ggplots-annotate . e.g. `annotate(geom = "richtext", label = "This sentence should be bold. This sentence shouldn't", fill = NA, label.color = NA, x = 50, y = 95)` – user20650 Sep 05 '21 at 15:22

1 Answers1

1

You can also try:

library(tidyverse)
library(ggtext)
tibble(x= 1:100, y= 1:100) %>% 
  ggplot(aes(x, y)) +
  scale_x_continuous(minor_breaks = seq(10, 100, 10)) +
  scale_y_continuous(minor_breaks = seq(10, 100, 10)) +
  geom_rect(xmin = 17, xmax=83, ymin=90, ymax=100, color='black',
            fill='#6d1d26', size=0.25) +
  geom_richtext(aes(x= 50, y=95, label= "**This sentence should be bold.** This sentence shouldn't"),
                size=5, col = 'white',fill='black')

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84