Home > front end >  How to manually set the color of sparkline lines and extrema in gt_plt_sparkline() of gtExtras 0.4.3
How to manually set the color of sparkline lines and extrema in gt_plt_sparkline() of gtExtras 0.4.3

Time:11-04

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 enter image description here

Parameters of gt_plt_sparkline() include:

gt_plt_sparkline(
   gt_object,
   column,
   type = "default",
   fig_dim = c(5, 30),
   palette = c("black", "black", "purple", "green", "lightgrey"),
   same_limit = TRUE,
   label = TRUE
)

I don't see parameters like line_color and range_colors, 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?

CodePudding user response:

Have a closer look at the docs (?gt_plt_sparkline). You could set the colors via the palette argument:

A character string 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).

Here I an example of how tho set the colors.

library(gt)
library(gtExtras)

sparkline <- "blue"
final_value <- "red"
range_low <- "yellow"
range_high <- "purple"
type <- "lightgrey"

spkl_palette <- c(sparkline, final_value, range_low, range_high, type)

gt_sparkline_tab <- mtcars %>%
  dplyr::group_by(cyl) %>%
  dplyr::summarize(mpg_data = list(mpg), .groups = "drop") %>%
  gt() %>%
  gt_plt_sparkline(mpg_data, palette = spkl_palette)

gt_sparkline_tab

enter image description here

  • Related