在color_tile函数中是否可以使用2种以上的颜色?

6l7fqoea  于 2023-03-05  发布在  其他
关注(0)|答案(5)|浏览(111)

我有一个dataframe列,目前正在使用formattable::color_tile函数格式化(如下所示):
color_tile( "red", "springgreen" )
我的问题是中间的值是一个难看的棕色,我希望它是一个红色-琥珀色-绿色的渐变,但是color_tile似乎只能接受min.color和max.color参数--在R中,是否可以使用第三种颜色,或者使用类似的格式化函数?

vh0rcniy

vh0rcniy1#

这个函数看起来不像是设计来处理两种以上颜色的,但是你可以在这个模板上制作你自己的建筑。

color_tile2 <- function (...) {
  formatter("span", style = function(x) {
    style(display = "block",
          padding = "0 4px", 
          `border-radius` = "4px", 
          `background-color` = csscolor(matrix(as.integer(colorRamp(...)(normalize(as.numeric(x)))), 
                                               byrow=TRUE, dimnames=list(c("red","green","blue"), NULL), nrow=3)))
  })}

可以像这样使用

formattable(mtcars, list(mpg = color_tile2(c("white", "pink"))))
formattable(mtcars, list(mpg = color_tile2(c("blue", "green", "pink"))))
7ivaypg9

7ivaypg92#

确定颜色1和2之间的行号以及颜色2和3之间的行号。然后调用color_tile两次。

formattable(x, 
        list(
          area(col = 2, row = c(1,3,5,7,8,9,10,13,14,15)) ~ color_tile("red", "white"),
          area(col = 2, row = c(2,4,6,11,12,16)) ~ color_tile("white","green")
        ))

不会完美地修复它,因为它不会保持两侧颜色的相对强度

v8wbuo2f

v8wbuo2f3#

在一个名为“entry of the github”formattable的网站上,我发现了这个看起来很有用的东西,它解决了我的问题,让一个列的颜色从负到正连续编码为红色到绿色,中间没有“棕色”(这将提供“透明”):

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)

这是相关链接:https://github.com/renkun-ken/formattable/issues/102#issuecomment-408649019

wpcxdonn

wpcxdonn4#

基于@cmilando,我重写了函数,使颜色更好地反映负数和正数,尽管有点手动:D

library(tidyverse)
library(RColorBrewer)
library(formattable)
library(kableExtra)
library(purrr)

# --------------------
# brewer.pal(10,"RdYlGn")

my_color_tile <- function() {
  
  return_col <- function(y) 
    map_chr(y,function(x) case_when(x > 80  ~ "#006837",
              x > 60  ~ "#1A9850",
              x > 40  ~ "#66BD63",
              x > 20  ~ "#A6D96A",
              x >= 0  ~ "#D9EF8B",
              x >= -20  ~ "#FEE08B",
              x >= -40  ~ "#FDAE61",
              x >= -60  ~ "#F46D43",
              x >= -80  ~ "#D73027",
              x >= -100  ~ "#A50026"
              ))
  
  formatter("span", 
            style = function(y) style(
              display = "block",
              padding = "0 4px",
              "border-radius" = "4px",
              "color" = ifelse( return_col(y) %in% c("#A50026","#D73027","#F46D43","#006837","#1A9850","#66BD63"),
                                csscolor("white"), csscolor("black")),
              "background-color" = return_col(y)
            )
  )
}

# --------------------
data.frame(value = c(seq(-100,100,10))) %>% 
  arrange(desc(value)) %>%  
  formattable(., list(
    area(col = 1) ~ my_color_tile()))
wdebmtf2

wdebmtf25#

利用RColorBrewer,类似这样的方法可能会奏效

color_tile3 <- function(fun = "comma", digits = 0, palette = 'RdBu', n = 9) {
  fun <- match.fun(fun)

  stopifnot(n >= 5)
  
  return_cut <- function(y) 
    cut(y, breaks = quantile(y, probs = 0:n/n, na.rm = T),
        labels = 1:n, ordered_result = T, include.lowest = T)
  
  return_col <- function(y) 
      RColorBrewer::brewer.pal(n, palette)[as.integer(return_cut(y))]
  
  formatter("span", x ~ fun(x, digits = digits),
            style = function(y) style(
              display = "block",
              padding = "0 4px",
              "border-radius" = "4px",
              "color" = ifelse( return_cut(y) %in% c(1, 2, n-1, n),
                                csscolor("white"), csscolor("black")),
              "background-color" = return_col(y)
            )
  )
}

使用案例:

library(tidyverse)
library(RColorBrewer)

mtcars[, 1:5] %>%
  corrr::correlate() %>%
  formattable(., list(
    `rowname` = formatter("span", style = ~ style(color = "grey", 
                                                  font.weight = "bold")), 
    area(col = 2:6) ~ color_tile3(digits = 2)))

输出:
下面是指向输出mtcars_color3的链接,如下所示:

相关问题