Is it possible to use more than 2 colors in the color_tile function?

前端 未结 5 935
青春惊慌失措
青春惊慌失措 2020-12-12 01:29

I have a dataframe column that I\'m currently formatting using the formattable::color_tile function (below):

color_tile( \"red\", \"springgreen\"

5条回答
  •  长情又很酷
    2020-12-12 01:57

    In an issue' entry of the github' formattable site, I found this which seems useful and solved my problem to have a column color coded on continuous scale from negative to positive as red to green, without having the "brown" in the middle (this will deliver "transparent"):

    library(dplyr)
    library(kableExtra)
    library(formattable)
    
    x = currency(c(1000000,
                     -3000,
                    400000,
                    800000,
                     -1700,
                         0,
                     50000))
    
    x = ifelse(
      x <= 0.0, 
      color_tile("red", "transparent")(x*c(x<=0)),
      color_tile("transparent", "green")(x*c(x>=0)))
    
    x %>% 
      kable(escape = F) %>% 
      kable_styling(bootstrap_options = c("striped", "hover"), 
                    full_width = F)
    

    This is the relevant link: https://github.com/renkun-ken/formattable/issues/102#issuecomment-408649019

提交回复
热议问题