Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gt_color_row colouring based on value in a different column #68

Closed
heerymichael opened this issue Sep 16, 2022 · 1 comment
Closed

gt_color_row colouring based on value in a different column #68

heerymichael opened this issue Sep 16, 2022 · 1 comment

Comments

@heerymichael
Copy link

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
Copy link
Owner

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants