Closed erdirstats closed 3 years ago
Thanks for submitting!
Please see the new behavior for gt_sparkline()
, refactored to use a density()
calc internally. This has a better raw implementation of a density.
You can install with remotes::install_github("jthomasmock/gtExtras")
library(gt)
library(dplyr, warn.conflicts = FALSE)
library(purrr)
set.seed(37)
fake_data <- tibble(
x = sprintf("%000d", sample(1:300)),
group = rep(c("grp-1", "grp-2", "grp-3"), each = 100),
data = purrr::map2(c(10, 20, 30), c(1, 2, 3), ~rnorm(100, .x, .y)) %>%
unlist()
)
fake_data %>%
group_by(group) %>%
dplyr::summarize(
mean = mean(data),
sd = sd(data),
list_data = list(data), .groups = "drop") %>%
gt() %>%
gtExtras::gt_sparkline(list_data, type = "density") %>%
gtsave("test-table.png")
Created on 2021-10-02 by the reprex package (v2.0.1)
Unfortunately, this doesn't seem to solve the issue when the tail of the density plot is just long (which happens a lot in my case).
Gotcha. So the approach that the ggplot2
team takes is available via stat_density(trim = TRUE)
Reference source code available at: https://github.com/tidyverse/ggplot2/blob/master/R/stat-density.r#L88-L93
I have chosen a slightly expanded range()
via scales::expand_range()
. Note that this will essentially limit the density to the observed x-range + 5% on each end rather than the simple range()
in ggplot2
.
I've pushed version gtExtras
v0.2.15, feel free to take it for a spin and if the trim
is ideal.
library(gt)
library(dplyr, warn.conflicts = FALSE)
library(purrr)
set.seed(37)
fake_data <- tibble(
x = sprintf("%000d", sample(1:300)),
group = rep(c("grp-1", "grp-2", "grp-3"), each = 100),
data = purrr::map2(c(10, 20, 30), c(1, 2, 3), ~rnorm(100, .x, .y)) %>%
unlist()
)
fake_data %>%
dplyr::group_by(group) %>%
dplyr::summarize(
mean = mean(data),
sd = sd(data),
list_data = list(data), .groups = "drop") %>%
gt() %>%
gtExtras::gt_sparkline(list_data, type = "density", trim = TRUE) %>%
gtsave("test-table.png")
Created on 2021-10-04 by the reprex package (v2.0.1)
Thanks a lot! It looks nice! Maybe the trim is a bit too much when there are no long tails, but in overall it would work I guess.
Cheers! Yah, I'm not a big fan of trimming but took the same approach as ggplot so it's at least consistent.
Hi. It would be great to have an option to limit the x axis, to avoid the long tails, when using the
type = "density"
option withingt_sparkline()
. The optionsame_limit = FALSE
doesn't always solve the problem and when it does, it has the side effect of making comparisons between rows very difficult. An example where this feature might be useful is shown in the image: