在R中的表格中超过阈值的颜色值

huangapple go评论76阅读模式
英文:

Color values beyond a threshold in a table in R

问题

如何只为表格中大于5的数值(而不是单元格)着色?

英文:

If I have the following table for the mtcars dataset:

knitr::kable(mtcars) %>%  
   kableExtra::kable_styling(c("bordered","condensed"), font_size = 15,full_width = T)

How can I color only the values (not cells) greater than, for example, 5 on that table?

Thanks in advance!

答案1

得分: 1

你可以使用cell_speckable(escape = FALSE)来预定义所有单元格的颜色:

library(dplyr)
library(knitr)
library(kableExtra)

df <- mtcars
df %>%
  mutate(across(everything(), ~ cell_spec(.x, color = ifelse(.x > 5, "red", "black")))) %>%
  kable(escape = FALSE) %>%
  kable_styling(c("bordered","condensed"), font_size = 15, full_width = T)

在R中的表格中超过阈值的颜色值

英文:

You can use cell_spec with kable(escape = FALSE) to predefine all cells' colors:

library(dplyr)
library(knitr)
library(kableExtra)

df &lt;- mtcars
df %&gt;% 
  mutate(across(everything(), ~ cell_spec(.x, color = ifelse(.x &gt; 5, &quot;red&quot;, &quot;black&quot;)))) %&gt;% 
  kable(escape = FALSE) %&gt;%  
  kable_styling(c(&quot;bordered&quot;,&quot;condensed&quot;), font_size = 15, full_width = T)

在R中的表格中超过阈值的颜色值

huangapple
  • 本文由 发表于 2023年7月3日 16:49:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定