jthomasmock / gtExtras

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

Sparkline is not sensitive to data fluctuation #34

Closed ahbon123 closed 2 years ago

ahbon123 commented 2 years ago

For the following data, we can see the data fluctuate slightly, but the plot spark is almost a straight line, is it possible to solve this issue? Sincere thanks.

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

df <- structure(list(type = c("v1", "v2"), `2017-06` = c(244.955, 1.2), `2017-07` = c(250, 1.5), `2017-08` = c(252, 1.8
), `2017-09` = c(245, 0.5)), class = "data.frame", row.names = c(NA, -2L))

df %>%
  rowwise() %>%
  mutate(data = list(c_across(-type))) %>%
  select(type, data) %>%
  gt() %>%
  gt_sparkline(data)

1639964605(1)

jthomasmock commented 2 years ago

Howdy! Per the gt_sparkline docs, you'll likely want to make use of the same_limit argument.

same_limit | A logical indicating that the plots will use the same axis range (TRUE) or have individual axis ranges (FALSE).

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

df <- structure(list(type = c("v1", "v2"), `2017-06` = c(244.955, 1.2), `2017-07` = c(250, 1.5), `2017-08` = c(252, 1.8
), `2017-09` = c(245, 0.5)), class = "data.frame", row.names = c(NA, -2L))

df %>%
  rowwise() %>%
  mutate(data = list(c_across(-type))) %>%
  select(type, data) %>%
  gt() %>%
  gt_sparkline(data, same_limit = FALSE) %>% 
  gtsave("test.png")

Created on 2021-12-20 by the reprex package (v2.0.1)