jthomasmock / gtExtras

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

Stretched numbers in gt_plt_bar_stack if different number of digits #23

Closed Kazink36 closed 2 years ago

Kazink36 commented 2 years ago

If the data in gt_plt_bar_stack has a different number of digits, the plot stretches the digits, likely due to the format() function. Passing trim = TRUE into the extra arguments produces the result I would expect as the default behavior.

library(gt)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

ex_df <- dplyr::tibble(
  x = c("Example 1","Example 1",
        "Example 1","Example 2","Example 2","Example 2",
        "Example 3","Example 3","Example 3","Example 4","Example 4",
        "Example 4"),
  measure = c("Measure 1","Measure 2",
              "Measure 3","Measure 1","Measure 2","Measure 3",
              "Measure 1","Measure 2","Measure 3","Measure 1","Measure 2",
              "Measure 3"),
  data = c(30, 2, 50, 30, 3, 40, 30, 4, 30, 30, 5, 20)
)

tab_df <- ex_df %>%
  group_by(x) %>%
  summarise(list_data = list(data))

tab_df
#> # A tibble: 4 × 2
#>   x         list_data
#>   <chr>     <list>   
#> 1 Example 1 <dbl [3]>
#> 2 Example 2 <dbl [3]>
#> 3 Example 3 <dbl [3]>
#> 4 Example 4 <dbl [3]>

ex_tab <- tab_df %>%
  mutate(trim_col = list_data) %>% 
  gt() %>%
  gtExtras::gt_plt_bar_stack(column = list_data) %>% 
  gtExtras::gt_plt_bar_stack(column = trim_col,trim = TRUE,labels = c("trim","=","TRUE"))
Screen Shot 2021-10-25 at 11 18 32 PM

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

jthomasmock commented 2 years ago

Thanks! Changed the internals to be scales::label_??? functions which provide more flexibility and better defaults than format() alone.