2

I want to be able to pass an argument from a function into a label on a ggplot graph and for that label to be in bold. Unicode won't work because many unicode symbols don't render to pdf. Plotmath expressions can be split apart in paste(), which makes them ideal for this sort of work, however I can't work out how to get the Plotmath bold(x) expression to bold the entire compiled string.

For example, this code...

y <- 5
(g <- ggplot(data.frame(x = rnorm(100,0,1)), aes(x)) +
      geom_histogram(bins = 100, fill = "skyblue") +
      annotate("text", x = 1.5, y = 4, label = paste0("bold(x<=", y, "^{2})"), parse = T))

...yields...

enter image description here

As you can see the special character (less than or equal to) has come out fine. However the problem is that the x is bolded but not the rest of the string. How do I bold the entire string in a way that still allows me to pass the outside object y into the string?

llewmills
  • 2,959
  • 3
  • 31
  • 58
  • Here is a similar question: https://stackoverflow.com/questions/26282128/r-bold-numbers-in-plot-annotation-created-by-plotmath. Note the comment by @MrFlick --'From the `?plotmath` help page: "Note that bold, italic and bolditalic do not apply to symbols, and hence not to the Greek symbols such as mu which are displayed in the symbol font. They also do not apply to numeric constants."' – bdemarest May 15 '19 at 23:37
  • Thanks @bdemarest. So there is no solution? – llewmills May 16 '19 at 04:29
  • One thought I had is to use literal unicode characters as a work-around. Then set the whole label to bold with `fontface=2`. `+ annotate("text", x = 1.5, y = 4, label = " ≤ 5²", fontface=2)`. But I immediately ran into font problems. It may also be possible to use latex code (instead of plotmath) but a quick search suggests this will be quite a complex solution. For me personally, I would probably give up and fix this in adobe illustrator. – bdemarest May 16 '19 at 23:20

0 Answers0