基于两列高亮重复行:无需手动指定颜色与行的实现方案
问题描述
我有一个数据集需要转换为表格,要求当ID与timepoint同时相同时,将对应的行组(成对或成组)用颜色高亮,且不同行组使用不同颜色。现有代码能实现功能,但需要手动指定颜色列表和行,无法适配不同数据集,求通用方法。
提供的数据框如下:
id_table <- structure(list(ID = c("168", "168", "168", "002", "002", "002", "002", "002", "002", "002 15/5/13", "002 15/5/13", "062", "062", "062+2 (11/02/14)", "062+2 (11/02/14)", "074", "074", "074", "074", "074", "074", "074", "074", "093", "093", "093", "093", "093", "093", "093", "093", "105", "105", "105", "105", "105", "105", "105", "105", "127", "127", "142", "142", "142", "142", "145", "145", "149", "149", "149", "149", "155", "155", "155", "155", "155", "155", "156", "156", "156", "156", "158", "158", "158", "158", "168", "168", "174", "174", "180", "180", "183", "183", "201", "201", "205", "205"), timepoint = c("1", "1", "1", "3", "3", "5", "5", "7", "7", NA, NA, "5", "5", "1", "1", "2", "2", "4", "4", "5", "5", "7", "7", "2", "2", "3", "3", "5", "5", "7", "7", "2", "2", "3", "3", "5", "5", "7", "7", "3", "3", "1", "1", "3", "3", "1", "1", "1", "1", "3", "3", "1", "1", "3", "3", "7", "7", "3", "3", "5", "5", "1", "1", "3", "3", "3", "3", "5", "5", "1", "1", "1", "1", "1", "1", "1", "1"), Plate = c("Plate_7", "Plate_3", "Plate_6", "Plate_1", "Plate_7", "Plate_1", "Plate_7", "Plate_7", "Plate_1", "Plate_1", "Plate_7", "Plate_1", "Plate_7", "Plate_7", "Plate_1", "Plate_7", "Plate_1", "Plate_7", "Plate_1", "Plate_7", "Plate_1", "Plate_7", "Plate_1", "Plate_7", "Plate_2", "Plate_7", "Plate_2", "Plate_7", "Plate_2", "Plate_7", "Plate_2", "Plate_2", "Plate_7", "Plate_7", "Plate_2", "Plate_7", "Plate_2", "Plate_3", "Plate_7", "Plate_6", "Plate_3", "Plate_6", "Plate_3", "Plate_6", "Plate_3", "Plate_3", "Plate_6", "Plate_3", "Plate_6", "Plate_6", "Plate_3", "Plate_3", "Plate_6", "Plate_6", "Plate_3", "Plate_7", "Plate_6", "Plate_4", "Plate_5", "Plate_4", "Plate_5", "Plate_3", "Plate_6", "Plate_6", "Plate_3", "Plate_6", "Plate_3", "Plate_4", "Plate_5", "Plate_3", "Plate_6", "Plate_3", "Plate_7", "Plate_7", "Plate_5", "Plate_5", "Plate_7"), `Year Run` = c(2022, 2020, 2022, 2020, 2022, 2020, 2022, 2022, 2020, 2020, 2022, 2020, 2022, 2022, 2020, 2022, 2020, 2022, 2020, 2022, 2020, 2022, 2020, 2022, 2020, 2022, 2020, 2022, 2020, 2022, 2020, 2020, 2022, 2022, 2020, 2022, 2020, 2020, 2022, 2022, 2020, 2022, 2020, 2022, 2020, 2020, 2022, 2020, 2022, 2022, 2020, 2020, 2022, 2022, 2020, 2022, 2022, 2022, 2022, 2022, 2022, 2020, 2022, 2022, 2020, 2022, 2020, 2022, 2022, 2020, 2022, 2020, 2022, 2022, 2022, 2022, 2022)), row.names = c(NA, -77L), class = c("tbl_df", "tbl", "data.frame"))
现有需手动配置的非通用代码:
color_scheme <- c("#DDDDDD", "#AAAAAA", "#777777", "#444444", "#111111") # Create a grouping variable based on ID and Timepoint group_var <- paste(id_table$ID, id_table$timepoint, sep = "_") # Create a color vector based on the grouping variable color_var <- as.numeric(factor(group_var)) # Generate the table and highlight rows based on the color vector id_table <- id_table %>% select(-ID) %>% kbl() %>% row_spec(which(color_var == 1), background = color_scheme[1]) %>% row_spec(which(color_var == 2), background = color_scheme[2]) %>% row_spec(which(color_var == 3), background = color_scheme[3]) %>% row_spec(which(color_var == 4), background = color_scheme[4]) %>% row_spec(which(color_var == 5), background = color_scheme[5]) # Print the table id_table
通用解决方案
核心逻辑是自动处理分组、配色和行样式,无需手动指定每个分组的参数:
library(knitr) library(kableExtra) library(dplyr) # 1. 生成分组键,处理timepoint中的NA值 id_table <- id_table %>% mutate(group_key = paste(ID, coalesce(timepoint, "NA"), sep = "_")) # 2. 获取唯一分组并自动生成对应数量的颜色 unique_groups <- unique(id_table$group_key) num_groups <- length(unique_groups) # 可选配色方案:灰色渐变、彩虹色、viridis调色板等 color_scheme <- gray.colors(num_groups, start = 0.9, end = 0.3) # color_scheme <- rainbow(num_groups) # color_scheme <- viridis::viridis(num_groups) # 3. 创建初始表格 table_output <- id_table %>% select(-ID, -group_key) %>% kbl() # 4. 循环批量设置行背景色 for (i in seq_along(unique_groups)) { target_rows <- which(id_table$group_key == unique_groups[i]) table_output <- table_output %>% row_spec(target_rows, background = color_scheme[i]) } # 输出最终表格 table_output
关键优化点
- 自动分组:用
coalesce将timepoint的NA转为字符串"NA",确保缺失值也能被正确分组 - 动态配色:根据数据集的唯一分组数量自动生成颜色列表,支持任意大小的数据集
- 批量处理:通过循环自动为每个分组应用行样式,避免手动重复编写
row_spec调用
内容的提问来源于stack exchange,提问作者Gabriella
相关产品推荐
相关产品推荐

