Home > Software design >  How to decrease the size of a graphical table in R
How to decrease the size of a graphical table in R

Time:12-18

I have created a table using the following command lines:

library(gridExtra)
library(grid)

grid.arrange(tableGrob(ds, rows=NULL))

The table should have 10 columns and 9 rows, however I got the following table:

enter image description here Is there any way not to cut my table by using some kind of option in tableGrob()? Basically I need to reduce the size of the table to enter the pic margins.

here an overview of the table:

structure(list(Sample_ID2 = c("204822", "401227", "401230", "401504", 
"403439", "410853"), Sample_ID1 = c("777777", "8888888", "999999", 
"111111", "1219283", "55555"), PI_HAT = c(0.9999, 0.9999, 0.9999, 
0.9998, 0.9999, 0.9999), Kinship = c(0.5, 0.5, 0.5, 0.4999, 0.4999, 
0.5), Plate_name_SampleID1 = c("A4960_2022_31", "A4960_2022_31", 
"A4960_2022_31", "A4960_2022_31", "A4960_2022_31", "A4960_2022_215"
), Plate_name_SampleID2 = c("A4960_2022_31", "A4960_2022_31", 
"A4960_2022_31", "A4960_2022_31", "A4960_2022_31", "A4960_2022_31"
), Well_SampleID1 = c("G09", "B08", "H08", "E08", "F09", "F03"
), Well_SampleID2 = c("G08", "B09", "H09", "E09", "F08", "C08"
), Sex2_SampleID1 = c(1L, 1L, 0L, 1L, 0L, 0L), Sex2_SampleID2 = c(1L, 
0L, 0L, 1L, 1L, 0L)), row.names = c(NA, 6L), class = "data.frame")

CodePudding user response:

Here is a solution by changing the font size of the table head and its entries. But I suggest that you choose shorter names for your last 4 columns, since the spacing is adjusted automatically based on the content and the length of the column head.

library(gridExtra)
library(grid)

a <- structure(list(Sample_ID2 = c("204822", "401227", "401230", "401504", 
"403439", "410853"), Sample_ID1 = c("777777", "8888888", "999999", 
"111111", "1219283", "55555"), PI_HAT = c(0.9999, 0.9999, 0.9999, 
0.9998, 0.9999, 0.9999), Kinship = c(0.5, 0.5, 0.5, 0.4999, 0.4999, 
0.5), Plate_name_SampleID1 = c("A4960_2022_31", "A4960_2022_31", 
"A4960_2022_31", "A4960_2022_31", "A4960_2022_31", "A4960_2022_215"
), Plate_name_SampleID2 = c("A4960_2022_31", "A4960_2022_31", 
"A4960_2022_31", "A4960_2022_31", "A4960_2022_31", "A4960_2022_31"
), Well_SampleID1 = c("G09", "B08", "H08", "E08", "F09", "F03"
), Well_SampleID2 = c("G08", "B09", "H09", "E09", "F08", "C08"
), Sex2_SampleID1 = c(1L, 1L, 0L, 1L, 0L, 0L), Sex2_SampleID2 = c(1L, 
0L, 0L, 1L, 1L, 0L)), row.names = c(NA, 6L), class = "data.frame")
#tt <- ttheme_default(colhead=list(fg_params = list(parse=TRUE)))
#grid.table(a)
tt <- ttheme_default(
    core = list(fg_params=list(cex = 0.5)),
    colhead = list(fg_params=list(cex = 0.4)),
    rowhead = list(fg_params=list(cex = 0.5))
    )
grid.table(a, theme=tt)

Simply change and play a little bit with the numbers given for the font size cex = ??, to get your desired output. Furthermore, the reason that you get only 6 rows is that the table that you provided here only has 6 rows and not 9.

  •  Tags:  
  • r
  • Related