我正在制作一个 gt
表格,显示个人实现目标的进度。表中的一行显示了实现该目标的进度的水平条形图(如果目标为 50,得分为 40,则条形图位于 80%)。
但是,当我使用 groupname_col
参数更改 gt 行的顺序时,其他单元格的顺序会发生变化,但 gtExtras
的顺序不会发生变化 gt_plt_bar_pct
列,因此它显示该行中的名称和分数的错误栏,相反,该列似乎始终按照输入数据中的行顺序表示。
我知道我可以在 gt
开始之前在 df 上使用 arrange
来解决这个问题,但这似乎不是一个好的解决方案,因为我想要更改行的顺序以按不同组查看。这是 gtExtras
的缺陷吗?有更好的解决办法吗?
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 %>%
# 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
)
)
这是上述代码的输出:请注意,条形图的长度并不总是反映它们所出现的行的值。相反,它们反映了原始数据集的顺序。
编辑:删除row_group_order
。如果我再次运行上面的代码,但注释掉旨在重新排列组的外观的行,则分组以不同的顺序显示(原始数据集中组的出现顺序),并且名称和前两列排序为这些组相应地进行,但条形图列仍然没有,并且保持数据集的原始顺序。下图:
I am making a gt
table showing the progress of individuals towards a goal. In the table, there is a row showing a horizontal bar graph of progress towards that goal (if goal is 50 and score is 40, the bar is at 80%).
However, when I change the order of the gt rows by using the groupname_col
argument, the order of the other cells changes, but not the order of the gtExtras
gt_plt_bar_pct
column, so it's showing the wrong bars for the name and score in that row, instead, that column seems to always be represented in the order of rows in the input data.
I understand that I can fix this by using arrange
on the df before the gt
begins, but this doesn't seem like a good solution since I'm going to want to change the order of the rows to view by different groups. Is this a flaw with gtExtras
? is there a better fix?
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 %>%
# 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
)
)
Here is the output from the above code: notice that the length of the bar charts do not always reflect the values of the rows they are appearing in. Instead, they reflect the order of the original dataset.
EDIT: remove row_group_order
. If I run the above code again, but comment out the line meant to rearrange the appearance of groups, the grouping shows up in a different order (order of appearance of groups in the original dataset), and the name and first two columns sort into these groups accordingly, but the bar chart column still does not, and remains in the original order of the dataset. Image below:
发布评论
评论(1)
在
gtExtras
v 0.2.4 中,此错误已得到修复。感谢您的提出和伟大的reprex
!Per
gtExtras
v 0.2.4 this bug has been fixed. Thanks for raising and the greatreprex
!