英文:
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_spec
与kable(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)
英文:
You can use cell_spec
with kable(escape = FALSE)
to predefine all cells' colors:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论