首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >两个不同gtable的相同宽度

两个不同gtable的相同宽度
EN

Stack Overflow用户
提问于 2018-04-10 19:55:43
回答 1查看 361关注 0票数 0

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

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

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

我的代码示例:

代码语言:javascript
复制
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)      

谢谢你的帮助!

沃尔克

EN

回答 1

Stack Overflow用户

发布于 2018-04-11 19:52:48

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

代码语言:javascript
复制
# 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)此代码

代码语言:javascript
复制
tab2$widths <- unit(rep(1/ncol(tab2), ncol(tab2)), "null")

为每列设置相同的宽度。但看起来,它并不是所需的最小列宽。在我的示例中,它是最后一列所需的最小宽度。如何用所需的最小列宽来拟合列,使每列具有相同的宽度?

b)如何改变babtistes函数id_cell,

代码语言:javascript
复制
# 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)
}

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

代码语言:javascript
复制
# 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)

代码语言:javascript
复制
# Headline over all cells
idh <- id_cell(tab2, 1, colg,"core-fg")
tab2$layout[idh,"l"] <- tab2$layout[idh,"l"] - (colg-1)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49753005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档