【问题标题】:Same Width of two different gtables两个不同gtable的相同宽度
【发布时间】:2018-04-10 11:55:43
【问题描述】:

(我想要一个带有一些数据、标题和脚注的 gtable 对象。 这三个元素中的每一个都是 gtables,它们首先使用 gtable_add_rows 组合成一个 gtable-object,然后使用 gtable_add_grob 将三个 gtables 排列成一个 grob。不幸的是,标题/脚注太长(分别太短)

如何设置标题/脚线的宽度?

如何将标题/脚注的文本分配给左对齐?

我的代码示例:

require(gtable)
require(grid)
require(gridExtra)

tbl<-matrix(paste(letters[1:6]),nrow=2)
colnames(tbl)<-c(paste0("col",1:3))
rownames(tbl)<-c(paste0("row",1:2))
tbl

tt1 <- ttheme_default(base_size = 10,rowhead=list(fg_params=list(fontface=2,hjust=0, x=0)))
tt2 <- ttheme_default(base_size = 15,fg_params=list(fontface="bold",hjust=1,x=0.9))
tt3 <- ttheme_default(base_size =7, fg_paras=list(fontface="italic",hjust=0,x=0.9))

gtbl <- tableGrob(tbl, theme=tt1)
htxt <- tableGrob("Headline is too long and this is stupid", theme=tt2)
ftxt <- tableGrob("Footnote", theme=tt3)

padding <- unit(1,"line")
table <- gtable_add_rows(gtbl,
                     heights = grobHeight(htxt) + padding,
                     pos = 0)

table <- gtable_add_rows(table, 
                     heights = grobHeight(ftxt)+ padding)
table <- gtable_add_grob(table, list(htxt, ftxt),
                     t=c(1, nrow(table)), l=c(1,1), 
                     r=ncol(table))
dim(table)
grid.newpage()
grid.draw(table)      

感谢您的帮助!

沃尔克

【问题讨论】:

    标签: r grid fixed-width gtable


    【解决方案1】:

    我解决了我的问题。 只剩下两个小问题。请参阅下面我的注释代码。

    # load packages ------------------------------------------------------------
    
    require(gtable)
    require(grid)
    require(gridExtra)
    
    
    # id makro ----------------------------------------------------------------
    # see here: https://stackoverflow.com/questions/43613320/how-to-add-multi-sub-columns-in-gridextratablegrob/43620247#43620247
    # Tanks to: baptiste
    id_cell <- function(table, row, col, name="colhead-fg"){
      l <- table$layout
      which(l$t %in% row & l$l %in% col & l$name==name)
    }
    
    # code to test it----------------------------------------------------------------
    
    
    # Some different table themes
    tt1 <- ttheme_default(base_size = 10,rowhead=list(fg_params=list(fontface=2,hjust=0, x=0))) # Data
    tt2 <- ttheme_default(base_size = 15,fg_params=list(fontface="bold",hjust=1,x=0.9)) # Header
    tt3 <- ttheme_default(base_size = 7,fg_params=list(fontface="italic",hjust=0,x=0.9)) # Footnote
    
    # Convert to gtable
    # 1. Data
    # The data 
    tbl<-matrix(paste(letters[1:6]),nrow=2)
    colnames(tbl)<-c("col1","column2","verywidecolum3")
    rownames(tbl)<-c(paste0("row",1:2))
    gtbl <- tableGrob(tbl, theme=tt1)
    
    # 2. Headline
    # Note: Define the Headline as gtable with as many columns a your Data
    # Headline is defined in last colum due to the Makro id_cells. 
    hl<-"Headline"
    hlm<-matrix(c(rep("",(dim(gtbl)[2]-1)),hl),nrow=1)
    hlm
    htxt <- tableGrob(hlm, theme=tt2)
    
    # 3. Footnote
    # Note: Define the Footnote as gtable with as many columns a your Data
    # The text of your footnote is defined in last colum due to the Makro id_cells. 
    ftm<-matrix(c(rep("",(dim(gtbl)[2]-1)),"Footnote"),nrow=1)
    ftxt <- tableGrob(ftm, theme=tt3)
    
    # Define your table with Headline, Data and Footnote in one gtable object
    tab2<-combine(htxt,gtbl,ftxt,along=2)
    
    # Draw Grid - not formated
    grid.newpage()
    grid.draw(tab2)     
    
    # Formating of  Grid Table
    
    # Adjust columnwidths: same widths for each column
    tab2$widths <- unit(rep(1/ncol(tab2), ncol(tab2)), "null")
    
    # 
    rowg<-dim(tab2)[1] # Number of rows of your gtable
    colg<-dim(tab2)[2] # Number of columns of your gtable
    eleg<-2*rowg*colg # Number of grobs of your gtable
    # Note: Grobs in gtables are arranged in rows. Within each row first grob class "text" is repeated for the columns of gtable, then grob class "rect"
    
    
    # Identify Grob elements which should be formated
    # This is Headline and footnote, 
    forg<-c(1:colg,(eleg-2*colg+1):(eleg-colg));forg
    for (i in forg){
      if ((class(tab2$grobs[[i]])=="text")[1] == TRUE) {
        print(i)
        tab2$grobs[[i]]$x<-unit(0,"npc")
        tab2$grobs[[i]]$hjust<-0
      }
    }
    
    # Headline over all cells
    idh <- id_cell(tab2, 1, colg,"core-fg")
    tab2$layout[idh,"l"] <- tab2$layout[idh,"l"] - (colg-1)
    
    # Footnote over all cells
    idf <- id_cell(tab2, rowg, colg,"core-fg")
    tab2$layout[idf,"l"] <- tab2$layout[idf,"l"] - (colg-1)
    
    # Draw grid
    grid.newpage()
    grid.draw(tab2)     
    

    我的两个小问题:

    a) 这段代码

    tab2$widths <- unit(rep(1/ncol(tab2), ncol(tab2)), "null")
    

    为每列设置相同的宽度。但它似乎不是所需的最小列宽。在我的示例中,它是最后一列所需的最小宽度。如何将列与所需的最小列宽匹配,以使每列具有相同的宽度?

    b) 如何更改 babtistes 函数 id_cell,

    # id makro ----------------------------------------------------------------
    # see here: https://stackoverflow.com/questions/43613320/how-to-add-multi-sub-columns-in-gridextratablegrob/43620247#43620247
    # Tanks to: baptiste
    id_cell <- function(table, row, col, name="colhead-fg"){
      l <- table$layout
      which(l$t %in% row & l$l %in% col & l$name==name)
    }
    

    是否可以先指定页眉/脚注,然后指定必要的空单元格? 这与此代码部分有关:

    # 2. Headline
    # Note: Define the Headline as gtable with as many columns a your Data
    # Headline is defined in last colum due to the Makro id_cells. 
    hl<-"Headline"
    hlm<-matrix(c(rep("",(dim(gtbl)[2]-1)),hl),nrow=1)
    htxt <- tableGrob(hlm, theme=tt2)
    

    # Headline over all cells
    idh <- id_cell(tab2, 1, colg,"core-fg")
    tab2$layout[idh,"l"] <- tab2$layout[idh,"l"] - (colg-1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-12
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多