我一直试图使用以下脚本在合并图下方制作一个2到3行的数字标题:
library(grid)
library(gridExtra)
library(ggplot2)
g1 <- ggplotGrob(pl) #pl is my plot 1
g2 <- ggplotGrob(pl1) #pl1 is my plot 2
g <- rbind(g1, g2) #this combines my two plots
caption <- textGrob(expression(paste(bold("Figure 1"), ". This is the first line with a", italic( " scientific name."),"\nThis should be the second line.","\nThis is the third line.")), hjust=0, x=0) #caption to be incorporated at the lower left of the combined plots
g <- gtable::gtable_add_rows(g, unit(2,"mm") + grobHeight(caption), -1) #incorporating the combined plots with the caption
g <- gtable::gtable_add_grob(g, caption, nrow(g), l = 4, r = ncol(g)) #incorporating the combined plots with the caption
grid.newpage()
grid.draw(g)
但是当我尝试执行此命令时,它仍然形成一个单行标题。
更新:
谢谢Sir @baptiste的建议。现在我知道newline命令与plotmath不兼容。但是,当我尝试合并新脚本时,我的情节消失了,并没有与标题结合。
现在我的查询是:
有关如何解决此问题的任何建议?非常感谢你。
答案 0 :(得分:2)
plotmath与换行符不兼容。我建议填写一个表格,文本分成不同的行,
library(gridExtra)
library(grid)
table_label <- function(label, params=list()) {
params <- modifyList(list(hjust=0, x=0), params)
mytheme <- ttheme_minimal(core = list(fg_params = params), parse=TRUE)
disect <- strsplit(label, "\\n")[[1]]
m <- as.matrix(disect)
tableGrob(m, theme=mytheme)
}
txt <- 'bold("Figure 1")\nThis is another line.\n alpha~beta*" are greek letters"'
grid.newpage()
g <- table_label(txt)
grid.draw(g)