我希望能夠在knitr中生成表格,根據映射到單元格中的值的顏色梯度設置單元格背景顏色。有沒有一種方便的方式在knitr中製作熱圖式桌子?
有關示例,請參閱towards the bottom of this page。
Pander似乎能夠做一些有條件的突出顯示,as described in this question,但我找不到允許條件格式化單元背景的選項。
我希望能夠在knitr中生成表格,根據映射到單元格中的值的顏色梯度設置單元格背景顏色。有沒有一種方便的方式在knitr中製作熱圖式桌子?
有關示例,請參閱towards the bottom of this page。
Pander似乎能夠做一些有條件的突出顯示,as described in this question,但我找不到允許條件格式化單元背景的選項。
我已經在過去的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)
'pander'將無法幫助色素細胞,這不是在'pandoc'的降價支撐,但如果你想:
而且漸變色值爲使用此代碼預定如HTML輸出,不需要生成其他文檔格式,我熱烈建議嘗試https://github.com/renkun-ken/formattable。 – daroczig