jthomasmock / gtExtras

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

gt_color_row colouring based on value in a different column #68

Closed heerymichael closed 1 year ago

heerymichael commented 2 years ago

Hi -

I was just wondering is it possible to colour code a column based on the value of a different column. I have attached a screen shot picture of a table I have produced below. As can be seen I have been able to colour code the numeric column for each game week, but not able to get the same colour background for the column with the team logos which I would like to test to see if it looks better.

Screenshot 2022-09-16 at 10 30 05

Is it possible to achieve this effect?

The code I am using for the numeric column is just the basic gtextras function:

gt_color_rows(columns = c(3,5,7,9), palette = c("#7d42be", "#ffffff", "#37af4a"), domain = c(0 - qb_palette_limit, 0, qb_palette_limit))

I have tried playing around with the domain part for the column 2,3,6,8 to be coded against columns 3,5,7,9 but can't get it to work at all!

jthomasmock commented 1 year ago

Howdy! It's not currently possible right now - there are some possible hacks but nothing first class in gt or gtExtras to color the entire background of a column by another column. You could use something like gt_img_circle():

library(gt)
library(gtExtras)
library(dplyr)

set.seed(37)

teams <- tibble::tribble(
  ~team_abb,                                               ~logo,
      "PIT", "https://a.espncdn.com/i/teamlogos/nfl/500/pit.png",
      "IND", "https://a.espncdn.com/i/teamlogos/nfl/500/ind.png",
      "SEA", "https://a.espncdn.com/i/teamlogos/nfl/500/sea.png",
      "CAR", "https://a.espncdn.com/i/teamlogos/nfl/500/car.png",
      "CLE", "https://a.espncdn.com/i/teamlogos/nfl/500/cle.png"
  ) |> 
  mutate(vals = sample(50:500, size = 5))

all_vals <- gt_index(gt(teams), vals)

pal_cls <- c("purple", "#d3d3d3", "green")

bord_clrs <- scales::col_numeric(palette = pal_cls, domain = range(all_vals))(all_vals)

gt(teams) |> 
  gt_img_circle(logo, border_color = bord_clrs, height = 30) |> 
  gtsave("test-img.png")

knitr::include_graphics("test-img.png")

Created on 2022-10-02 by the reprex package (v2.0.1)