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

2
  • 1
    These things are bit easier using ggtext . Example stackoverflow.com/questions/63540258/… . e.g. annotate(geom = "richtext", label = "<b>This sentence should be bold.</b> This sentence shouldn't", fill = NA, label.color = NA, x = 50, y = 95) Commented Sep 5, 2021 at 15:22
  • This got it working for me ... fontface = "bold" Commented Feb 9, 2024 at 9:28

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.