[R] apply a color range (kind of like a heat map) to the values in each cell of a data frame
Yuan Chun Ding
ycd|ng @end|ng |rom coh@org
Fri Jun 18 02:03:51 CEST 2021
Dear R users,
I have a numeric table with 140 rows and 30 columns, here I only made partial table, test1, as an example. I want to apply a blue color range to the value in each cell of the data frame test1.
I found some R code using DT library. However, I only can see the colored table at my R studio viewer window, also only show the first 10 rows. I hope to save the colored table into a PNG file and want to modify the size of each cell, so I can apply the code to a much bigger table with 140 rows and 30 column.
Can you help me?
Thank you very much!!
Ding
s1 <-c(0.085,0.086,0.139,0.129,0.235,0.177,0.000,0.126,0.271,0.000,0.083,0.163)
s2 <-c(0.000,0.093,0.000,0.080,0.072,0.388,0.138,0.107,0.000,0.000,0.474,0.000)
s13 <-c(0.000,0.077,0.000,0.000,0.000,0.000,0.000,0.000,0.000,0.166,0.308,0.000)
s3 <-c(0.000,0.478,0.000,0.332,0.163,0.000,0.000,0.000,0.145,0.469,0.000,0.000)
test1 <-data.frame(s1,s2,s13,s3)
row.names(test1)<c("COH_001","COH_002","COH_003","COH_004","COH_005","COH_006","COH_007","COH_008","COH_009","COH_010","COH_011","COH_012")
library(DT)
dtable1 <- datatable(test1, rownames=TRUE, options = list(lengthChange = FALSE, dom='t'))
colRamp <- colorRamp(c("white","blue"))
for(column in names(test1)){
x <- na.omit(test1[[column]])
brks <- quantile(x, probs = seq(.05, .95, .01))
RGB <- colRamp(c(0, (brks-min(x))/(max(x)-min(x))))
clrs <- apply(RGB, 1, function(rgb){
sprintf("rgb(%s)", toString(round(rgb,0)))
})
dtable1 <- dtable1 %>%
formatStyle(column, backgroundColor = styleInterval(brks, clrs))
}
dtable1
----------------------------------------------------------------------
------------------------------------------------------------
-SECURITY/CONFIDENTIALITY WARNING-
This message and any attachments are intended solely for the individual or entity to which they are addressed. This communication may contain information that is privileged, confidential, or exempt from disclosure under applicable law (e.g., personal health information, research data, financial information). Because this e-mail has been sent without encryption, individuals other than the intended recipient may be able to view the information, forward it to others or tamper with the information without the knowledge or consent of the sender. If you are not the intended recipient, or the employee or person responsible for delivering the message to the intended recipient, any dissemination, distribution or copying of the communication is strictly prohibited. If you received the communication in error, please notify the sender immediately by replying to this message and deleting the message and any accompanying files from your system. If, due to the security risks, you do not wish to receive further communications via e-mail, please reply to this message and inform the sender that you do not wish to receive further e-mail from the sender. (LCP301)
More information about the R-help
mailing list