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.
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)
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:
In Rmarkdown looks nice with these settings: