2014-12-05 59 views
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() 

回答

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() 
+0

巴蒂斯特您好,感謝響應,我看到一些其他線程上喜歡這裏你的代碼:http://stackoverflow.com/questions/9690648/point-clipped-on-x-axis-in-ggplot 。但我不知道把grid.arrange(...)放在哪裏。問題是在用dlply的輸出填充繪圖/表格後,無處可包含grid.arrange(...) – user3466328 2014-12-05 21:58:04

+0

非常棒!這工作完美!我真的很感謝你的幫助:) – user3466328 2014-12-05 22:49:51

+0

考慮添加更多的解釋,說明如何解決OP的問題。事實上,這將被視爲低質量的答案。 – 2014-12-05 22:50:45