0

我正在尝试将每个组的图形和表格输出到 PDF 的一页(每个组一页)。我快到了,但我只是在最后一步遇到了麻烦。我在同一页面上有一个表格和一个情节,但表格在情节的顶部。当我将它移到绘图窗口之外时,它会消失。

有没有办法可以将图形放在绘图下方(位于绘图窗口之外)?

这是我的代码:

lss <- read.csv(file="WithBlanksFilled_4.csv",head=TRUE, sep=",") # read in csv and name columns
st<-lss$server_type
tenant <- lss$tenant_n
date<- lss$date_time
ss <- lss$Max_load_source_size

df1 <- data.frame(st, tenant, date, ss)


#Create graph which will then be populated by output of dlply
library(ggplot2)
library(scales)
library(grid)
library(gridExtra)
library(gtable)
p<-ggplot(df1, aes(x=as.Date(date, "%d/%m/%Y %H:%M"), y=ss))+geom_point()+labs(x="Date", y="Load Source Size")+facet_wrap(~st+tenant, ncol=1)+scale_x_date(breaks = date_breaks("week"), minor_breaks=date_breaks("1 day"), labels = date_format("%d-%b"), limits = c(as.Date("2014-09-06"), as.Date("2014-12-01"))) + theme(axis.text.x = element_text(size=5, color="grey"), plot.margin=unit(c(1,1,5,1), "cm"), panel.border=element_blank()) +ylim(0,NA) 
#p1 <- ggplot_gtable(ggplot_build(p))
#p1$layout$clip[p1$layout$name=="panel"] <- "off"

#Grouping by t&st
library(plyr)
plots<-dlply(df1, .(st, tenant), function(df1) p %+% df1 %+% annotation_custom(tableGrob(df1), ymin=-2, ymax=-2))

#outputting graphs  table to PDF.
pdf(file = file.path("<filepath>.pdf"), onefile=TRUE)

plots 

dev.off()
4

1 回答 1

0

由于您希望 tableGrob 位于绘图面板之外而不是内部,因此您不应该使用 annotation_custom,而是使用arrangeGrob 在页面上安排绘图和表格。然后可以在 pdf 设备中逐页打印 grobs 列表。

library(plyr)
plots <- dlply(iris, "Species", function(d) {
               arrangeGrob(qplot(1,1), tableGrob(head(d)))
               })

pdf("multipage.pdf")
plots
dev.off()
于 2014-12-05T21:46:02.277 回答