gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:52:46 28 4
gpt4 key购买 nike

是否可以将部分斜体文本标签传递到 ggplot 中?我尝试过使用 expressionitalic 命令 (expression(paste(italic("some text")))),但这些不能传递到数据帧中,因为命令的结果不是原子的。设置参数 fontface = "italic" 也不够,因为这会使整个标签变成斜体,而不仅仅是标签中选定的一组字符。例如,我希望一些必须斜体的拉丁短语在标签中以斜体显示(例如“in vivo point”中的“in vivo”)。

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 。您必须将字符串重写为绘图,但如果不是太多,也还不错。

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

关于r - 如何在 ggplot 的 geom_text_repel 或 geom_text 标签中包含斜体文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41528953/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com