【问题标题】:Change color_bar color based on odd or even rows, R table根据奇数行或偶数行更改color_bar颜色,R表
【发布时间】:2021-11-10 18:30:47
【问题描述】:

我目前有一个可格式化的表格,例如以下示例:

library(data.table)
library(dplyr)
library(formattable)
library(tidyr)
customGreen0 = "#DeF7E9"
customGreen = "#71CA97"
customRed = "#ff7f7f"

austinData= fread('https://raw.githubusercontent.com/lgellis/MiscTutorial/master/Austin/Imagine_Austin_Indicators.csv', data.table=FALSE, header = TRUE, stringsAsFactors = FALSE)
attach(austinData)

i1 <- austinData %>%
  filter(`Indicator Name` %in% 
           c('Prevalence of Obesity', 'Prevalence of Tobacco Use', 
             'Prevalence of Cardiovascular Disease', 'Prevalence of Diabetes')) %>%
  select(c(`Indicator Name`, `2011`, `2012`, `2013`, `2014`, `2015`, `2016`)) %>%
  mutate (Average = round(rowMeans(
    cbind(`2011`, `2012`, `2013`, `2014`, `2015`, `2016`), na.rm=T),2), 
    `Improvement` = round((`2011`-`2016`)/`2011`*100,2))
i1

color_bar3 <- function (color = "#49CA69", fun = "proportion", ...)
{
  fun <- match.fun(fun)
  formatter("span", style = function(x) style(display = "inline-block",
                                              `border-radius` = "5px", `padding-left` = "3px",
                                              `background-color` = csscolor(color),
                                              width = percent(fun(as.numeric(gsub(",", "", x)), ...))))
}


formattable(i1, align =c("l","c","c","c","r", "c", "l", "l", "r"), list(
  `Indicator Name` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")), 
  `2011`= color_tile(customGreen, customGreen0),
  `2012`= color_tile(customGreen, customGreen0),
  `2013`= color_tile(customGreen, customGreen0),
  `2014`= color_bar(customRed),
  `2015`= color_tile(customGreen, customGreen0),
  `2016`= color_bar(customGreen),
  `Average` = color_bar3(customRed)
))

拥有这张桌子:

我想做但我没有找到方法是: 如您所见,Average 列使用偏红的颜色....

只有当我位于第 2 行(烟草使用的流行率)或第 4 行(糖尿病的流行率)时,如何才能将“平均值”列的条形颜色更改为蓝色?

换句话说,如果我在第 1 行和第 3 行,我希望平均条为红色,如果我在第 2 和第 4 行,我希望为蓝色。如下所示:

谢谢!

【问题讨论】:

    标签: r r-markdown kable formattable


    【解决方案1】:

    我们可以在area 中使用row/col

    formattable(i1, align =c("l","c","c","c","r", "c", "l", "l", "r"), list(
      `Indicator Name` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")), 
      `2011`= color_tile(customGreen, customGreen0),
      `2012`= color_tile(customGreen, customGreen0),
      `2013`= color_tile(customGreen, customGreen0),
      `2014`= color_bar(customRed),
      `2015`= color_tile(customGreen, customGreen0),
      `2016`= color_bar(customGreen),
      area(row = c(1, 3), col = `Average`) ~ color_bar3(customRed),
      area(row = c(2, 4), col = `Average`) ~ color_bar3("lightblue")
    ))
    

    -输出

    【讨论】:

      【解决方案2】:

      您可以为Average 列添加一个向量来设置颜色:

      formattable(i1, align =c("l","c","c","c","r", "c", "l", "l", "r"), list(
      `Indicator Name` = formatter("span", style = ~ style(color = "grey",font.weight = "bold")), 
      `2011`= color_tile(customGreen, customGreen0),
      `2012`= color_tile(customGreen, customGreen0),
      `2013`= color_tile(customGreen, customGreen0),
      `2014`= color_bar(customRed),
      `2015`= color_tile(customGreen, customGreen0),
      `2016`= color_bar(customGreen),
      `Average` = color_bar3(c(customRed,"blue",customRed,"blue"))
      ))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-31
        • 2013-08-21
        • 1970-01-01
        • 2011-06-17
        • 2014-03-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多