jthomasmock / gtExtras

A Collection of Helper Functions for the gt Package.
https://jthomasmock.github.io/gtExtras/
Other
195 stars 27 forks source link

`gt_plt_conf_int()` draws white squares with `text_size = 0` #54

Closed jmbarbone closed 2 years ago

jmbarbone commented 2 years ago

When textsize = 0 there is still something plotted -- and this leaves a white background that is visible with a darker row color. When textsize <= 0 can the steps for plotting text in add_ci_plot() just be skipped to avoid this?

library(gtExtras)
dplyr::tibble(
  mean    = c(12, 10),
  ci1     = c( 8,  5),
  ci2     = c(16, 15),
  ci_plot = c(12, 10)
) %>%
  gt::gt() %>%
  gt_plt_conf_int(
    ci_plot, c(ci1, ci2),
    text_size = 0,
  ) %>% 
  gt::tab_options(
    row.striping.include_table_body = TRUE,
    row.striping.background_color = "black"
  ) 

Created on 2022-06-15 by the reprex package (v2.0.1)

image

jthomasmock commented 2 years ago

Fixed in dev-version, thanks for heads up. The underlying label had a background fill which I removed. Note there are two ways of removing the text, either one works:

library(gt)
library(gtExtras)

ci_table <- generate_df(
  n = 50, n_grps = 3,
  mean = c(10, 15, 20), sd = c(10, 10, 10),
  with_seed = 37
) %>%
  dplyr::group_by(grp) %>%
  dplyr::summarise(
    n = dplyr::n(),
    avg = mean(values),
    sd = sd(values),
    list_data = list(values)
  ) %>%
  gt::gt() %>%
  gt_plt_conf_int(list_data, ci = 0.9)

# You can also provide your own values
# based on your own algorithm/calculations
pre_calc_ci_tab <- dplyr::tibble(
  mean = c(12, 10), ci1 = c(8, 5), ci2 = c(16, 15),
  ci_plot = c(12, 10)
) %>%
  gt::gt() 

pre_calc_ci_tab1 <- pre_calc_ci_tab %>%
  gt_plt_conf_int(
    ci_plot, c(ci1, ci2),
    # can use text size = 0
    text_size = 0,
    # can use palette = c("red", "lightgrey", "black", "transparent")
    palette = c("red", "lightgrey", "black", "red")
  ) %>% 
  gt_theme_dark()

gtsave_extra(pre_calc_ci_tab1, "tab1.png", selector = "table")

knitr::include_graphics("tab1.png")


pre_calc_ci_tab2 <- pre_calc_ci_tab %>%
  gt_plt_conf_int(
    ci_plot, c(ci1, ci2),
    # can use text size = 0 or
    # can use palette = c("red", "lightgrey", "black", "transparent")
    palette = c("red", "lightgrey", "black", "transparent")
  ) %>% 
  gt_theme_dark()

gtsave_extra(pre_calc_ci_tab2, "tab2.png", selector = "table")

knitr::include_graphics("tab2.png")

Created on 2022-06-15 by the reprex package (v2.0.1)