17

Is it possible to pass partially italicized text labels into ggplot? I have tried using the expression and italic commands (expression(paste(italic("some text")))), but these cannot be passed into a data frame because the result of the commands is not atomic. Setting the parameter fontface = "italic" also doesn't suffice, since this italicizes the entire label, rather than just a select set of characters in the label. For instance, I would like some necessarily italicized Latin phrases to be italicized in a label (such as "in vivo" in "in vivo point").

library(ggplot)
library(ggrepel)

df <- data.frame(V1 = c(1,2), V2 = c(2,4), V3 = c("in vivo point","another point"))

ggplot(data = df, aes(x = V1, y = V2)) + geom_point() + geom_text_repel(aes(label = V3))
zx8754
  • 52,746
  • 12
  • 114
  • 209
Bob
  • 451
  • 1
  • 5
  • 12
  • 7
    Is adding `fontface = "italic"` not an option: `ggplot(data = df, aes(x = V1, y = V2)) + geom_point() + geom_text_repel(aes(label = V3), fontface = "italic")` ? – Jota Jan 08 '17 at 03:31
  • 1
    To be somewhat fair to the OP, the help on those `_repel` functions fail to include all supported aesthetics directly in the text but it _does_ say _"See the documentation for those functions [geom_text/geom_label] for more details"_ – hrbrmstr Jan 08 '17 at 04:28
  • Sorry, in a rush, I forgot to include that only a substring of the label is to be italicized; I will amend the question. – Bob Jan 08 '17 at 05:14
  • 1
    If you use `parse = TRUE` you can use `?plotmath`, though you'll have to reconfigure your labels. – alistaire Jan 08 '17 at 05:55
  • 1
    Thank you! As an aside, this actually appears not to work in the current stable version of ggrepel, but the development version supports plotmath expressions. (https://github.com/slowkow/ggrepel/issues/60). If you would like, you can post as an answer so I can mark your answer as correct. – Bob Jan 08 '17 at 06:45

1 Answers1

14

You can use parse = TRUE to pass ?plotmath expressions (as strings) to geom_text or geom_text_repel. You'll have to rewrite the strings as plotmath, but if it's not too many it's not too bad.

df <- data.frame(V1 = c(1,2), V2 = c(2,4), 
                 V3 = c("italic('in vivo')~point", "another~point"))

ggplot(data = df, aes(x = V1, y = V2, label = V3)) + 
    geom_point() + 
    geom_text_repel(parse = TRUE)

plot with partial italic label

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • 2
    `element_text()` has an argument called `face`. I am not sure why `geom_text()` doesn't have this option. – mindlessgreen Apr 30 '18 at 21:19
  • FYI, to see possible rendering options: `demo(plotmath)` – Megatron Apr 21 '20 at 15:39
  • 2
    geom_text() calls the option fontface -- http://www.cookbook-r.com/Graphs/Fonts/ – hd1 Jul 10 '20 at 20:55
  • 3
    @hd1 From the question: "Setting the parameter `fontface = "italic"` also doesn't suffice, since this italicizes the entire label, rather than just a select set of characters in the label." – alistaire Jul 11 '20 at 00:31