How do I fit a very wide grid.table or tableGrob to fit on a pdf page?
I got this done using font sizes. Not the best solution (requires manual intervention) but maybe someone can contribute something more elegant.
termTable = tableGrob(terms, h.even.alpha=1, h.odd.alpha=1, v.even.alpha=0.5, v.odd.alpha=1, core.just='left', rows=c(),
gpar.coretext =gpar(fontsize=8),
gpar.coltext=gpar(fontsize=10, fontface='bold'),
gpar.rowtext=gpar(fontsize=10, fontface='bold')
)
How to adjust the Font Size of tableGrob
You can do this via themes:
mytheme <- gridExtra::ttheme_default(
core = list(fg_params=list(cex = 2.0)),
colhead = list(fg_params=list(cex = 1.0)),
rowhead = list(fg_params=list(cex = 1.0)))
myt <- gridExtra::tableGrob(mtcars[1:5, 1:5], theme = mytheme)
grid.draw(myt)
There are a number of other examples in browseVignettes("gridExtra")
-- look at the tableGrob
examples. A great deal of control is possible.
How to write huge rows of data using r tableGrob
it works just fine if you have enough time
library(grid)
library(gridExtra)
pdf("y.pdf", height=22, width=0.05)
grid.table(sample(letters, size = 1001, TRUE), theme=ttheme_minimal(2, padding = unit(c(0,0),"mm")))
dev.off()
enter image description here
Saving dataframe to pdf adjust width
I figured out I could add the scale
parameter to ggsave
. I wrote a simple function to get the optimal scale:
optimal.scale <- function(w,h, wanted.w, wanted.h) max(c(w/wanted.w, h/wanted.h))
I added 0.1 to the scale to add a margin to the plot such that the text is not directly on the edge of the paper. Then I passed the resulting scale to ggsave
tg = gridExtra::tableGrob(table
h = grid::convertHeight(sum(tg$heights), "mm", TRUE)
w = grid::convertWidth(sum(tg$widths), "mm", TRUE)
scale = optimal.scale(w,h, 279, 210) + 0.1 #A4 = 279 x 210 in landscape
ggplot2::ggsave("test.pdf", tg, width = 279, height = 210, units = 'mm' , scale = scale)
Now my table fits on the A4:
Forcing tableGrob() to fit in .tiff file
Assuming that save_plot
comes from the cowplot package (there is more than one package with a function called save_plot
), you can specify the height and aspect ratio like this:
save_plot("table isnt cut off.tiff", g, dpi = 300, base_height = 5.5, base_asp = 0.4)
Data.frame header on every PDF page (R - grid & gridExtra)
The trick is to create a new data frame on each page from subsets of the original. The difficulties arise in adjusting the row height to accommodate the new header row, and in preserving numbering of rows across pages.
Anyway, here is the updated function to accomplish that:
library(magrittr)
library(gridExtra)
library(grid)
set_multipage_pdf <- function(df, paper_size = 'a4', margin = 1.30, landscape = FALSE,
page_cex = 1, table_cex = 1)
{
sizes <- list(a4 = c(larger_size = 29.70, smaller_size = 21.00),
a3 = c(larger_size = 42.00, smaller_size = 29.70),
letter = c(larger_size = 27.94, smaller_size = 21.59),
executive = c(larger_size = 18.41, smaller_size = 26.67))
if (landscape) {
paper_height <- sizes[[paper_size]]['smaller_size'] * page_cex
paper_width <- sizes[[paper_size]]['larger_size'] * page_cex
} else {
paper_height <- sizes[[paper_size]]['larger_size'] * page_cex
paper_width <- sizes[[paper_size]]['smaller_size'] * page_cex
}
tg <- df %>% tableGrob(rows = seq_len(nrow(df)),
theme = ttheme_default(base_size = 5 * table_cex))
fullheight <- convertHeight(sum(tg$heights), "cm", valueOnly = TRUE)
page_margin <- unit(margin, "cm")
margin_cm <- convertHeight(page_margin, "cm", valueOnly = TRUE)
freeheight <- paper_height - margin_cm
npages <- ceiling(fullheight / freeheight)
nrows <- nrow(tg)
heights <- convertHeight(tg$heights, "cm", valueOnly = TRUE) * (nrows + npages)/nrows
rows <- cut(cumsum(heights), include.lowest = FALSE,
breaks = c(0, cumsum(rep(freeheight, npages))))
groups <- split(seq_len(nrows), rows)
gl <- lapply(groups, function(id)
{
df[id,] %>%
tableGrob(rows = id, theme = ttheme_default(base_size = 5 * table_cex))
})
for(page in seq_len(npages)){
if(page > 1) grid.newpage()
grid.draw(gl[[page]])
}
}
Related Topics
Ggplot2, Geom_Bar, Dodge, Order of Bars
Shiny Renderui Selectinput Returned Null
Read Observations in Fixed Width Files Spanning Multiple Lines in R
Calculate Percentage of Each Category in Each Group in R
How to Scrape/Automatically Download PDF Files from a Document Search Web Interface in R
How to Filter Data Without Losing Na Rows Using Dplyr
If {...} Else {...}:Does the Line Break Between "}" and "Else" Really Matters
How to Fit a Very Wide Grid.Table or Tablegrob to Fit on a PDF Page
Create Binary Column (0/1) Based on Condition in Another Column
Convert Many Formats of Date-Time to One Standard Format
Submit Form with No Submit Button in Rvest
What Is the Benefit of Import in a Namespace in R