将单元格颜色映射到kableExtra中的数据值以创建热图表 [英] Map cell colors to data values in kableExtra to create a heatmap table

查看:181
本文介绍了将单元格颜色映射到kableExtra中的数据值以创建热图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一张桌子,想应用ROW级的热图.

I have a table below and would like to apply ROW-level heat maps.

(1)知道如何吗?现在,下面一行中的两个值的热图都相同.

(1) Any idea how? Right now the heat map is the same for both values in the single row below.

(2)有没有办法使组列的标题不成90度角?现在,所有标题都是有角度的,但对于group列,最好不使用angle = 90.

(2) Is there a way to make the header for the group column NOT be angled 90 degrees? Right now all headers are angled but for the group column it be better without angle=90.

这是rmd文件.

---
title: "Untitled"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)


library(dplyr)
library(kableExtra)
#d = data.frame(group= c("A","b"),cat1=c(2,50),cat2=c(100,2))
d = data.frame(group= c("A"),cat1=c(2),cat2=c(NA))

d = d %>%
    mutate_if(is.numeric, function(x) {
  cell_spec(x, "latex", bold = F, background = spec_color(x,option="C", begin=.5, end = 0.99))
}) 

```



```{r table , echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE, fig.height=3, fig.width = 8} 

kable(
      d, format ="latex",
      caption = "",
      booktabs = T, 
      longtable = T,
      escape = F ,
      align = "c"
      ) %>% kable_styling(latex_options = c(
        "striped", 
        "repeat_header"
        )
       )%>% row_spec( 0,angle = 90)


```

注意:理想情况下,最好使用kableExtra功能来完成此操作,以便它们的配色方案与其他kableExtra表完全匹配.

Note: Ideally it'd be good to have this done with the kableExtra functionality so they color schemes match exactley to other kableExtra tables.

推荐答案

我实际上不确定如何从spec_color获取所需的颜色映射.另外,在下面的代码中,我使用colorRamp函数和调色板生成函数生成了颜色映射,可以根据需要对其进行调整.该代码还通过将缺失的颜色涂成浅灰色来处理缺失的值.

I'm actually not sure how to get the desired color mapping from spec_color. As an alternative, in the code below I've generated the color mapping using the colorRamp function and a palette-generating function, which can be adjusted as desired. The code also deals with missing values by coloring them light gray.

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(viridis)
library(dplyr)
library(kableExtra)

set.seed(2)
d = data_frame(group = sample(LETTERS[1:5], 10, replace=TRUE), cat1=runif(10, 0, 100), cat2=runif(10, 0, 100))
d[5,2] = NA

max.val = max(d[ , sapply(d, is.numeric)], na.rm=TRUE)

#pal.fnc = colorRamp(viridis_pal(option="C")(2))
pal.fnc = colorRamp(c("red", "yellow", "green"))

d = d %>%
    mutate_if(is.numeric, function(x) {
  cell_spec(round(x,1), "latex", bold = F, color=grey(.3),
            background = rgb(pal.fnc(x/max.val) %>% replace(., is.na(.), 200), maxColorValue=255))
}) 

```

```{r table , echo= FALSE, comment = FALSE, message= FALSE, warning = FALSE, fig.height=3, fig.width = 8} 
kable(
      d, format ="latex",
      linesep="",
      caption = "",
      booktabs = T, 
      longtable = T,
      escape = F ,
      align = "c"
      ) %>% kable_styling(latex_options = c(
        "striped", 
        "repeat_header"
        )
       )
```

这篇关于将单元格颜色映射到kableExtra中的数据值以创建热图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆