2016-03-15 90 views
0
進行2或3線長圖題

我一直在嘗試使用下面的腳本進行合併曲線下方的2至3線圖題:「 n」命令無法正常工作使用textGrob

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爵士提供)在組合地塊的左下部分?

有關如何解決此問題的任何建議?非常感謝你。

回答

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) 

enter image description here