我試圖做一個函數,它會給我一個準備好插圖,illustrator或inkscape的陰謀。在試圖這樣做時,我遇到了兩個我無法解決的問題。tableGrob:設置grid.table的高度和寬度
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
我還沒有清理我的代碼,所以請裸機!
1.行標籤默認的調整是左,你可以改變它爲中心。 2.有沒有選項來設置表的大小,不幸的是,它的計算以適應內容,因此將改變取決於字體大小。 – baptiste 2013-05-10 12:56:04