3

I thought this would be simple, but can't seem to find anything.

I have a ggplot, which contains a label:

labs(y = "Total Yearly Funding (£m) - 2019 Prices\n\n(log10)\n")

I want to bold everything but '(log10)'.

I thought it would be as simple as using '<b>' tags i.e.:

labs(y = "<b>Total Yearly Funding (£m) - 2019 Prices</b>\n\n(log10)\n")


But that doesn't do anything. Is what I want possible?

Many thanks

Nicholas
  • 3,517
  • 13
  • 47
  • 86
  • 1
    I would recommend looking into the [ggtext](https://github.com/clauswilke/ggtext) package. – caldwellst Jan 10 '20 at 10:02
  • Thank you Caldwellst, I had never heard of the ggtext package. This is just what I need (not just for the above problem but for some other things I had done in the past. Perfect! – Nicholas Jan 10 '20 at 10:07

2 Answers2

3

One method is to use an expression, another keyword could be italic if wanted:

p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point()
p + labs(x=(expression(paste(bold("this is bold"), " and this is not bold"))))

plot for first example


Edit: since we found another solution by commentary I want to input it here. A simple line break is possible by using atop. For further information check this old question. atop provides one line break for the plotmath expressions and centers them.

with:

labs(x=(expression(atop(bold("top"), "bottom"))))

we get:

another try

mischva11
  • 2,811
  • 3
  • 18
  • 34
  • Hey, thank you for your answer. A couple of things, is it possible to do it without the '+' char showing? ... also, the '\n\n' new line parts no longer work, is that right? Thank you again. – Nicholas Jan 10 '20 at 10:21
  • 2
    @ScoutEU just use a comma instead of +, not sure why is used it. The new line in expression ist kinda tricky. If you got just one line break try: `labs(x=(expression(atop(bold("top"), "bottom"))))`. But this centers the output. – mischva11 Jan 10 '20 at 10:23
0

How about this:

p <- ggplot(...) + ggtitle("Yeah Buddy") +
  xlab("bold_x") + ylab("Total Yearly Funding (£m) - 2019 Prices\n\n(log10)\n")
p + theme(
axis.title.y = element_text(face="bold")
)
p