jthomasmock / gtExtras

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

Can I make multiple gt_plt_bullet? #21

Closed IvoVillanueva closed 2 years ago

IvoVillanueva commented 2 years ago

In this example, I would make 3 gt_plt_bullet, and only can make one, how Could I make

mtcars %>% mutate( mean_disp = mean(disp), mean_hp = mean(hp), mean_drat = mean(drat), ) %>% select(disp, mean_disp, hp, mean_hp, drat, mean_drat) %>% gt() %>% gt_plt_bullet(., keep_column = TRUE, column = disp, target = mean_disp, width = 30, colors = c("#f28422","#0e1631") )

Captura de pantalla 2021-10-21 a las 14 56 55

when i put 2 bullets

mtcars %>% mutate( mean_disp = mean(disp), mean_hp = mean(hp), mean_drat = mean(drat), ) %>% select(disp, mean_disp, hp, mean_hp, drat, mean_drat) %>% gt() %>% gt_plt_bullet(., keep_column = TRUE, column = disp, target = mean_disp, width = 30, colors = c("#f28422","#0e1631") ) %>% gt_plt_bullet(., keep_column = TRUE, column = hp, target = mean_hp, width = 30, colors = c("#f28422","#0e1631") )

Captura de pantalla 2021-10-21 a las 14 59 48
jthomasmock commented 2 years ago

Howdy! I'm refactoring that function, but for now, I'd suggest using the eventual replacement function gtExtras::duplicate_column(). The keep_column syntax is not likely to work longterm but we'll see.

See below, and note that disp and mpg still exist as columns in the data.

library(gt)
library(gtExtras)
set.seed(37)
df_in <- tibble::rownames_to_column(mtcars) %>%
 dplyr::select(rowname, cyl:drat, mpg) %>%
 dplyr::group_by(cyl) %>%
 dplyr::mutate(target_col = mean(mpg),
               target_disp = mean(disp)) %>%
 dplyr::slice_sample(n = 3) %>%
 dplyr::ungroup() 

df_in %>%
 gt::gt() %>%
# duplicate the columns for plotting
  gt_duplicate_column(mpg, dupe_name = "mpg_plot") %>% 
  gt_duplicate_column(disp, dupe_name = "disp_plot") %>% 
 gt_plt_bullet(column = mpg_plot, target = target_col, width = 45,
               colors = c("lightblue", "black")) %>%
  gt_plt_bullet(column = disp_plot, target = target_disp, width = 45,
                colors = c("lightblue", "black")) %>%
  gtsave("test.png")

Created on 2021-10-21 by the reprex package (v2.0.1)

IvoVillanueva commented 2 years ago

Thank you very much Thomas

jthomasmock commented 2 years ago

I've refactored that function internally so that the latest version of gtExtras works as intended with 2x keep_column without needing gt_duplicate_column() although I still like that as an option!