表焦点 - 根据多个列的价值更改文本颜色

发布于 2025-01-31 23:56:22 字数 1190 浏览 3 评论 0原文

我想复制下表,在Tableau中称为 spotlighting

”“在此处输入图像说明”

在我的情况下,我想用以下基础复制它,使我对每行的最大值,这是我有一个想法的问题使用cell_spec()kableExtra package

library(knitr)
library(kableExtra)


Name<-c("question1",  "question",  "question3",  "question4", 
        "question5",  "question6",  "question7",  "question8", 
        "question9",  "question10")
A<-c(0, 3 ,0 ,1, 0, 0, 0, 0, 2, 0)
B<-c(5, 0, 1, 0, 3, 0, 3, 1, 0, 1)
C<-c(3, 0, 2 ,2 ,0 ,1, 0 ,1 ,0 ,2)
D<-c(4, 1, 3 ,2 ,0 ,5, 0 ,1 ,3 ,2)

tab<-data.frame("Name"=Name,"A"=A,"B"=B,"C"=C,"D"=D)

tab%>%
  kbl() %>%
  kable_paper("striped",full_width = F)

“在此处输入映像”

请记住,我想获得具有类似格式的表仅显示表中最多的数字

I would like to replicate the following table that in tableau is called Spotlighting

enter image description here

In my case I would like to replicate it with the following base that gives me color to the maximum value per row that are the questions I have an idea to do it with cell_spec() from the kableExtra package

library(knitr)
library(kableExtra)


Name<-c("question1",  "question",  "question3",  "question4", 
        "question5",  "question6",  "question7",  "question8", 
        "question9",  "question10")
A<-c(0, 3 ,0 ,1, 0, 0, 0, 0, 2, 0)
B<-c(5, 0, 1, 0, 3, 0, 3, 1, 0, 1)
C<-c(3, 0, 2 ,2 ,0 ,1, 0 ,1 ,0 ,2)
D<-c(4, 1, 3 ,2 ,0 ,5, 0 ,1 ,3 ,2)

tab<-data.frame("Name"=Name,"A"=A,"B"=B,"C"=C,"D"=D)

tab%>%
  kbl() %>%
  kable_paper("striped",full_width = F)

enter image description here

Remember that I want to get a table with a similar format only that now I will only show the largest number in the table

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃安静 2025-02-07 23:56:22

通过数字列循环并根据值添加颜色(根据需要更改 ifelse 语句):

tab %>%
  mutate_if(is.numeric, 
            function(i){
              cell_spec(i, color = ifelse(i > 1, "green", "red"))}) %>% 
  kbl(escape = FALSE) %>%
  kable_paper("striped", full_width = FALSE)


“ nofollow noreferrer”>

# transpose, get colour, transpose
tmp <- data.frame(t(
  data.frame(t(tab[ -1 ])) %>% 
  mutate_all(function(i) cell_spec(i, color = ifelse(i == max(i), "green", "red")))
  ), row.names = NULL)

# keep 1st name column, add other formatted columns, and kable
cbind(tab[ 1 ], tmp) %>%
  kbl(escape = FALSE) %>%
  kable_paper("striped", full_width = FALSE)

“在此处输入映像”

Loop through the numeric columns and add a colour based on the value (change the ifelse statement as needed):

tab %>%
  mutate_if(is.numeric, 
            function(i){
              cell_spec(i, color = ifelse(i > 1, "green", "red"))}) %>% 
  kbl(escape = FALSE) %>%
  kable_paper("striped", full_width = FALSE)

enter image description here


Edit:

To do the same per row, we can transpose, then as above loop through columns and change the colour based on value, then transpose it back again:

# transpose, get colour, transpose
tmp <- data.frame(t(
  data.frame(t(tab[ -1 ])) %>% 
  mutate_all(function(i) cell_spec(i, color = ifelse(i == max(i), "green", "red")))
  ), row.names = NULL)

# keep 1st name column, add other formatted columns, and kable
cbind(tab[ 1 ], tmp) %>%
  kbl(escape = FALSE) %>%
  kable_paper("striped", full_width = FALSE)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文