グリッド グラフィックス オブジェクトの配置に問題があります。Murrell の本を含め、見つけられるすべてのドキュメントを読みましたが、成功しませんでした。私がやろうとしていることは非常に簡単だと思うので、うまくいけば単純なものが欠けています。
これは、ハドリーのパッケージの目的地ごとにすべての航空会社の 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
を、グロブの先頭に正当化する必要があります。現在、グロブ内の垂直方向および水平方向の中央に配置されています。への呼び出しでそれを行う必要がありますかtableGrob
、それともarrangeGrob
呼び出しの中でですか? 別の言い方をすれば、上記が明確でない場合、テーブル全体(テーブル内のテキストではなく) をコンテナの上/下/左/右に揃えるにはどうすればよいですか?
ありがとう!