You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在gt表格中为汇总行添加sparkline迷你图?

问题:gt表格中让汇总行也显示sparkline迷你图

我想在gt表格里,让数据行和汇总行(包括总计行)都显示gtExtras包的sparkline迷你图,但目前只能在原始数据行生成,汇总行无法显示。现有实现代码如下:

library(tidyverse)
library(gt)
library(gtExtras)

these_years <- c(1980,1990,2000)

df <- countrypops |>
  dplyr::filter(country_code_2 %in% c("BE", "NL", "LU",
                                      "DK", "SE", "NO"),
                year %in% these_years) |>
  dplyr::mutate(region = ifelse(country_code_2 %in% c("BE", "NL", "LU"),
                         "Benelux",
                         "Scandinavia")) |> 
  dplyr::select(region, country_name, year, population) |>
  tidyr::pivot_wider(names_from = year, values_from = population) 

df |>
  mutate(trend = list(c_across(where(is.numeric))), .by = country_name) |>
  gt(
    rowname_col = "country_name",
    groupname_col = "region"
  ) |>
    summary_rows(
    groups = everything(),
    columns = num_range(prefix = "", these_years),
    fns = list(
      Sum =  ~ sum(., na.rm = TRUE)
    )
  ) |>
  grand_summary_rows(
    columns = num_range(prefix = "", these_years),
    fns = list(
      Grand_Sum =  ~ sum(., na.rm = TRUE)
    ),
    side = c("bottom"),
    missing_text = "---"
  ) |>
  gtExtras::gt_plt_sparkline(
    column = trend,
    palette = c("grey40", "grey40", "grey40", "dodgerblue1", "grey40"),
    type = "points",
    same_limit = FALSE
  )

通过summary_rows()和grand_summary_rows()生成的汇总行无法直接应用gt_plt_sparkline(),因为这些汇总行是gt表格层面动态生成的,并不存在于原始数据框中,也就没有对应的trend列(存储迷你图数据的列表列)。

可行解决方案

手动在数据框层面添加分组汇总行和总计行,生成对应的trend列后再用gt渲染,即可让所有行(包括汇总行)都显示sparkline迷你图。实现代码如下:

df |>
   bind_rows(df |>
              pivot_longer(cols = -c(region, country_name),
                           names_to = "year",
                           values_to = "value") |>
              summarise(total = sum(value), .by = c(region, year)) |>
              pivot_wider(names_from = year,
                          values_from = total) |>
              mutate(country_name = "Sum"),
            df |>
              pivot_longer(cols = -c(region, country_name),
                           names_to = "year",
                           values_to = "value") |>
              summarise(total = sum(value), .by = c(year)) |>
              pivot_wider(names_from = year,
                          values_from = total) |>
              mutate(country_name = "Grand_Sum",
                     region = "")) |>
  mutate(trend = list(c_across(where(is.numeric))), .by = country_name) |>
  gt(
    #rowname_col = "country_name",
    groupname_col = "region"
  ) |>
  tab_style(
    style = cell_borders(
      sides = c("top", "bottom"),
      weight = px(1.5),
      style = "solid"
    ),
    locations = cells_body(rows = country_name == "Sum")
  ) |>
  gtExtras::gt_plt_sparkline(
    column = trend
  )

内容的提问来源于stack exchange,提问作者thebasket

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 21:56:03