-1

I'm new to arranging grids. Below is my own try which is quite messy, firstly the grids don't seem to align on the y axis (I want the table right below the graph). And secondly, the table does not scale correctly to the graphs depending on the window size.

How can this be resolved? And where can I turn for more information on aligning the grids specifically?

Table data

> AAK.stats

                           NA
    Observations    1811.0000
    NAs                0.0000
    Minimum           -0.0612
    Quartile 1         0.0000
    Median             0.0000
    Arithmetic Mean    0.0008
    Geometric Mean     0.0007
    Quartile 3         0.0013
    Maximum            0.0696
    SE Mean            0.0003
    LCL Mean (0.95)    0.0003
    UCL Mean (0.95)    0.0013
    Variance           0.0001
    Stdev              0.0110
    Skewness           0.9051
    Kurtosis           5.9795

Code

> grid.arrange(chart(AAK.strategy), tableGrob(t(AAK.stats)))

Output

enter image description here

uncool
  • 2,613
  • 7
  • 26
  • 55

1 Answers1

3

There are several questions already on the topic of resizing a tableGrob. The question I always have to ask is this: how do you want it resized? By default it adjusts the row/column sizes so that all the text fits. That implies a fixed physical size for the table. It can be tuned manually, but one has to decide whether to

  • alter the font size
  • set the sizes arbitrarily, e.g. with "null" units, and hope the text will fit. With this strategy one still needs to decide whether each row/column should be given equal space, or relative spacing should be maintained.

Anyway, let's start with a reproducible example for the sake of reproducibility

library(gridExtra)
library(ggplot2)

p <- ggplot()

t <- tableGrob(iris[1,], rows = NULL)
grid.arrange(p,t)

enter image description here

In this instance I would resize the plot as follows,

library(gtable)
library(grid)

g <- ggplotGrob(p)
margin <- unit(0.5,"line")
# allocate a new row to fit the table
g <- gtable_add_rows(g, sum(t$heights) + margin)
# locate the plot panel column
panel <- g$layout[g$layout$name == "panel", "l"]
# add the table
g <- gtable_add_grob(g, t, nrow(g), panel)
# set the plot panel width to the table's width
g$widths[panel] <- list(sum(t$widths))
grid.newpage()
grid.draw(g)

enter image description here

Community
  • 1
  • 1
baptiste
  • 75,767
  • 19
  • 198
  • 294