{gtExtras} 列在分组时在 {gt} 表中显示的顺序错误

问题描述 投票:0回答:1

我正在制作一个

gt
表格,显示个人实现目标的进度。表中的一行显示了实现该目标的进度水平条形图(如果目标为 50,得分为 40,则条形图位于 80%)。

但是,当我使用

groupname_col
参数更改 gt 行的顺序时,其他单元格的顺序会发生变化,但
gtExtras
gt_plt_bar_pct
列的顺序不会改变,因此它显示了错误的条形该行中的名称和分数,相反,该列似乎始终按输入数据中的行顺序表示。

我知道我可以通过在

arrange
开始之前在 df 上使用
gt
来解决此问题,但这似乎不是一个好的解决方案,因为我想要更改要查看的行的顺序不同的群体。这是
gtExtras
的缺陷吗?有更好的解决办法吗?

代表:

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

# make dataframe of individuals and their goals
df <- tribble(
  ~name, ~group, ~score, ~goal,
    "Bob", "C",   20,   40,
    "Chris",  "A", 50,   40,
    "Dale",  "B",  30,   50,
    "Jay",    "A", 0,   40,
     "Ben",   "B", 10,   20
  
) %>%
  # calculate percent towards goal, and cap at 100%
  mutate(percent_to_goal = score/goal *100,
         percent_to_goal = case_when(percent_to_goal >= 100 ~ 100,
                                     TRUE ~ percent_to_goal))


df %>%
  
  # this fixes the issue, but doesn't seem like a permanent solution
  #arrange(group, name) %>%
  
  # make gt table
  gt(rowname_col = "name", groupname_col = "group") %>%
  
  # order groups
  row_group_order(groups = c("A","B","C")) %>%
  
  # add bar chart column
  gt_plt_bar_pct(column = percent_to_goal)  %>%
  
  # highlight blue if person reaches their goal
  tab_style(
    style = list(
      cell_fill(color = "lightcyan"),
      cell_text(weight = "bold")),
    locations = cells_body(
      columns = c(goal,score, percent_to_goal),
      rows = score >= goal
    )
  ) 

以下是上述代码的输出:请注意,条形图的长度并不总是反映它们所出现的行的值。相反,它们反映了原始数据集的顺序。 output with original code

编辑:删除

row_group_order
。如果我再次运行上面的代码,但注释掉旨在重新排列组的外观的行,则分组以不同的顺序显示(原始数据集中组的出现顺序),并且名称和前两列排序为这些组相应地进行,但条形图列仍然没有,并且保持数据集的原始顺序。下图:

Output without row_group_order

r dataframe gt gtextras
1个回答
1
投票

gtExtras
v 0.2.4 此错误已得到修复。感谢您的提出和伟大的
reprex

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

# make dataframe of individuals and their goals
df <- tribble(
  ~name, ~group, ~score, ~goal,
  "Bob", "C",   20,   40,
  "Chris",  "A", 50,   40,
  "Dale",  "B",  30,   50,
  "Jay",    "A", 0,   40,
  "Ben",   "B", 10,   20
  
) %>%
  # calculate percent towards goal, and cap at 100%
  mutate(percent_to_goal = score/goal *100,
    percent_to_goal = case_when(percent_to_goal >= 100 ~ 100,
      TRUE ~ percent_to_goal))


df %>%
  # make gt table
  gt(rowname_col = "name", groupname_col = "group") %>%
  
  # order groups
  row_group_order(groups = c("A","B","C")) %>%
  
  # add bar chart column
  gt_plt_bar_pct(column = percent_to_goal)  %>%
  
  # highlight blue if person reaches their goal
  tab_style(
    style = list(
      cell_fill(color = "lightcyan"),
      cell_text(weight = "bold")),
    locations = cells_body(
      columns = c(goal,score, percent_to_goal),
      rows = score >= goal
    )
  ) 

Screenshot of table with inline bar charts

© www.soinside.com 2019 - 2024. All rights reserved.