【问题标题】:R: how to display a table with a heat map-type representation of percentage valuesR:如何显示具有百分比值的热图类型表示的表格
【发布时间】:2020-03-04 04:18:01
【问题描述】:

R:如何使用热图类型的百分比值表示形式显示表格,如在 Excel 中。与 SC 中显示的相同。

在热图表/图中,我希望除 Total(%) 之外的所有列都具有条件​​格式,以便较低的值显示为绿色,而较高的值显示为绿色值以红色显示。

0 或 early(%) 列不应在热图中突出显示。 检查附加的 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

【问题讨论】:

  • 您是否希望获得与 Excel 在 ggplot2plotly 中显示的软管相同的热图?你试过什么代码?
  • @dc37 我试图从 excel 中提供的行数据中开发它以便理解现在我们在 sql 中获取数据,这里所有数据都是分开的,而在 excel 中只有两列,所以我使用了 @ 987654326@ 在 Excel 中,现在 SQL 中只有 days_bin 的列,按单独的 bin 百分比获取列。
  • 抱歉,您的数据是什么样子的并不是很清楚。您是否尝试将数据导入 R ?它们长什么样?可以发dput(NameOfYourDataframe)的回复吗?

标签: r ggplot2 plotly


【解决方案1】:

如果您想重现与使用 excel 获得的“热图”相同的“热图”,我宁愿考虑使用 formattable 包而不是 ggplot2formattable 允许将数据帧呈现为应用了格式化函数的 HTML 表格,这类似于 Microsoft Excel 中的条件格式化 (https://cran.r-project.org/web/packages/formattable/vignettes/formattable-data-frame.html)。

我从@MrFlick 对这篇文章的回答中获得启发:Is it possible to use more than 2 colors in the color_tile function? 构建以下答案。

首先,我们正在创建一个函数,该函数将为热图制作颜色模式。根据您的 excel 输出,0% 的值是绿色的,然后您有一个从黄色到橙色到红色的渐变。

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

【讨论】:

  • 是的,这和预想的一模一样,你是天才。我完全困惑如何做到这一点,非常感谢您的帮助和解释。
  • 不客气 ;) 很高兴它能让你满意,即使它没有使用 ggplot2
猜你喜欢
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多