3

我有一个数据框列,我目前正在使用该formattable::color_tile函数进行格式化(如下):

color_tile( "red", "springgreen" )

我的问题是中间附近的值是丑陋的棕色,我希望它是红色-琥珀色-绿色渐变,但 color_tile 似乎只能采用 min.color 和 max.color参数 - 是否可以在 R 中使用具有此或类似格式功能的第三种颜色?

4

5 回答 5

8

看起来该功能并非旨在处理两种以上的颜色,但您可以在该模板上制作自己的建筑。

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"))))
于 2018-04-17T20:54:42.637 回答
3

Determine which row numbers you want between color 1 and 2 and which row numbers for between color 2 and 3. Then call color_tile twice. For example

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")
        ))

Won't fix it perfectly, since it won't keep the relative intensity of the colors on either side

于 2019-04-07T21:05:52.447 回答
1

基于@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()))

于 2020-09-19T09:10:36.640 回答
1

在 github 格式化站点的一个问题条目中,我发现这似乎很有用并解决了我的问题,即在从负到正的连续刻度上将列颜色编码为红色到绿色,中间没有“棕色”(这将提供“透明”):

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

于 2019-02-18T13:53:22.957 回答
0

像这样的东西可能会起作用,利用 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

我还不能嵌入,但这是输出的链接

于 2020-04-23T17:39:55.107 回答