0

I would like to create beautiful tables with gt package and put three of them side by side using knitr function kable. It does not work as expected. The gt_tbl object is a list and kable prints all elements separately. Is there a way to have nice side by side gt tables? The output format is bookdown / markdown html.

library(tidyverse)
library(gt)

cars <- head(mtcars) %>% 
  gt() %>%
  tab_header(
    title = "Mtcars"
  )

flowers <- head(iris) %>% 
  gt() %>%
  tab_header(
    title = "Iris"
  )

knitr::kable(list(cars, flowers)) 

I also tried this solution Combine multiple gt tables to single one plot which does not yield any good solution. Perhaps gtsave, ggdraw and draw_image can be modified. But I didn't find a good scaling parameter.

library(tidyverse)
library(gt)

crosssection <- data.frame(id = c(1:6),
                           year = rep(2016,6),
                           income = c(20,15,35,10,76,23))
repeatedcross <- data.frame(id = c(1:6),
                           year = c(rep(2015,3),rep(2016,3)),
                           income = c(20,15,35,10,76,23))
paneldata <- data.frame(id = c(rep(1,3), rep(2,3)),
                           year = rep(c(2014:2016),2),
                           income = c(20,15,35,10,76,23))

cs <- crosssection %>%
  gt() %>%
  tab_header(
    title = "Cross-Section" # #subtitle = "Cross-Section"
  )

rc <- repeatedcross %>%
  gt() %>%
  tab_header(
    title = "Repeated Cross-Section"
  ) 

pd <- paneldata %>%
  gt() %>%
  tab_header(
    title = "Paneldata"
  ) 

cs %>% gtsave("p11.png", path = "Data/")
rc %>% gtsave("p12.png", path = "Data/")
pd %>% gtsave("p13.png", path = "Data/")

library(cowplot)
p111 <- ggdraw() + draw_image("Data/p11.png", scale = 0.8)
p112 <- ggdraw() + draw_image("Data/p12.png", scale = 0.8)
p113 <- ggdraw() + draw_image("Data/p13.png", scale = 0.8)

plot_grid(p111, p112, p113)

Those tables would clearly fit sidy by side when space between them is optimally used.

enter image description here

After applying the suggestion fixing scale parameter and setting nrow=1 tables are still misaligned in the Viewer pane, as well as the markdown files.

p111 <- ggdraw() + draw_image("Data/p11.png", scale = 0.7)
p112 <- ggdraw() + draw_image("Data/p12.png", scale = 0.96)
p113 <- ggdraw() + draw_image("Data/p13.png", scale = 0.7)

plot_grid(p111, p112, p113, nrow = 1)

enter image description here

Marco
  • 2,368
  • 6
  • 22
  • 48
  • You can look [there](https://stackoverflow.com/questions/70603983/combine-multiple-gt-tables-to-single-one-plot) Maybe this solution is suitable for you. If not - we will think further. – manro Sep 16 '22 at 14:24
  • I tried this solution. It does not yield good results for slightly different tables. And I didn't found any options to modify it. But I would go with any easy option that produces a nice result. – Marco Sep 16 '22 at 14:43
  • I'll try to play with your example, thanks. – manro Sep 16 '22 at 14:46

1 Answers1

1

You were near the result, I have only finished it for you.

In most cases we should control the scale parameter manually.

Code:

library(gt)
library(tidyverse)

crosssection <- data.frame(id = c(1:6),
                           year = rep(2016,6),
                           income = c(20,15,35,10,76,23))
repeatedcross <- data.frame(id = c(1:6),
                            year = c(rep(2015,3),rep(2016,3)),
                            income = c(20,15,35,10,76,23))
paneldata <- data.frame(id = c(rep(1,3), rep(2,3)),
                        year = rep(c(2014:2016),2),
                        income = c(20,15,35,10,76,23))

cs <- crosssection %>%
    gt() %>%
    tab_header(
        title = "Cross-Section" # #subtitle = "Cross-Section"
    )

rc <- repeatedcross %>%
    gt() %>%
    tab_header(
        title = "Repeated Cross-Section"
    ) 

pd <- paneldata %>%
    gt() %>%
    tab_header(
        title = "Paneldata"
    ) 

cs %>% gtsave("marco1.png")
rc %>% gtsave("marco2.png")
pd %>% gtsave("marco3.png")

library(cowplot)
p111 <- ggdraw() + draw_image("marco1.png", scale = 0.7)
p112 <- ggdraw() + draw_image("marco2.png", scale = 0.96)
p113 <- ggdraw() + draw_image("marco3.png", scale = 0.7)

plot_grid(p111, p112, p113, nrow = 1)

Output:

enter image description here


In Rmarkdown looks nice with these settings:

enter image description here

manro
  • 3,529
  • 2
  • 9
  • 22
  • Did you produce this is an empty markdown file? For me, the tables get closer, but are mis-aligned. – Marco Sep 16 '22 at 15:22
  • @Marco i did it into console and made a screenshot in ```plots``` pane – manro Sep 16 '22 at 15:23
  • 1
    @Marco Look to an addition. As I said earlier - in the most cases we should control ```scale``` manually. – manro Sep 16 '22 at 15:47
  • Can you give me a hint on spacing of the pasted tables? Each table `marco1` is perfectly fine, no margins. Pasted together there is a lot space above and below the tables. – Marco Sep 17 '22 at 12:28
  • @Marco you should play with ```gt``` table's margins. Look [here](https://gt.rstudio.com/reference/tab_options.html) After this you can repeat all actions ```ggsave ->ggdraw -> plot_grid``` – manro Sep 17 '22 at 12:45
  • @Marco Also look here: https://stackoverflow.com/questions/67723866/is-there-a-way-in-gt-to-remove-the-strange-white-border-around-the-table-when – manro Sep 17 '22 at 13:09