jthomasmock / gtExtras

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

gtExtras::gt_fa_column better palette handling #12

Closed JackLich10 closed 3 years ago

JackLich10 commented 3 years ago

The only reason I'm creating these issues is because the package is awesome! But, I have another feature request. Basically, in my doubled table from the other issue, I want to use font-awesome icons to show trends. My issue is that because the values of the icons do not line up in the same order across the trend and trend2 columns, the call within gtExtras::gt_fa_column of unique(x) changes based on the column, resulting in different colors being mapped to the same icons across the two columns. I'm hoping that a solution would be to input a named palette vector like I have below to solve this issue. Thanks for all your help and work!

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

fa_palette <- c("angle-double-up" = "#009E73",
                "angle-double-down" = "#D55E00",
                "sort" = "#000000")

readRDS(url("https://github.com/JackLich10/JackLich10/blob/main/gtextra_data.rds?raw=true"))%>%
  gt::gt() %>% 
  gt::cols_label(rank = "Rank",
                 trend = "",
                 team_logo_espn = "",
                 name = "",
                 adj_qb = "Composite",
                 adj_qb_data = "YTD",
                 rank2 = "Rank",
                 trend2 = "",
                 team_logo_espn2 = "",
                 name2 = "",
                 adj_qb2 = "Composite",
                 adj_qb_data2 = "YTD") %>%
  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_fa_column(column = trend, palette = fa_palette) %>% 
  gtExtras::gt_fa_column(column = trend2, palette = fa_palette) %>% 
  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.06, 0.21), 
                         palette = my_palette,
                         accuracy = 0.001) %>%
  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)

Results in:

Screen Shot 2021-10-03 at 10 43 24 AM

Note how Rodgers and Burrow have different icons, but are mapped the same color.

jthomasmock commented 3 years ago

Love it, please keep the ideas coming!

I would ask, if you could simplify the reprex, that would be great! See below:

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

fa_palette <- c("angle-double-up" = "#009E73",
                "angle-double-down" = "#D55E00",
                "sort" = "#000000")

df_in <- readRDS(url("https://github.com/JackLich10/JackLich10/blob/main/gtextra_data.rds?raw=true"))%>%
  select(trend, trend2) 

df_in %>% 
  gt() %>% 
  gt_fa_column(column = trend, palette = fa_palette) %>% 
  gt_fa_column(column = trend2, palette = fa_palette) 

The latest package version (v0.2.15) has enabled support for named color vectors. Essentially the names will be mapped to the levels and the colors will be mapped to the actual colors.

So your example:

c("angle-double-up" = "#009E73",
                "angle-double-down" = "#D55E00",
                "sort" = "#000000")

Works now with the internals.

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)
my_palette <- c("#ffffff", "#f2fbd2", "#c9ecb4", "#93d3ab", "#35b0ab")

fa_palette <- c("angle-double-up" = "#009E73",
                "angle-double-down" = "#D55E00",
                "sort" = "#000000")

df_in <- readRDS(url("https://github.com/JackLich10/JackLich10/blob/main/gtextra_data.rds?raw=true"))%>%
  select(trend, trend2) 

df_in %>% 
  slice(1:10) %>% 
  gt() %>% 
  gt_fa_column(column = trend, palette = fa_palette) %>% 
  gt_fa_column(column = trend2, palette = fa_palette) %>% 
  gtsave("test-tab.png")

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

JackLich10 commented 3 years ago

Awesome, and will do for future reprex's