10

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
  • 70,273
  • 8
  • 34
  • 66
Sarah A
  • 103
  • 1
  • 5
  • 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
  • 70,273
  • 8
  • 34
  • 66
  • How do you set it up if the bold section comes first and the non-bold section comes second? – canderson156 Jul 12 '23 at 16:58
  • @canderson156 Does `bquote('This is' ~ bold('my plot') ~ 'revised')` answer the question? – Rui Barradas Jul 12 '23 at 17:08
  • I'm wondering which would be correct: bquote( ~bold('This is') ~ 'my plot') or bquote( bold('This is') ~ 'my plot'). – canderson156 Jul 13 '23 at 18:13
  • @canderson156 The latter, `bquote( bold('This is') ~ 'my plot')`. The tilde puts a space between the two strings and in this case (and in general) that's what you want. And 2, 3, etc tildes will put an even greater space between the strings, try `p + labs(title = bquote(bold('my plot') ~~~ "revised"))`. – Rui Barradas Jul 13 '23 at 22:08
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,841
  • 4
  • 37
  • 89
  • 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