如何在 ggplot 的 geom_text_repel 或 geom_text 标签中包含斜体文本? [英] How do I include italic text in geom_text_repel or geom_text labels for ggplot?

查看:66
本文介绍了如何在 ggplot 的 geom_text_repel 或 geom_text 标签中包含斜体文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将部分斜体的文本标签传递到 ggplot 中?我曾尝试使用 expressionitalic 命令(expression(paste(italic("some text")))),但这些不能传递到数据帧中,因为命令的结果不是原子的.设置参数 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))

推荐答案

您可以使用 parse = TRUE?plotmath 表达式(作为字符串)传递给 geom_textgeom_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.

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)

这篇关于如何在 ggplot 的 geom_text_repel 或 geom_text 标签中包含斜体文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆