用交通灯颜色替换分类值 [英] Replace categorical values with traffic light colors
本文介绍了用交通灯颜色替换分类值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在表格中以R或r-markdown中的红色,黄色和绿色斑点显示一些分类值.
I have some categorical values I want to show in a table as red, yellow and green spots in R or r-markdown.
有没有办法做到这一点?也许是一个图标包,它将让我为某个图标添加gsub文本?我搜寻了互联网,空手而归.任何想法表示赞赏.
Is there a way to do this? maybe an icon package that will let me gsub text for an Icon? I have scoured the internet and come up empty handed. Any thoughts are appreciated.
library(kableExtra)
dt <- mtcars[1:5, 1:6]
kable(dt) %>%
kable_styling(full_width = F, font_size = 12) %>%
row_spec(0, angle = -90)%>%
row_spec(1:5, bold = F, color = "black")
推荐答案
以下是在knitr中使用html格式的示例.它使用cell_spec
函数使用图标和颜色来创建新列.
Here's an example that uses html format in knitr. It uses the cell_spec
function to make a new column using an icon and colour.
library(kableExtra)
library(emojifont)
library(dplyr)
load.fontawesome()
options(knitr.table.format = 'html')
cylcols = function(cyl) {
case_when(
cyl == 4 ~ 'green',
cyl == 6 ~ 'yellow',
cyl == 8 ~ 'red'
)
}
dt <-
mtcars[1:5, 1:6] %>%
mutate(symbol = fontawesome('fa-circle')) %>%
mutate(symbol = cell_spec(symbol, 'html', color=cylcols(cyl))) %>%
select(mpg, cyl=symbol, disp, hp, drat, wt)
kable(dt, escape = F) %>%
kable_styling(full_width = F, font_size = 12) %>%
row_spec(0, angle = -90) %>%
row_spec(1:5, bold = F, color = "black")
这篇关于用交通灯颜色替换分类值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文