gt icon indicating copy to clipboard operation
gt copied to clipboard

`tab_row_group()` doesn't group `ggplot_image` with the rest of the rows

Open askol-lurie opened this issue 3 years ago • 0 comments


When creating a table that contains, characters, numbers and ggplot_image objects, plots remain in their original position after the issuance of a tab_row_group (or probably other grouping command).

library(dplyr)
library(purrr)
library(gt)
library(tidyr)
library(ggplot2)

plots <- iris %>% 
  pivot_longer(-Species, 
               names_to = "Vars", 
               values_to = "Values") %>%
  group_by(Vars) %>%
  nest() %>%
  mutate(plot = map(data, 
                    function(df) df %>% 
                      ggplot(aes(Values)) + 
                      geom_histogram())) %>%
  select(-data)

irisGT <- iris %>%
  pivot_longer(-Species, 
               names_to = "Vars", 
               values_to = "Values") %>%
  group_by(Vars) %>%
  summarise(obs = n(),
            mean = round(mean(Values),2),
            median = round(median(Values),2)) %>%
  mutate(ggplot = NA) %>%
  gt() %>%
  text_transform(
    locations = cells_body(vars(ggplot)),
    fn = function(x) {
      map(plots$plot, ggplot_image, height = px(100))
    }
  )

irisGT2 <- irisGT  %>% 
  tab_row_group(
    label = "Petal",
    rows = 1:2
  ) %>%
  tab_row_group(
    label = "Sepal",
    rows = 3:4
  ) 

gtsave(irisGT, filename = "Iris1.html")
gtsave(irisGT2, filename = "Iris2.html")

Iris1.html.pdf

Iris2.html.pdf

Session info:

sessionInfo() R version 3.6.0 (2019-04-26) Platform: x86_64-redhat-linux-gnu (64-bit) Running under: CentOS Linux 7 (Core)

Matrix products: default BLAS/LAPACK: /usr/lib64/R/lib/libRblas.so

locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages: [1] parallel stats4 stats graphics grDevices utils datasets [8] methods base

other attached packages: [1] purrr_0.3.4 gt_0.3.1 ggfortify_0.4.12
[4] cowplot_1.0.0 ggpubr_0.4.0 ggplot2_3.3.5
[7] GenomicRanges_1.36.1 GenomeInfoDb_1.20.0 IRanges_2.18.3
[10] S4Vectors_0.22.1 BiocGenerics_0.30.0 vcfR_1.12.0
[13] optparse_1.6.4 tidyr_1.1.4 dplyr_1.0.7

loaded via a namespace (and not attached): [1] nlme_3.1-139 bitops_1.0-6 fs_1.5.0
[4] tools_3.6.0 backports_1.1.3 utf8_1.1.4
[7] R6_2.4.0 vegan_2.5-4 DBI_1.0.0
[10] mgcv_1.8-28 colorspace_1.4-0 permute_0.9-5
[13] withr_2.4.2 tidyselect_1.1.0 gridExtra_2.3
[16] curl_3.3 compiler_3.6.0 cli_3.0.1
[19] sass_0.4.0 labeling_0.3 scales_1.1.1
[22] checkmate_2.0.0 rappdirs_0.3.3 commonmark_1.7
[25] systemfonts_1.0.3 stringr_1.4.0 digest_0.6.25
[28] foreign_0.8-71 rio_0.5.16 XVector_0.24.0
[31] base64enc_0.1-3 pkgconfig_2.0.2 htmltools_0.5.2
[34] fastmap_1.1.0 rlang_0.4.11 readxl_1.3.1
[37] rstudioapi_0.13 generics_0.0.2 farver_2.0.3
[40] zip_2.2.0 car_3.0-8 RCurl_1.95-4.12
[43] magrittr_2.0.1 GenomeInfoDbData_1.2.1 Matrix_1.2-17
[46] Rcpp_1.0.1 munsell_0.5.0 fansi_0.4.0
[49] ape_5.3 abind_1.4-5 lifecycle_1.0.1
[52] stringi_1.4.3 carData_3.0-4 MASS_7.3-51.4
[55] zlibbioc_1.30.0 pinfsc50_1.1.0 grid_3.6.0
[58] forcats_0.5.1 crayon_1.4.1 lattice_0.20-38
[61] haven_2.4.3 splines_3.6.0 hms_1.1.1
[64] pillar_1.6.3 tcltk_3.6.0 ggsignif_0.6.0
[67] glue_1.4.2 data.table_1.14.2 vctrs_0.3.8
[70] cellranger_1.1.0 gtable_0.2.0 getopt_1.20.3
[73] assertthat_0.2.0 openxlsx_4.1.5 broom_0.7.9
[76] rstatix_0.7.0 ragg_0.2.0 viridisLite_0.3.0
[79] tibble_3.1.5 cluster_2.0.8 ellipsis_0.3.2

askol-lurie avatar Oct 28 '21 20:10 askol-lurie