[英]How coloring data frame in R
I have data frame of the following type 我有以下类型的数据框
> df
V1 V2 V3 V4 V5
1 10.603.3.100 2 1 5 1
2 10.603.3.101 3 2 4 5
3 10.603.3.102 1 3 3 2
4 10.603.1.103 4 4 3 3
5 10.603.3.104 5 5 1 4
And the task is to colorize values by any palette by columns V2, V3, V4, V5. 任务是通过V2,V3,V4,V5列通过任何调色板对值进行着色。 But unfortunately I have absolutely no ideas how to do it.
但是不幸的是,我完全不知道如何去做。 The forum found answers to the question of coloring in two colors.
论坛找到了两种颜色着色问题的答案。
Something similar 相似的东西
If I understood your question correctly then you may use library(DT)
to colorise your columns like below: 如果我正确理解了您的问题,则可以使用
library(DT)
为列着色,如下所示:
?datatable
from DT package: ?datatable
从DT包:
This function creates an HTML widget to display rectangular data (a matrix or data frame) using the JavaScript library DataTables.
此函数创建一个HTML小部件,以使用JavaScript库DataTables显示矩形数据(矩阵或数据框)。
df <- head(iris, 10)
library(tidyverse)
library(DT)
datatable(df, rownames = FALSE) %>%
formatStyle(columns = "Sepal.Length",
backgroundColor = "yellow") %>%
formatStyle(columns = "Sepal.Width",
backgroundColor = "blue") %>%
formatStyle(columns = "Petal.Width",
backgroundColor = "green")
New EDIT after OP added Picture : OP添加图片后的新EDIT图片 :
df <- data.frame(cbind(matrix(round(rnorm(50), 3), 10), sample(0:1, 10, TRUE)))
breaks <- quantile(df, probs = seq(.05, .95, .05), na.rm = TRUE)
colors <- round(seq(255, 40, length.out = length(breaks) + 1), 0) %>%
{paste0("rgb(255,", ., ",", ., ")")}
datatable(df) %>% formatStyle(names(df), backgroundColor = styleInterval(breaks, colors))
You can check more from here : 您可以从这里查看更多信息 :
The above example is taken straight from the above place, Please note that if you want to change the color you have to change the "colors" object with different value of rgb, for example if you change the above string with this: 上面的示例是直接从上面的地方获取的,请注意,如果要更改颜色,则必须使用rgb的不同值来更改“ colors”对象,例如,如果您使用以下方法更改上面的字符串:
colors <- round(seq(255, 40, length.out = length(breaks) + 1), 0) %>%
{paste0("rgb(", ., ",255,", ., ")")}
You will get parrot green flavour instead of red. 鹦鹉会变成绿色而不是红色。
How about a heatmap? 热点图怎么样?
library(ComplexHeatmap)
library(circlize)
set.seed(123)
mat = cbind(rbind(matrix(rnorm(16, -1), 4), matrix(rnorm(32, 1), 8)),
rbind(matrix(rnorm(24, 1), 4), matrix(rnorm(48, -1), 8)))
# permute the rows and columns
mat = mat[sample(nrow(mat), nrow(mat)), sample(ncol(mat), ncol(mat))]
rownames(mat) = paste0("R", 1:12)
colnames(mat) = paste0("C", 1:10)
Plot the heatmap with default settings. 使用默认设置绘制热图。 The default style of the heatmap is quite the same as those generated by other similar heatmap functions.
热图的默认样式与其他类似热图函数生成的样式完全相同。
Heatmap(mat)
https://bioconductor.org/packages/devel/bioc/vignettes/ComplexHeatmap/inst/doc/s2.single_heatmap.html https://bioconductor.org/packages/devel/bioc/vignettes/ComplexHeatmap/inst/doc/s2.single_heatmap.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.