本文介绍了tableGrob:设置grid.table的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图制作一个功能,可以为我提供一个适合于indesign,illustrator或inkscape的情节。在试图这样做时,我遇到了两个我无法解决的问题。 设置我的绘图的宽度和高度(或者只是grobTable):我得到的输出非常小,并且在illustrator中将其升级字体随之而来,并成为大的方式。因此我想用手动定义的宽度和高度进行绘图。 2)有时标题,音符和流行音乐会被放错位置(详情请参见图1和2之间的区别)。当rownames短暂时会发生。 库(gridExtra)库(ggplot2) 数据(钻石) ##绘图函数 kryds.row< - 函数(x,y,p = 100,decor =%,digits = 3, titel = ,note =,red = 219,green = 55,blue = 153){ c< - table(x,y)s< - as.character (c))s j r k j j < - round(j,digits)j [] farve 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,显示.rownames = TRUE, show.rsep = TRUE, show.hlines = TRUE, show.csep = FALSE, show.vlines = FALSE,显示.box = FALSE, padding.h =单位(15,mm), padding.v =单位(8,mm), core.just =center , row.just =left, separator = farve) hh< - grobHeight(table) ww< - grobWidth(table) border default.units =npc,r = unit(0.1,snpc)) , just =center, 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 = 中心, name = NULL,gp = gpar(fill = NA,col = farve,vp = NULL)) title< - textGrob(titel,x =单位(0.5,npc)-0.5 * ww +单位(5,mm),y =单位(0.5,npc)+ 0.5 * hh +单位b $ b vjust = 0,hjust = 0,gp = gpar(fontsize = 12,fontface =bold)) 脚注< - textGrob(注:x =单位(0.5,npc) - 0.5 * ww +单位(5,mm),y =单位(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 - 单位(2,mm),y =单位(0.5,npc)+ 0.5 * hh +单位(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) } #绘制 kryds.row(钻石$ color,diamonds $ cut ,titel =title,note =note)#plot 1 kryds.row(diamonds $ cut,diamonds $ color,titel =title,note =note)#plot 2 #问题#1:标题,注释和row.text中的j在plot 1中非常糟糕,但不是plot 2 #2我无法设置我的ta的宽度和高度ble 我还没有清理过我的代码,所以请随身携带!解决方案我用的解决方案是操纵像这样的级别长度: 最长数字 $ b if(最长< nchar(levels(x))[i]){最长< -nchar(levels(x))[i] 最长号码< -i } } $ (i为1 :( 100-最长)){等级(x)[最长编号]< - 粘贴(等级(x)[最长编号],,sep =b $ b )} 这样我就可以控制表格的宽度 I'm trying to make a function that will give me a plot ready for indesign, illustrator or inkscape. In trying to do so, I have 2 problems I cannot solve.1) set the width and height of my plot (or just the grobTable):The output I get is very small and when upscaling it in illustrator the font follows and become way to big. Therefore I want to make plots with manually defined widths and heights.2) Sometimes the title, note and rownames gets "misplaced" (see the difference between plot 1 and 2 for details). It happens when the rownames are short. library(gridExtra)library(ggplot2)data(diamonds)## plot functionkryds.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 itkryds.row(diamonds$color, diamonds$cut, titel="title", note="note") # plot 1kryds.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 tableI have not cleaned up in my code yet, so please bare with it! 解决方案 The solution I went with was to manipulate the length of the levels like this: 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="") }That way I can control the width of the table 这篇关于tableGrob:设置grid.table的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-01 22:14