R:如何显示百分比值的热图类型表示。与SC中显示的相同。
在热图表/图中应显示除总(%)]以外的所有列并且在<= 5到<= 25情人百分比或零的列中,绿色显示,而更多的值显示在红色中。
0或早期(%)
列不应在热图上突出显示。检查附加的excel屏幕截图。无法理解如何将这种类型的excel转换为R。在表中列下方显示的数据库中。
User 0 or early(%) <=5(%) <=10(%) <=15(%) <=20(%) <=25(%) TOTAL (%)
A 57 15 18 5 5 0 100
B 64 22 12 2 0 0 100
C 73 12 10 3 2 0 100
D 45 37 7 4 3 5 100
E 87 4 2 2 1 4 100
F 44 39 3 0 1 13 100
G 84 7 2 5 2 0 100
H 90 3 0 7 0 0 100
I 88 2 2 7 2 0 100
J 43 17 0 34 6 0 100
K 69 4 2 20 2 2 100
L 37 5 5 0 5 49 100
M 69 18 0 10 3 0 100
N 59 8 3 30 0 0 100
O 91 6 3 0 0 0 100
P 50 7 10 27 3 3 100
Q 40 23 7 13 10 7 100
R:如何显示百分比值的热图类型表示。与SC中显示的相同。在热图表/图中应显示除Total(%)以外的所有列,并在<= 5到<= ...]的列中显示
formattable
包而不是ggplot2
。 formattable
允许使数据框呈现为具有应用格式器功能的HTML表,这类似于Microsoft Excel(https://cran.r-project.org/web/packages/formattable/vignettes/formattable-data-frame.html)中的条件格式。library(formattable)
color_tile2 <- function (...) {
formatter("span", style = function(x) {
style(display = "block",
padding = "0 4px",
`border-radius` = "4px",
`background-color` = ifelse(x ==0, "green", csscolor(matrix(as.integer(colorRamp(...)(normalize(as.numeric(x)))),
byrow=TRUE, dimnames=list(c("red","green","blue"), NULL), nrow=3))))
},
x ~ percent(x/100))}
在这里,将下面的函数应用于数据框,并使特定的列变为彩色,而其他不变为:
library(formattable)
formattable(df, align = "c", list(
area(col = `<=5(%)`:`<=25(%)`) ~color_tile2(c("yellow","orange","red")),
User = FALSE,
`TOTAL_(%)` = FALSE,
`0_or_early(%)` = formatter("span", style = ~style(color = "darkgreen"), x ~ percent(x/100)))
)
看起来您想要得到什么?
可复制的示例
structure(list(User = c("A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q"), `0_or_early(%)` = c(57L,
64L, 73L, 45L, 87L, 44L, 84L, 90L, 88L, 43L, 69L, 37L, 69L, 59L,
91L, 50L, 40L), `<=5(%)` = c(15L, 22L, 12L, 37L, 4L, 39L, 7L,
3L, 2L, 17L, 4L, 5L, 18L, 8L, 6L, 7L, 23L), `<=10(%)` = c(18L,
12L, 10L, 7L, 2L, 3L, 2L, 0L, 2L, 0L, 2L, 5L, 0L, 3L, 3L, 10L,
7L), `<=15(%)` = c(5L, 2L, 3L, 4L, 2L, 0L, 5L, 7L, 7L, 34L, 20L,
0L, 10L, 30L, 0L, 27L, 13L), `<=20(%)` = c(5L, 0L, 2L, 3L, 1L,
1L, 2L, 0L, 2L, 6L, 2L, 5L, 3L, 0L, 0L, 3L, 10L), `<=25(%)` = c(0L,
0L, 0L, 5L, 4L, 13L, 0L, 0L, 0L, 0L, 2L, 49L, 0L, 0L, 0L, 3L,
7L), `TOTAL_(%)` = c(100L, 100L, 100L, 100L, 100L, 100L, 100L,
100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L, 100L)), row.names = c(NA,
-17L), class = c("data.table", "data.frame"))