jthomasmock / gtExtras

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

Set small column width when using gt_plt_winloss()? #19

Closed gkaramanis closed 3 years ago

gkaramanis commented 3 years ago

Thanks for the great package, Tom!

My use case is that I want to always show the latest 5 results but with a narrower column so the pills are tighter. It seems that the smallest width is always 100 pixels even when I set it to values <100.

Is there a workaround for this?

data.frame(team = LETTERS[1:5]) %>% 
  rowwise() %>% 
  mutate(result = list(sample(c(0, 0.5, 1), 10, replace = TRUE))) %>%
  gt() %>% 
  gt_plt_winloss(result, max_wins = 5) %>% 
  cols_width(
    result ~ px(50)
    )

image

jthomasmock commented 3 years ago

Howdy!

You'll want to set the width argument, which sets the width of the plot in "mm".

Width

A numeric indicating the width of the plot in mm, this can help with larger datasets where data points are overlapping.

There's two components here, the max_wins which sets the length of the x-axis (in most sports leagues there are a set number of games) which folks want to align with, and then width which affects the ggsave() internals to widen/narrow the plot itself. Since the width of the inline "plot" is determined ahead of time, the cols_width() can only make the column wider.

See below:

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
library(gt)
library(gtExtras)
set.seed(37)
in_data <- data.frame(team = LETTERS[1:5]) %>% 
  rowwise() %>% 
  mutate(result = list(sample(c(0, 0.5, 1), 5, replace = TRUE))) %>%
  ungroup() 

in_data %>% 
  gt() %>% 
  # default
  gt_plt_winloss(result, max_wins = 5) %>% 
  gtsave("default.png")

in_data %>% 
  gt() %>% 
  # narrower
  gt_plt_winloss(result, max_wins = 5, width = 7) %>% 
  gtsave("narrow.png")

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

gkaramanis commented 3 years ago

Yes, it works! Thanks for the explanation, too 😃