jthomasmock / gtExtras

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

How to manually set the color of sparkline lines and extrema in gt_plt_sparkline() of gtExtras 0.4.3 #73

Closed ahbon123 closed 1 year ago

ahbon123 commented 1 year ago

Hello Tom,

On gtExtras 0.3.8, we can set the color of sparkline lines and extrema by parameters line_color = 'grey', range_colors = c('blue', 'red')(refer to gt_sparkline())

When gtExtras is upgraded to version 0.4.3, we need to execute the following code to plot sparkline (refer to gt_plt_sparkline()):

library(gt)
library(gtExtras)
mtcars %>%
  dplyr::group_by(cyl) %>%
  # must end up with list of data for each row in the input dataframe
  dplyr::summarize(mpg_data = list(mpg), .groups = "drop") %>%
  gt() %>%
  gt_plt_sparkline(mpg_data,
                             type = "shaded",
                             # palette =c('blue', 'red'),
                             same_limit = FALSE,
                             label = TRUE)

Out:

image

I don't see parameters like line_colorand range_colors in new version of gtExtras, does that mean we won't be able to manually modify the color of lines and extremes or there are other ways to doing so? Thanks.

jthomasmock commented 1 year ago

Howdy @ahbon123 ! Please see the documentation for that function and the palette argument:

palette

A character string with 5 elements indicating the colors of various components. Order matters, and palette = sparkline color, final value color, range color low, range color high, and 'type' color (eg shading or reference lines).

The first element of the palette argument is the sparkline color.

For your example snippet:

library(gt)
mtcars %>%
  dplyr::group_by(cyl) %>%
  # must end up with list of data for each row in the input dataframe
  dplyr::summarize(mpg_data = list(mpg), .groups = "drop") %>%
  gt() %>%
  gt_plt_sparkline(mpg_data, palette = c("grey", "black", "red", "blue", "lightgrey"))

image