我目前有一個可格式化的表格,如下面的例子:
我有一個可格式化的表格。
library(data.table)
library(dplyr)
library(formattable)
library(tidyr)
customGreen0 = "#DeF7E9"
customGreen = "#71CA97"/span>
customRed = "#ff7f7f"/span>
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 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)。
`改進` = 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", "r", "c", "l", "r"), list(
`指標名稱`=formatter("span", style = ~ style(color = "gray",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)
))
擁有此表:
我想做的是,但我還沒有找到方法。 正如你所看到的,平均列使用的是一種紅色....
。我怎樣才能將平均列的條形顏色僅在第2行(煙草使用流行率)或第4行(糖尿病流行率)中改為藍色?
換句話說,如果我在第1行和第3行,我希望平均數欄是紅色的,如果我在第2行和第4行,則是藍色的。就像下面這樣:
謝謝!
uj5u.com熱心網友回復:
你可以為Average列添加一個矢量來設定顏色:
formattable(i1, 對齊=c("l"。 "c","c",。 "r", "c", "l"。 "l", "r")。 list()
`指標名稱` = formatter("span"。 樣式= ~樣式(color = "grey", 字體。 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"))
))
uj5u.com熱心網友回復:
我們可以在area中使用row/col
formattable(i1, 對齊=c("l"。 "c","c",。 "r", "c", "l"。 "l", "r")。 list()
`指標名稱` = formatter("span"。 樣式= ~樣式(color = "grey", 字體。 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")
))
輸出
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/306881.html
標籤:
上一篇:<p>我正在使用R編程語言。假設有一家酒店有一個客戶串列,其中有他們的入住和退房時間(注:日期的實際值是"POSIXct",寫成"年月日"。):&l



