8

I want to edit the title of my plot so it has four words with only the last ones being bold, example: Title: "This is" (normal font) "my plot" (bold).

I have tried several codes I found online but I only managed to make all of the title for the plot bold.My code (example) is looking something like this as I also want to change thee colour and the position of the title. Right now all of the title is in bold due to "face=bold" in my code. As explained above I would only like the last two words be in bold, yet in one line, so no subtitle or another line below. I am using ggplot2 and help will be greatly appreciated!

plot4 <- plot4 + labs(title = "This is my plot")

plot4 <- plot4 + theme(plot.title=element_text(hjust=0.5, vjust=0.1, face='bold', colour="blue"))
Rui Barradas
  • 57,195
  • 8
  • 29
  • 57
Sarah A
  • 83
  • 1
  • 4
  • 2
    Have a look at https://cran.r-project.org/web/packages/latex2exp/vignettes/using-latex2exp.html – Christoph Jul 28 '19 at 19:54
  • @Christoph An answer based on package `latex2exp` could be useful to others. Care to post one? – Rui Barradas Jul 28 '19 at 20:09
  • very related https://stackoverflow.com/questions/32555531/how-to-italicize-part-one-or-two-words-of-an-axis-title?answertab=trending#tab-top – tjebo Apr 14 '22 at 12:15

2 Answers2

6

Use plotmath as documented by R documentation and in the ggplot2 wiki.

library(ggplot2)

p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, colour = Species)) +
  geom_point()

p + labs(title = bquote('This is' ~ bold('my plot')))

enter image description here

Rui Barradas
  • 57,195
  • 8
  • 29
  • 57
4

You can also use the latex2exp package:

library(ggplot2)
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, colour = Species)) +
  geom_point()
p + labs(title = latex2exp::TeX("$\\alpha = 5$ text, then \\textbf{bold}"))

or

plot(0, 0, main = latex2exp::TeX("$\\alpha = 5$ text, then \\textbf{bold}"))

with the same effect but more flexibility.

Christoph
  • 6,529
  • 3
  • 38
  • 79
  • Very nice! Specially for latex users like me, I always have to go to the plotmath docs and even like that it can take a while to get it right. – Rui Barradas Jul 29 '19 at 07:51
  • This is the only solution that worked for me when passing the "bold string" as a variable. E.g. inside a function that plots several things and you want to make bold the part of the title that changed – kael Aug 06 '20 at 14:26
  • @ahorn Now I got it - I didn't look at the first line as the example was correct... BTW: You can edit a question / answer in such a case ;-) – Christoph Sep 04 '20 at 10:08