0

I would like to change the labels of my graph. ChatGPT etc. gave me this useful command:

x_labels <- c(
  bquote(bold("Arabisch gelesen, " ~ italic("Weiß"))),
  bquote(bold("Schwarz, " ~ italic("Weiß")))
)

To make the text "Weiß" also in bold, it suggest me this:

x_labels <- c(
  bquote(bold("Arabisch gelesen, " ~ bold(italic("Weiß")))),
  bquote(bold("Schwarz, " ~ bold(italic("Weiß"))))
)

But if I run it, the labels stay the same, so "Weiß" never appears bold.

Any suggestions?

user438383
  • 5,716
  • 8
  • 28
  • 43
  • Consider also trying `ggtext::element_markdown()` which will give you a lot more control over the appearance of the labels. – qdread Aug 25 '23 at 15:24

1 Answers1

2

You can use bolditalic. For example, let's make your second Weiß bold italic and leave the first as italic:

x_labels <- c(
  bquote(bold("Arabisch gelesen, " ~ italic("Weiß"))),
  bquote(bold("Schwarz, ") ~ bolditalic("Weiß"))
)

Testing, we get:

library(ggplot2)

ggplot(data.frame(x = c('a', 'b'), y = 1:2), aes(x, y)) +
  geom_col() +
  scale_x_discrete(labels = x_labels) +
  theme_bw(base_size = 20)

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • This just worked perfectly, thank you so much – Aaron Lauterbach Aug 25 '23 at 13:14
  • 1
    `quote()` can be used instead of `bquote()` here. But in any case be careful because this is undocumented use AFAIU, what is documented is the use of an "expression vector", so `expression(bold("Arabisch gelesen, " ~ italic("Weiß")), bold("Schwarz, ") ~ bolditalic("Weiß"))` might be more appropriate. – moodymudskipper Aug 25 '23 at 13:54