How do I include italic text in geom_text_repel or geom_text labels for ggplot?

15,003

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

Share:
15,003
Bob
Author by

Bob

Updated on June 26, 2022

Comments

  • Bob
    Bob about 2 years

    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))
    
    • Jota
      Jota over 7 years
      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") ?
    • hrbrmstr
      hrbrmstr over 7 years
      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"
    • Bob
      Bob over 7 years
      Sorry, in a rush, I forgot to include that only a substring of the label is to be italicized; I will amend the question.
    • alistaire
      alistaire over 7 years
      If you use parse = TRUE you can use ?plotmath, though you'll have to reconfigure your labels.
    • Bob
      Bob over 7 years
      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. (github.com/slowkow/ggrepel/issues/60). If you would like, you can post as an answer so I can mark your answer as correct.
  • mindlessgreen
    mindlessgreen about 6 years
    element_text() has an argument called face. I am not sure why geom_text() doesn't have this option.
  • Megatron
    Megatron about 4 years
    FYI, to see possible rendering options: demo(plotmath)
  • hd1
    hd1 almost 4 years
    geom_text() calls the option fontface -- cookbook-r.com/Graphs/Fonts
  • alistaire
    alistaire almost 4 years
    @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."