r - tableGrob: set the height and width of a grid.table -
i'm trying make function give me plot ready indesign, illustrator or inkscape. in trying so, have 2 problems cannot solve.
1) set width , height of plot (or grobtable): output small , when upscaling in illustrator font follows , become way big. therefore want make plots manually defined widths , heights.
2) title, note , rownames gets "misplaced" (see difference between plot 1 , 2 details). happens when rownames short.
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 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: title, note , j in row.text badly placed in plot 1 not plot 2 #2 cannot set width , height of table
i have not cleaned in code yet, please bare it!
the current version of gridextra::tablegrob doesn't let set widths/heights. can try different (experimental) version of tablegrob built scratch using gtable.
#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"))
edit (08/2015): can edit widths/heights, since grid.table based on gtable.
Comments
Post a Comment