Replace categorical values with traffic light colors

前端 未结 1 1410
一生所求
一生所求 2021-01-24 08:58

I have some categorical values I want to show in a table as red, yellow and green spots in R or r-markdown.

Is there a way to do this? maybe an icon package that will l

相关标签:
1条回答
  • 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") 
    

    0 讨论(0)
提交回复
热议问题