【问题标题】:How to change color gradient in formattable?如何更改格式化表中的颜色渐变?
【发布时间】:2021-03-16 03:26:34
【问题描述】:

我正在创建以下格式表:

我已经在 Var1 上设置了渐变,因此值越高,它越红。我想知道是否可以更改它,使其值越高越绿。我喜欢它的一切,我只希望渐变从黑色到绿色,而不是从黑色到红色。我曾尝试调整background-color 命令中的数字,但似乎无法正确处理。有可能解决这个问题吗?是否有我可能缺少的关于数字颜色渐变的指南?

library(formattable)

ex <- structure(list(Group = 1:3, Var1 = c(0, 0.4, 0.8)), class = "data.frame", row.names = c(NA, 
-3L))

ex <- formattable(ex, 
                  align = c("l","l", "c", "c", "c", "c"),
                  list(Var1 = formatter("span",
                                                  style = x ~ style(display = "block",
                                                                    "border-radius" = "4px",
                                                                    "padding-right" = "4px",
                                                                    color = "white",
                                                                    "background-color" = rgb(ex$Var1/max(ex$Var1), 0, 0, 1)))))

【问题讨论】:

    标签: r dataframe formattable


    【解决方案1】:

    您可以使用csscolor(gradient()) 定义渐变颜色。 - 这也是formattablecolor_tile函数中用到的函数。

    ex <- formattable(ex, 
                      align = c("l","l", "c", "c", "c", "c"),
                      list(Var1 = formatter("span",
                                            style = x ~ style(display = "block",
                                                              "border-radius" = "4px",
                                                              "padding-right" = "4px",
                                                              color = "white",
                                                              "background-color" = csscolor(gradient(as.numeric(x), "black", "green"))))))
    
    ex
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-15
      • 2023-04-06
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-17
      相关资源
      最近更新 更多