在对齐网格图形对象时遇到了一些麻烦——我已经阅读了我能找到的所有文档,包括 Murrell 的书,但没有成功。我认为我正在尝试做的事情非常简单,所以希望我缺少简单的东西。
这是一个可重现的示例,它将按目的地在 Hadley 的包裹中制作所有航空承运人的 PDF hflights
(反映我在不同数据集上尝试做的事情)。
require(hflights)
require(gridExtra)
require(Cairo)
make_table <- function(df) {
p <- tableGrob(
df
,padding.h=unit(.25, "mm")
,show.rownames=FALSE
,gpar.coretext = gpar(fontsize=8, lineheight=0)
#this doesn't seem to justify the table
,just = c("bottom")
,show.box = T
)
return(p)
}
dests <- unique(hflights$Dest)
#list to hold the plots
plot_list <- list()
#loop over destinations and make a simple report
for (i in dests) {
#just this destination
this_dest <- hflights[hflights$Dest == i, ]
#the title
title <- textGrob(label = i, gp = gpar(fontsize=72, fontface = 'bold'))
#a table of carriers
carriers <- unique(this_dest$UniqueCarrier)
carriers <- data.frame(
carrier=carriers
)
carrier_table <- make_table(carriers)
#put them together
p <- arrangeGrob(
title, carrier_table
,nrow=2
)
plot_list[[i]] <- p
}
#print the report
Cairo(
width = 11, height = 8.5
,file = paste('destinations.pdf', sep = ''), type="pdf"
,units = "in"
)
print(plot_list)
dev.off()
tableGrob
我希望(在函数中)生成的整个表make_table
都可以在 grob 的顶部进行调整。现在它在 grob 内垂直和水平居中。我需要在调用 to 时这样做,tableGrob
还是在调用中这样做arrangeGrob
?换一种方式问,如果上述内容不清楚,我怎样才能使整个表格(而不是其中的文本)与其容器的顶部/底部/左侧/右侧对齐?
谢谢!