jthomasmock / gtExtras

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

Issues with gt_sparkline and gt_color_box #11

Closed JackLich10 closed 2 years ago

JackLich10 commented 2 years ago

I am having issues with the two specified functions. Here is reproducible code:

readRDS(url("https://github.com/JackLich10/JackLich10/blob/main/gtextra_data.rds?raw=true")) %>%
gt::gt() %>% 
  gt::cols_label(rank = "Rank",
                 team_logo_espn = "",
                 name = "",
                 adj_qb = "Composite",
                 adj_qb_data = "",
                 rank2 = "Rank",
                 team_logo_espn2 = "",
                 name2 = "",
                 adj_qb2 = "Composite",
                 adj_qb_data2 = "") %>%
  gt::fmt_number(columns = c(adj_qb, adj_qb2),
                 decimals = 3) %>% 
  gt::tab_style(style = list(
    gt::cell_borders(
      sides = "left",
      color = "black",
      weight = gt::px(3))),
    locations = list(
      gt::cells_body(
        columns = c(rank2)))) %>%
  gt::tab_style(style = list(
    gt::cell_borders(
      sides = "bottom",
      color = "black",
      weight = gt::px(3))),
    locations = list(
      gt::cells_column_labels(
        columns = gt::everything()))) %>% 
  gtExtras::gt_sparkline(column = adj_qb_data) %>% 
  gtExtras::gt_sparkline(column = adj_qb_data2) %>% 
  gtExtras::gt_color_box(columns = c(adj_qb, adj_qb2), 
                         domain = c(-0.055, 0.202)), 
                         palette = c("#ffffff", "#f2fbd2", "#c9ecb4", "#93d3ab", "#35b0ab")) %>%
  gt::text_transform(locations = gt::cells_body(columns = c(team_logo_espn, team_logo_espn2)),
                     fn = function(x) gt::web_image(url = x, height = 25)) %>% 
  gtExtras::gt_theme_538() %>% 
  gt::tab_options(source_notes.font.size = 9,
                  table.font.size = 12)

My table looks like this:

Screen Shot 2021-10-02 at 11 51 36 PM

The issues are:

I get these warnings when performing as well:

geom_path: Each group consists of only one observation. Do you need to adjust
the group aesthetic?
geom_path: Each group consists of only one observation. Do you need to adjust
the group aesthetic?
Warning messages:
1: Removed 1 row(s) containing missing values (geom_path). 
2: Removed 1 rows containing missing values (geom_point). 
3: Removed 1 rows containing missing values (geom_point). 
4: Removed 1 row(s) containing missing values (geom_path). 
5: Removed 1 rows containing missing values (geom_point). 
6: In fn(body[[col]][stub_df$rownum_i %in% loc$rows]) :
  NAs introduced by coercion

Thanks for any help! Love the package

jthomasmock commented 2 years ago

Howdy, thanks for the reprex!

For the "rounded numbers", note that gt_color_box() accepts input to the scales::label_number() function. I'd try setting accuracy = 0.01.

Ex:

library(gt)
test_data <- dplyr::tibble(x = letters[1:10],
                    y = seq(1, .1, by = -.1),
                    z = seq(.001, .1, by = .01))

test_data %>%
  gt() %>%
  gt_color_box(
    columns = y, domain = 0.1:1, 
    palette = "ggsci::blue_material", accuracy = 0.01) %>%
  gt_color_box(columns = z, domain = range(test_data$z),
               palette = c("purple", "lightgrey", "green"),
               accuracy = 0.001)

I'll explore the other two notes, and get back to you.

JackLich10 commented 2 years ago

Hmm, when I run your example for accuracy in gt_color_box, I get the following error:

Error in gtExtras::gt_color_box(., columns = z, domain = range(test_data$z),  : 
  unused argument (accuracy = 0.001)

This is under gtExtras version: gtExtras_0.2.4

jthomasmock commented 2 years ago

Ok, so I just pushed an update, should correct your specific problem re: sparkline cutting off. Here's a minimal version of your reprex, returned. Please install latest package version. 0.2.4 is about 9 versions behind, the joy of a under development package that is only 3 weeks old is the freedom to tinker and integrate new features before I get it on CRAN 😉

remotes::install_github("jthomasmock/gtExtras")

Note the use of gt_double_table() + gt_two_column_layout() rather than duplicating the columns.

library(gt)
library(dplyr)
raw_data <- readRDS(url("https://github.com/JackLich10/JackLich10/blob/main/gtextra_data.rds?raw=true"))

long_df <- raw_data %>% 
  select(rank:adj_qb_data) %>% 
  bind_rows(
    raw_data %>% 
      select(rank2:last_col()) %>% 
      purrr::set_names(nm = names(raw_data)[1:5])
  )

my_palette <- c("#ffffff", "#f2fbd2", "#c9ecb4", "#93d3ab", "#35b0ab")

my_gt_table <- function(x){
  gt::gt(x) %>% 
    gt::cols_label(
      rank = "Rank", team_logo_espn = "", name = "", adj_qb = "Composite",
      adj_qb_data = ""
    ) %>%
    gtExtras::gt_img_rows(team_logo_espn) %>% 
    gt_sparkline(column = adj_qb_data) %>% 
    gtExtras::gt_color_box(columns = adj_qb, 
                           domain = c(-0.06, 0.21), 
                           # Set accuracy, which is passed to
                           # scales::label_number()
                           accuracy = 0.001, 
                           palette = my_palette) %>% 
    gtExtras::gt_theme_538()
}

gtExtras::gt_double_table(long_df, my_gt_table, nrows = 16) %>% 
  gtExtras::gt_two_column_layout()
JackLich10 commented 2 years ago

Just re-installed, working great now. I still prefer the output of duplicating columns rather than the double table function, but thank you so much! One small feature request would be to allow the user to specify the size of both the line in gt_sparkline and the size of the min and max points, since sometimes I think they are rather small.

JackLich10 commented 2 years ago

Last thing: When you use gt::fmt_number(columns = c(adj_qb, adj_qb2), decimals = 3) when making the table, Justin Fields value is still NA no matter the lower domain value. However, when removing this code (the gt::fmt_number), it works fine.

raw_data <- readRDS(url("https://github.com/JackLich10/JackLich10/blob/main/gtextra_data.rds?raw=true"))
raw_data %>%
  gt::gt() %>% 
  gt::cols_label(rank = "Rank",
                 team_logo_espn = "",
                 name = "",
                 adj_qb = "Composite",
                 adj_qb_data = "YTD",
                 rank2 = "Rank",
                 team_logo_espn2 = "",
                 name2 = "",
                 adj_qb2 = "Composite",
                 adj_qb_data2 = "YTD") %>%
  gt::fmt_number(columns = c(adj_qb, adj_qb2), # Can remove this and works fine
                 decimals = 3) %>% 
  gt::tab_style(style = list(
    gt::cell_borders(
      sides = "left",
      color = "black",
      weight = gt::px(3))),
    locations = list(
      gt::cells_body(
        columns = c(rank2)))) %>%
  gt::tab_style(style = list(
    gt::cell_borders(
      sides = "bottom",
      color = "black",
      weight = gt::px(3))),
    locations = list(
      gt::cells_column_labels(
        columns = gt::everything()))) %>% 
  gtExtras::gt_sparkline(column = adj_qb_data, same_limit = TRUE) %>% 
  gtExtras::gt_sparkline(column = adj_qb_data2, same_limit = TRUE) %>% 
  gtExtras::gt_color_box(columns = c(adj_qb, adj_qb2), 
                         domain = c(-0.1, 0.21), 
                         palette = my_palette,
                         accuracy = 0.01) %>%
  gt::text_transform(locations = gt::cells_body(columns = c(team_logo_espn, team_logo_espn2)),
                     fn = function(x) gt::web_image(url = x, height = 25)) %>% 
  gtExtras::gt_theme_538() %>% 
  gt::tab_options(source_notes.font.size = 9,
                  table.font.size = 12)
jthomasmock commented 2 years ago

Gotcha! fmt_number and friends can interfere and are redundant with internal use of scales::label_number() with ability to modify the values/rounding/etc.

If you are affecting columns with gt_color_box, I'd stick with the arguments passed to scales::label_number().

jthomasmock commented 2 years ago

Going to close this one out for now, thanks for the feedback!

shakhrukhb commented 1 year ago

When I try to reproduce the work of [JackLich10], it shows the following error: Error: 'gt_sparkline' is not an exported object from 'namespace:gtExtras'

I am wondering that 'gt_sparkline()' function is not longer available in gtExtras?