Home > Software design >  How to manage a graphical table margins in R
How to manage a graphical table margins in R

Time:12-18

I am working in R Markdown and I am creating a graphical table in R. I run the following code:

library(gridExtra)
library(grid)
 
tt <- ttheme_default(
    core = list(fg_params=list(cex = 0.57)),
    colhead = list(fg_params=list(cex = 0.8))
)

grid.arrange(tableGrob(couples5, theme=tt, rows=NULL))

And I got the following table:

enter image description here

Is it possible to reduce the white margine above and below the table? Is it possible to manage a plot margin using the library grid? Here detail on the dataset:

structure(list(Sample_ID2 = c("111111", "22222", "33333", "4444444", 
"55555", "777777"), Sample_ID1 = c("111111", "22222", "33333", "4444444", 
"55555", "777777"), 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_1 = c("A4960_2022_31", "A4960_2022_31", "A4960_2022_31", 
"A4960_2022_31", "A4960_2022_31", "A4960_2022_215"), Plate_2 = c("A4960_2022_31", 
"A4960_2022_31", "A4960_2022_31", "A4960_2022_31", "A4960_2022_31", 
"A4960_2022_31"), Well_1 = c("G09", "B08", "H08", "E08", "F09", 
"F03"), Well_2 = c("G08", "B09", "H09", "E09", "F08", "C08"), 
    Sex2_1 = c(1L, 1L, 0L, 1L, 0L, 0L), Sex2_2 = c(1L, 0L, 0L, 
    1L, 1L, 0L)), row.names = c(NA, 6L), class = "data.frame")

CodePudding user response:

You could use fig.height in your Rmarkdown chunk to change the place of your table. Here you can see the white space will be less:

---
date: "2022-12-17"
output: html_document
---

```{r fig.height=2, fig.width=10}
couples5 <- structure(list(Sample_ID2 = c("111111", "22222", "33333", "4444444", 
"55555", "777777"), Sample_ID1 = c("111111", "22222", "33333", 
"4444444", "55555", "777777"), 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_1 = c("A4960_2022_31", "A4960_2022_31", "A4960_2022_31", 
"A4960_2022_31", "A4960_2022_31", "A4960_2022_215"), Plate_2 = c("A4960_2022_31", 
"A4960_2022_31", "A4960_2022_31", "A4960_2022_31", "A4960_2022_31", 
"A4960_2022_31"), Well_1 = c("G09", "B08", "H08", "E08", "F09", 
"F03"), Well_2 = c("G08", "B09", "H09", "E09", "F08", "C08"), 
    Sex2_1 = c(1L, 1L, 0L, 1L, 0L, 0L), Sex2_2 = c(1L, 0L, 0L, 
    1L, 1L, 0L)), row.names = c(NA, 6L), class = "data.frame")
library(gridExtra) 
library(grid)

tt <- ttheme_default( core = list(fg_params=list(cex = 0.57)), colhead = list(fg_params=list(cex = 0.8)) )

grid.arrange(tableGrob(couples5, theme=tt, rows=NULL))
```

Output:

enter image description here

CodePudding user response:

add heigths argument:

grid.arrange(tableGrob(couples5, theme=tt, rows=NULL), heights=c(3,2))

enter image description here

  •  Tags:  
  • r
  • Related