2016-04-30 81 views
0

我希望能夠在knitr中生成表格,根據映射到單元格中的值的顏色梯度設置單元格背景顏色。有沒有一種方便的方式在knitr中製作熱圖式桌子?

有關示例,請參閱towards the bottom of this page

Pander似乎能夠做一些有條件的突出顯示,as described in this question,但我找不到允許條件格式化單元背景的選項。

+0

'pander'將無法幫助色素細胞,這不是在'pandoc'的降價支撐,但如果你想:

而且漸變色值爲使用此代碼預定如HTML輸出,不需要生成其他文檔格式,我熱烈建議嘗試https://github.com/renkun-ken/formattable。 – daroczig

回答

3

見例如年齡列的that page

+0

當您引用的格式化表格的代碼包含在.Rnw腳本(knitr和LaTeX)的塊中時,在調用庫(formattable)之後,生成的pdf文件中會顯示一條錯誤消息:「eval中的錯誤(expr,envir,enclos):找不到函數格式。「該包是否適用於.Rnw和例如xelatex? – lawyeR

+0

@Dontas,這似乎是一個斷開的鏈接。 –

1

我已經在過去的LaTeX來完成這個底部簡單地通過解析數據幀要傳遞給xtable和評估表項,只是在相應的增加乳膠色標記。例如,我的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) 
相關問題