【问题标题】:"\n" command not working to make 2 or 3-line long figure caption using textGrob“\n”命令无法使用 textGrob 制作 2 行或 3 行长图标题
【发布时间】:2016-03-15 01:21:38
【问题描述】:

我一直在尝试使用以下脚本在组合图下方制作 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)

但是当我尝试执行这个命令时,它仍然形成一个单行标题。

更新:

感谢@baptiste 先生的建议。现在我知道换行命令与 plotmath 不兼容。但是,当我尝试合并新脚本时,我的情节消失了,并且没有与标题结合。

现在我的查询是:

  • 如何将@baptiste 爵士建议的脚本合并到我的原始脚本中(如上所示),并将两个情节(pl 和 pl1)与左下角的标题(@baptiste 爵士提供)结合起来合并地块的一部分?

关于如何解决此问题的任何建议?非常感谢。

【问题讨论】:

    标签: r caption gtable grob


    【解决方案1】:

    不幸的是,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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-19
      • 2017-02-12
      • 2017-01-16
      • 2012-08-18
      • 1970-01-01
      • 2018-07-28
      • 2015-07-31
      • 1970-01-01
      相关资源
      最近更新 更多