jc52766
jc52766

Reputation: 61

Report writing plots plus table on 1 page

I am hoping to use R to produce a 1 page report that is to be laid out as follows:

topleft:
plot 1 plot 2
plot 3 plot 4

Topright: plot 5 (4x as big as plots 1:4)

Bottomright: Table

This page will then be rerun ~100 times so that each page of the report is identical but each page is just for a different store in this case.

I was hoping to do this using ggplot for the 5 plots. I can produce all 6 items separately but I really have no clue how to go about setting them neatly onto 1 page in the layout above.

I have read a little bit on LaTex but not sure if I'm just wasting time. I like the base layout() and par(mfrow) capabilities but I don't know what to do here seeing as I need similar functionality for ggplot2 plots.

So the topleft 4 plots can be done using something like:

library(gridExtra)
grid.arrange(plot1,plot2,plot3,plot4,ncol=2,nrow=2)

What I really need is something like

grid.arrange(plot1,plot2,(no plot),(no plot),plot3,plot4,(no plot),(no plot), 
                                                                 ncol=4,nrow=3)

Something like this would prepare the plot region for plot 5 in the top right and leave space down the bottom for the table.

So my questions are

  1. is how would I get plots 1:4 (same size) and plot 5 (4x as big as plots 1:4) all onto the page as planned using ggplot2 plots?
  2. How would I then get a table on the bottom right?

I like the look of this table: grid.table function from gridExtra package.

The table I need will be 6 rows * 5 columns. Something like:

grid.table(head(iris))

The only issue is that it always gets thrown right into the middle and I don't know how to control it so that it sits bottom right.

Any suggestions?

Upvotes: 0

Views: 390

Answers (1)

Masato Nakazawa
Masato Nakazawa

Reputation: 1000

I think you can create what you want using nested arrangeGrob's. Let me illustrate:

library(ggplot2)
library(gridExtra)

# create a dummy ggplot and its gtable
g <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
gt <- ggplot_gtable(ggplot_build(g))
gt$layout$clip[gt$layout$name=="panel"] <- "off"

# create a table grob
my_table <- tableGrob(head(mtcars))

# arrange the 5 plots and the table
grid.arrange(arrangeGrob(
             arrangeGrob(gt, gt, gt, gt, ncol=2), ## 4 top left panels
                         gt, ncol=2),             ## 1 top right panel
                         my_table, nrow=2)  ## bottom table

enter image description here

Upvotes: 3

Related Questions