如何在gomplot的geom_text_repel或geom_text标签中包含斜体文本?

Bob*_*Bob 7 text r ggplot2 plotmath ggrepel

是否可以将部分斜体文本标签传递给ggplot?我尝试使用expressionitaliccommands(expression(paste(italic("some text")))),但这些不能传递到数据框,因为命令的结果不是原子的.设置参数fontface = "italic"也不够,因为这会使整个标签斜体化,而不仅仅是标签中的一组选定字符.例如,我想在标签中使用一些斜体字的拉丁短语(例如"体内点"中的"体内").

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))
Run Code Online (Sandbox Code Playgroud)

ali*_*ire 5

您可以parse = TRUE用来将?plotmath表达式(作为字符串)传递给geom_textgeom_text_repel。您必须将字符串重写为plotmath,但是如果不是太多,也还不错。

注意:的CRAN版本ggrepel有一个会中断的错误parse = TRUE,但已在GitHub版本上修复。ggplot2::geom_text工作正常。

# 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)
Run Code Online (Sandbox Code Playgroud)

带有部分斜体标签的图

  • @hd1 来自问题:“设置参数`fontface =“italic”`也不够,因为这会使整个标签变成斜体,而不仅仅是标签中的一组选定字符。” (3认同)
  • `element_text()` 有一个名为 `face` 的参数。我不知道为什么 `geom_text()` 没有这个选项。 (2认同)
  • 哦,没关系。它叫做`fontface`。 (2认同)
  • geom_text() 调用选项 fontface - http://www.cookbook-r.com/Graphs/Fonts/ (2认同)