jthomasmock / gtExtras

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

Add label feature for `gt_plt_bar_pct` #99

Closed andreweatherman closed 1 year ago

andreweatherman commented 1 year ago

I mentioned this briefly in Discord, but there is no support for adding column value labels with gt_plt_bar_pct -- as there is with gt_plt_bar.

E.g. plotting with gt_plt_bar_pct

set.seed(1)
data.frame(val = runif(10)) |>
   gt::gt() |>
   gt::cols_width(val ~ px(100)) |>
   gtExtras::gt_plt_bar_pct(val)

I've modified the gt_plt_bar_pct function to add this functionality. It also supports arguments to adjust the font style (bold, normal, italic), round the label number, add percent signs, and specify the font color for the inside/outside labels.

By default, the function does not include labels -- this can be toggled by setting labels to TRUE.

The function places any labels for values <= 50% (of the max. value) to the right of the bar, while labels for all values > 50% are placed inside the bars. The user can specify any value for this cutoff (especially useful if you want to show more than 1 or 2 digits).

E.g., plotting with the same data:

source('https://gist.github.com/andreweatherman/0a81c7122a133a015583587573a123ff/raw/47bf96362798887a319a84f5e073b94acc534ed2/gt_plt_bar_pct.R')

set.seed(1)
data.frame(val = runif(10),
           val2 = runif(10)) |>  
  gt() |> 
  cols_width(everything() ~ px(100)) |> 
  gt_bars(val) |> 
  gt_bars(val2, labels = TRUE)

I added documentation for the new arguments in the PR. I tried for consistency re: your docs, but you should definitely check and compare.