How do I include italic text in geom_text_repel or geom_text labels for ggplot?
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)
Bob
Updated on June 26, 2022Comments
-
Bob about 2 years
Is it possible to pass partially italicized text labels into ggplot? I have tried using the
expression
anditalic
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 parameterfontface = "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 over 7 yearsIs 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 over 7 yearsTo 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 over 7 yearsSorry, in a rush, I forgot to include that only a substring of the label is to be italicized; I will amend the question.
-
alistaire over 7 yearsIf you use
parse = TRUE
you can use?plotmath
, though you'll have to reconfigure your labels. -
Bob over 7 yearsThank 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 about 6 years
element_text()
has an argument calledface
. I am not sure whygeom_text()
doesn't have this option. -
Megatron about 4 yearsFYI, to see possible rendering options:
demo(plotmath)
-
hd1 almost 4 yearsgeom_text() calls the option fontface -- cookbook-r.com/Graphs/Fonts
-
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."