2

may be easy but can't figure it out:

I want to add two subtitles to my plot and make the first bold but I can't find a way to do it without scrambling everything around. I tried this:

ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(title = expression(""*bold("Title")),
       subtitle = expression(""*bold("First subtitle")*"\nSecond subtitle"))

But for the second subtitle the \n does not work and stays on the same line. How do I put it below without making it bold? Thanks in advance!

maplesyrup123
  • 303
  • 1
  • 6

2 Answers2

4

Alternatively, you can use element_markdown from ggtext. There, you need to use <br> to create a line break.

library(ggplot2)
library(ggtext)
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  theme(
    plot.title = element_markdown(),
    plot.subtitle = element_markdown()
  ) +
  labs(title = "**Title**",
       subtitle = "**First subtitle**<br>Second subtitle")

Created on 2023-01-17 by the reprex package (v1.0.0)

starja
  • 9,887
  • 1
  • 13
  • 28
  • I cannot install ```ggtext``` for some reason... I get this output: ```WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding``` I downloaded Rtools 4.3. but still get the same error... – maplesyrup123 Jan 17 '23 at 14:53
  • 1
    Which R Version do you use? According to the [download page](https://cran.r-project.org/bin/windows/Rtools/), Rtools 4.3 is only for R 4.3.0 – starja Jan 17 '23 at 15:03
2

How about this:

library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  labs(title = expression(bold(Title)),
       subtitle = expression(atop(bold(First~subtitle),~~~~Second~subtitle)))

Created on 2023-01-17 by the reprex package (v2.0.1)

This answer proposed the use of atop() instead of a line break.

DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25