簡體   English   中英

R 與 DataTables 接口中的“綠色”到“紅色”的單元格如何着色?

[英]How colorized cells from “green” to “red” in R interface to the DataTables?

有一個很好的例子: https://rstudio.github.io/DT/010-style.html顏色值范圍從“white”到“redrg2”

library(DT)

# 1. Data
df = as.data.frame(cbind(matrix(round(rnorm(50), 3), 10), sample(0:1, 10, TRUE)))

# 2. Create 19 breaks and 20 rgb color values ranging from white to red
brks <- quantile(df, probs = seq(.05, .95, .05), na.rm = TRUE)
clrs <- round(seq(255, 40, length.out = length(brks) + 1), 0) %>%
    {paste0("rgb(255,", ., ",", ., ")")}

# 3. DT
datatable(df) %>% formatStyle(names(df), backgroundColor = 
 styleInterval(brks, clrs))

但是如何從“綠色”到“紅色”進行相同的着色?

謝謝!

您可以使用colorRampPalette

ramp <- colorRampPalette(c("red", "green"))

然后 select 的數量 colors 來自length(brks)+1

clrs <- ramp(length(brks)+1)

這是你的想法嗎?

顏色漸變紅色到綠色的數據表

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM