jthomasmock / gtExtras

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

Is there a way to color the Sparkline in gtExtras? #103

Closed wagathu closed 11 months ago

wagathu commented 12 months ago

I raised the same question on SO

I am working on a gtExtras table with the following example

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

dd <- data.frame(
  where = c("where1", "where2", "where3", "where4"),
  "year_2014" = c(30, 60, 20, 10),
  "year_2022" = c(10, 50, 30, 20)) 

dd |> rowwise() |>
  mutate(change = list(c(year_2014, year_2022))) |>
  gt() |>
  gt_plt_sparkline(change, label = FALSE)

I intend to color the Sparkline (the full Sparkline and not the end, as it in the default) depending on whether there was a decrease "green" or an increase "red".

How do I proceed?

image

jthomasmock commented 11 months ago

Howdy! Thanks for the feature request.

I don't currently have plans to enable this feature because "change" is relatively arbitrary, because are we just looking at the first vs last? The overall trend? Or some other constraint.

I think a better option is user defined value that indicate increases or decreases, such as:

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

dd <- data.frame(
  where = c("where1", "where2", "where3", "where4"),
  "year_2014" = c(30, 60, 20, 10),
  "year_2022" = c(10, 50, 30, 20)) 

dd |> rowwise() |>
  mutate(change = list(c(year_2014, year_2022))) |>
  mutate(dir = last(change)- first(change)) |>
  gt() |>
  gt_plt_sparkline(change, label = FALSE) |> 
  gtExtras::gt_fa_rank_change(
    column = dir, font_color = "match"
  )

image

jthomasmock commented 11 months ago

Closing as not planned