r - tableGrob : set the height and width of a grid. 表

标签 r ggplot2 gridextra

我正在尝试制作一个功能,该功能将为我提供一个可用于 indesign、插画或inkscape 的绘图。在尝试这样做时,我有两个无法解决的问题。

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

2) 有时标题、注释和行名会“错位”(有关详细信息,请参阅图 1 和图 2 之间的区别)。当行名很短时会发生这种情况。

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

我还没有清理我的代码,所以请随意!

最佳答案

gridExtra::tableGrob 的当前版本不允许您设置宽度/高度。但是,您可以尝试不同的 (experimental)使用 gtable 从头构建的 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。

关于r - tableGrob : set the height and width of a grid. 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16471808/

相关文章:

当 Data1 中的变量名与 Data2 中 Column1 的值相同时,将 data1 中的变量名替换为 Data2 中 Column2 的值

在R中删除字符串中的重复单词

r - 在 ggplot 中使用预定义的调色板

r - 如何摆脱ggplot中轴文本上的度数符号?

r - 动态轴在绘图区域的顶部和底部滴答作响,循环时具有一致的间隔数和一致的宽度(ggplot2、grid.arrange)

r - 如何用值向量填充索引矩阵

r - 使用 NA 将时间序列转换为季度

r - Plotly 中的金字塔图

R - 在网格中排列时如何更改图像的高度

python - Python 中是否有 gridExtra/cowplot 类型包与 Plotnine 一起使用以对齐子图(即边际分布)