【问题标题】:Is there a convenient way to make a heatmap-style table in knitr?有没有一种方便的方法可以在 knitr 中制作热图样式的表格?
【发布时间】:2016-05-16 22:56:45
【问题描述】:

我希望能够在 knitr 中生成一个表格,其中单元格背景颜色将根据映射到单元格中的值的颜色渐变来设置。

例如,请参阅towards the bottom of this page

Pander 似乎能够进行某些条件突出显示,as described in this question,但我找不到允许对单元格背景进行条件格式设置的选项。

【问题讨论】:

  • pander 无法帮助为单元格着色,因为pandoc 的降价不支持,但如果您想要例如 HTML 输出并且不需要生成其他文档格式,我热烈建议尝试github.com/renkun-ken/formattable

标签: r knitr


【解决方案1】:

例如查看that page底部的年龄列

【讨论】:

  • 当您引用的格式化表格的代码包含在 .Rnw 脚本(knitr 和 LaTeX)的块中时,在调用 library(formattable) 后,结果中会出现错误消息pdf 文件:“eval 中的错误(expr、envir、enclos):找不到可格式化的函数。”该软件包是否适用于 .Rnw 和例如 xelatex?
  • @Dontas,这似乎是一个损坏的链接。
【解决方案2】:

我过去在 LaTeX 中实现了这一点,只需解析要传递给 xtable 的数据帧,然后评估表条目并添加相应的 LaTeX 颜色标记。例如,我的 LaTeX 序言包括:

\usepackage{xcolor,colortbl}
\definecolor{darkblue}{rgb}{0.0,0.0,0.3}
\definecolor{blue0}{HTML}{E5E5FE}
\definecolor{blue1}{HTML}{CBCBFE}
\definecolor{blue2}{HTML}{B2B2FE}
\definecolor{blue3}{HTML}{9898FE}
\definecolor{blue4}{HTML}{7F7FFE}
\definecolor{blue5}{HTML}{6666FE}
\definecolor{blue6}{HTML}{4C4CFE}
\definecolor{blue7}{HTML}{3333FE}
\definecolor{blue8}{HTML}{1919FE}
\definecolor{blue9}{HTML}{0000FE}

在我的代码中,我使用了这个嵌套的for 循环(其中KnownResults 是一个带有数字条目的数据框):

for(i in seq_along(colnames(KnownResults))){ 
  for(q in seq_along(rownames(KnownResults))){ 
    if(KnownResults[q,i]!="NA"){ 
          if(KnownResults[q,i] > 0.9){ 
            KnownResults[q,i] <- paste0("\\cellcolor{blue9}{", 
                                        "\\textcolor{white}{",
                                        formatC(KnownResults[q,i], format='f', digits=4 ),
                                        "}}")            
          } else if(KnownResults[q,i] > 0.8){
            KnownResults[q,i] <- paste0("\\cellcolor{blue8}{", 
                                        "\\textcolor{white}{",
                                        formatC(KnownResults[q,i], format='f', digits=4 ),
                                        "}}")
          } else if(KnownResults[q,i] > 0.7){
             KnownResults[q,i] <- paste0("\\cellcolor{blue7}{", 
                                        "\\textcolor{white}{",
                                        formatC(KnownResults[q,i], format='f', digits=4 ),
                                        "}}")
          } else if(KnownResults[q,i] > 0.6){
            KnownResults[q,i] <- paste0("\\cellcolor{blue6}{", 
                                        "\\textcolor{white}{",
                                        formatC(KnownResults[q,i], format='f', digits=4 ),
                                        "}}")
          } else if(KnownResults[q,i] > 0.5){
            KnownResults[q,i] <- paste0("\\cellcolor{blue5}{", 
                                        "\\textcolor{white}{",
                                        formatC(KnownResults[q,i], format='f', digits=4 ),
                                        "}}")
          } else if(KnownResults[q,i] > 0.4){            
            KnownResults[q,i] <- paste0("\\cellcolor{blue4}{",  
                                        formatC(KnownResults[q,i], format='f', digits=4 ),
                                        "}")
          } else if(KnownResults[q,i] > 0.3){
            KnownResults[q,i] <- paste0("\\cellcolor{blue3}{", 
                                        formatC(KnownResults[q,i], format='f', digits=4 ),
                                        "}")
          } else if(KnownResults[q,i] > 0.2){
            KnownResults[q,i] <- paste0("\\cellcolor{blue2}{", 
                                        formatC(KnownResults[q,i], format='f', digits=4 ),
                                        "}")
          } else if(KnownResults[q,i] > 0.1){
            KnownResults[q,i] <- paste0("\\cellcolor{blue1}{", 
                                        formatC(KnownResults[q,i], format='f', digits=4 ),
                                        "}")
          } else if(KnownResults[q,i] > 0.0){
            KnownResults[q,i] <- paste0("\\cellcolor{blue0}{", 
                                        formatC(KnownResults[q,i], format='f', digits=4 ),
                                        "}")
          } else {}

    } else { KnownResults[q,i]<-"NS"}
  }

}

我相信您可以修改它以满足您的需要(并可能简化代码!)。

渐变颜色值也是使用此代码预先确定的:

HPSColors<-colorRamp(c('white','blue'))
HPSColors<-rgb(HPSColors(seq(from=0.1,to=1.0,by=0.1)),maxColorValue=256)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    • 2022-04-27
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    相关资源
    最近更新 更多