tableGrob:设置grid.table的高度和宽度

时间:2013-05-09 21:56:23

标签: r ggplot2 gridextra

我正在尝试制作一个功能,它可以为我提供一个可以用于indesign,illustrator或inkscape的情节。在尝试这样做时,我遇到了两个我无法解决的问题。

1)设置我的绘图的宽度和高度(或只是grobTable): 我得到的输出非常小,当在illustrator中升级它时,字体跟随并变为大的方式。因此,我想制作手动定义的宽度和高度的图。

2)有时标题,注释和rownames会“放错位置”(有关详细信息,请参阅第1和第2小节之间的区别)。当rownames很短时就会发生这种情况。

library(gridExtra)
library(ggplot2)

data(diamonds)

## plot function

kryds.row <- function(x,y, p=100, decor="%", digits=3,
                  titel="", note="", red=219, green=55, blue= 153){

c <- table(x, y)
s <- as.character(sum(c))
s <- paste("Antal svarpersoner=", s, sep=" ")
j <- prop.table(c,1)
r <- c(rownames(j),"Total")
k <- c(colnames(j), "Total")
j <- addmargins(j, margin =2, FUN = sum)
j <- round(j, digits)
j[]<-paste(j*p, decor, sep=" ")

farve <- rgb(red,green,blue, maxColorValue =255)

table     <-   tableGrob(j,
                       cols = k,
                       gpar.coretext = gpar(fontsize = 12),            
                       gpar.coltext  = gpar(fontsize = 12,col="white"),            
                       gpar.rowtext  = gpar(fontsize = 12, fontface="bold"),            
                       gpar.corefill = gpar(fill = rgb(255,255,255, maxColorValue      =255), alpha = 1, col = NA),
                       gpar.rowfill  = gpar(fill = rgb(255,255,255, maxColorValue =255), alpha = 1, col = NA),           
                       gpar.colfill  = gpar(fill = 0, alpha = 1 ,col= "white"),                       
                       equal.width   = TRUE,            
                       show.rownames = TRUE,            
                       show.rsep     = TRUE, 
                       show.hlines   = TRUE,                               
                       show.csep     = FALSE, 
                       show.vlines   = FALSE,
                       show.box      = FALSE,
                       padding.h     = unit(15, "mm"),            
                       padding.v     = unit(8, "mm"),
                       core.just     = "center", 
                       row.just      = "left",
                       separator     = farve)


 hh <- grobHeight(table)
 ww <- grobWidth(table)

 border <- roundrectGrob(x=0.5, y=0.5, width=ww, height=hh,
                      default.units="npc",
                      r=unit(0.1, "snpc"),
                      just="centre",
                      name=NULL, gp=gpar(col="white", fill=farve, vp=NULL)) 

 border2 <- roundrectGrob(x=0.5, y=0.5, width=ww, height=hh,
                       default.units="npc",
                       r=unit(0.1, "snpc"),
                       just="centre",
                       name=NULL, gp=gpar(fill=NA, col=farve, vp=NULL))

 title <- textGrob(titel,
                x=unit(0.5,"npc") -0.5*ww + unit(5, "mm"), 
                y=unit(0.5,"npc") +0.5*hh + unit(2, "mm"), 
                vjust=0,hjust=0, gp=gpar(fontsize=12, fontface="bold"))

 footnote <- textGrob(note, 
                   x=unit(0.5,"npc") - 0.5*ww + unit(5,"mm"),
                   y=unit(0.5,"npc") - 0.5*hh, 
                   vjust=1, hjust=0,gp=gpar( fontsize=10))

 svarpersoner       <- textGrob(s, 
                             x=unit(0.5,"npc") + 0.5*ww -unit(5, "mm"),
                             y=unit(0.5,"npc") + 0.5*hh + unit(2, "mm"), 
                             vjust=0, hjust=1,gp=gpar( fontsize=10))
 grid.newpage()
 gt <- gTree(children=gList(border,table,border2, title, footnote, svarpersoner))
 grid.draw(gt)

}



# Plot it 
kryds.row(diamonds$color, diamonds$cut, titel="title", note="note") # plot 1
kryds.row(diamonds$cut, diamonds$color, titel="title", note="note") # plot 2


# Problems
#1: The title, note and the j in the row.text is very badly placed in plot 1 but not   plot 2
#2 I cannot set the width and height of my table

我还没有在我的代码中清理过,所以请光着它!

2 个答案:

答案 0 :(得分:5)

当前版本的gridExtra :: tableGrob不允许您设置宽度/高度。但是,您可以尝试使用gtable从头开始构建另一个(experimental)版本的tableGrob。

#library(devtools)
#install_github("tablegrob", "baptiste")
require(tablegrob)

d <- iris[sample(seq.int(nrow(iris)), 6),]

grid.newpage()
pushViewport(viewport(height=0.8,width=0.9))
g2 <- tableGrob(d, rows=NULL,
                widths=unit(1,"null"), heights=unit(1/(nrow(d)),"npc"))
grid.draw(g2)
grid.roundrect(y=unit(0,"line"), height=unit(1,"npc") +unit(1,"lines"),
               just="bottom", r=unit(0.05, "snpc"))

编辑(08/2015):您现在可以编辑宽度/高度,因为grid.table现在基于gtable。

答案 1 :(得分:1)

我采用的解决方案是操纵这样的水平长度:

 longest <- 0
 longestnumber <- 0

 for (i in 1:length(levels(x))){

  if (longest < nchar(levels(x))[i]){
  longest <- nchar(levels(x))[i] 
  longestnumber <- i
  } 
   }

 for (i in 1:(100-longest)){
  levels(x)[longestnumber] <- paste(levels(x)[longestnumber], " ", sep="")
  }

这样我可以控制表格的宽度