如何在geom_text_repel或gomplot的geom_text标签中包含斜体文本? [英] How do I include italic text in geom_text_repel or geom_text labels for ggplot?
问题描述
是否可以将部分斜体文本标签传递给ggplot?我试过使用表达式
和斜体
命令(表达式(paste(italic(some文本)))
),但这些不能传递到数据框中,因为这些命令的结果不是原子的。设置参数 fontface =italic
也不够,因为这会斜体化整个标签,而不仅仅是标签中的一组选定字符。例如,我希望一些必须用斜体表示的拉丁语短语在标签中加以斜体(例如in vivo point中的in vivo)。
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))
推荐答案
code> parse = TRUE 将?plotmath
表达式(作为字符串)传递给 geom_text
或 geom_text_repel
。您必须将这些字符串重写为plotmath,但如果它不是太多,那就不算太坏。
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.
注意: CRAN版本的 ggrepel
有一个缺陷 parse = TRUE
,但它已在GitHub版本中修复。 ggplot2 :: geom_text
正常工作。
$ b
Note: The CRAN version of ggrepel
has a bug that breaks parse = TRUE
, but it has been fixed on the GitHub version. ggplot2::geom_text
works fine.
# devtools::install_github('slowkow/ggrepel')
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)
这篇关于如何在geom_text_repel或gomplot的geom_text标签中包含斜体文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!