0

I am trying to output a graph and table for each group to one page of a PDF (one page for each group). I am nearly there but I am just having trouble with the final step. I have both a table and a plot on the same page but the table is on top of the plot. When I move it outside of the plot window it disappears.

Is there a way I can put the graph underneath the plot (which is outside of the plot window)?

Here is my code:

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()
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
user3466328
  • 398
  • 1
  • 11

1 Answers1

0

since you want the tableGrob to be outside the plot panel and not inside, you shouldn't use annotation_custom, but arrangeGrob to arrange a plot and a table on a page. The list of grobs can then be printed page by page in the pdf device.

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

pdf("multipage.pdf")
plots
dev.off()
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Hi baptiste, thanks for responding, I saw some of your code on other threads like here: http://stackoverflow.com/questions/9690648/point-clipped-on-x-axis-in-ggplot. But I don't know where to put the grid.arrange(...). The problem is after populating the plots/tables with the output of dlply, there is nowhere to include grid.arrange(...) – user3466328 Dec 05 '14 at 21:58
  • Consider adding more explanation as to how this works to solve OP's question. As it is, this would be considered a low-quality answer. – Heretic Monkey Dec 05 '14 at 22:50