gpt4 book ai didi

r - 图标题学名+textGrob gtable中的符号

转载 作者:行者123 更新时间:2023-12-04 10:53:29 25 4
gpt4 key购买 nike

首先,我要感谢 Baptiste 爵士通过使用 gtable/textGrob 在组合图的左下角添加标题来帮助我改进 R 脚本,如下所示:

library(grid)
library(gridExtra)
library(ggplot2)

p1 <- p2 <- ggplot()

g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)

g <- rbind(g1, g2)
caption <- textGrob("Figure 1. This is a caption", hjust=0, x=0)
g <- gtable::gtable_add_rows(g, unit(2,"mm") + grobHeight(caption), -1)
g <- gtable::gtable_add_grob(g, caption, nrow(g), l = 4, r = ncol(g))
grid.newpage()
grid.draw(g)

不过,我想补充两点:

(1) 在标题中插入学名,用斜体字书写。 - 例如,基于上面提到的标题,我只想将“is”一词斜体化,而其余的都是纯文本。

(2) 我还会在标题中添加符号,例如点形状=c(1,22);颜色=c(“黑色”,“红色”); fill=c("红", "黑").

我要怎么做这些?我是 R 程序的新手,因此非常感谢您的帮助。谢谢。

更新:

我已经在@Docconcoct、@user20650 和@baptiste 的帮助下使用这个脚本解决了查询 1:

library(grid)
library(gridExtra)
library(ggplot2)

g1 <- ggplotGrob(pl)
g2 <- ggplotGrob(pl1)

g <- rbind(g1, g2)
caption <- textGrob(expression(paste("Figure 1. This", italic(" is"), " a caption")), hjust=0, x=0)
g <- gtable::gtable_add_rows(g, unit(2,"mm") + grobHeight(caption), -1)
g <- gtable::gtable_add_grob(g, caption, nrow(g), l = 4, r = ncol(g))
grid.newpage()
grid.draw(g)

对于查询 2,正如 @baptiste 先生在我给他的原始电子邮件中所述,我已经有了关于组合地 block 的图例。然而,在图的标题中,我需要说明图例中的那些符号是什么意思,以及情节的其他一些细节。根据sir baptiste给出的例子,我需要在标题中包含supp的意思,以及OJ(黑圈)和VC(黑三角)的符号。

再次感谢!

最佳答案

根据评论,我建议采用以下策略:创建一个虚拟图,将您的图形标题(文本)作为图例标题,提取图例,并将其放在 gtable 的底部。

library(grid)
library(gridExtra)
library(ggplot2)
library(gtable)

p1 <- ggplot()
p2 <- ggplot(ToothGrowth, aes(len, dose, shape=supp)) + geom_point() +
theme(legend.position="bottom",
legend.background=element_rect(colour="black"))

title <- expression("Figure 1. This "*italic(is)*" now a legendary caption")
dummy <- ggplotGrob(p2 + guides(shape = guide_legend(title = title)))

g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
caption <- gtable_filter(dummy,"guide")[["grobs"]][[1]]
caption$widths <- grid:::unit.list(caption$widths)
caption$widths <- unit.c(unit(0,"mm"), caption$widths[2], unit(1,"null"))

g <- rbind(g1, g2)
g <- gtable::gtable_add_rows(g, unit(2,"mm") + grobHeight(caption), -1)
g <- gtable::gtable_add_grob(g, caption, nrow(g), l = 4, r = ncol(g))
grid.newpage()
grid.draw(legend)
grid.draw(g)

enter image description here

关于r - 图标题学名+textGrob gtable中的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35936319/

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