R中如何为数据框着色 [英] How coloring data frame in R
问题描述
我具有以下类型的数据框
> 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
任务是通过V2,V3,V4,V5列的任何调色板为值着色。但是不幸的是,我完全不知道如何去做。论坛找到了两种颜色着色问题的答案。
类似的东西
如果我正确理解了您的问题,则可以使用 library( DT)
为您的列着色,如下所示:
?datatable
:
此函数使用JavaScript库DataTables创建一个HTML小部件以显示矩形数据(
矩阵或数据框)。 / p>
df<-head(iris,10)
库(tidyverse)
库(DT)
datatable(df,行名= FALSE)%&%;%
formatStyle(columns = Sepal.Length,
backgroundColor = yellow )%>%
formatStyle(columns = Sepal.Width,
backgroundColor = blue)%>%
formatStyle(columns = Petal.Width,
backgroundColor = green)
OP添加图片后的新编辑:
df<-data.frame(cbind(matrix(round(rnorm(50),3), 10),sample(0:1,10,TRUE)))
中断<-分位数(df,概率= seq(.05,.95,.05),na .rm = TRUE)
颜色<-圆(seq(255,40,length.out =长度(breaks)+ 1),0)%>%
{paste0( rgb(255 ,,。,,,。,))}
datatable(df)%>%formatStyle(names(df),backgroundColor = styleInterval(breaks,colors))
:
上面的示例直接从上述位置获取,请注意,如果要更改颜色,则必须更改颜色例如,如果您使用
更改上述字符串,则该对象具有不同的rgb值:
colors< -round(seq(255,40,length.out = length(breaks)+ 1),0)%&%;%
{paste0( rgb(,。,,255,,。, ))}
鹦鹉会发出绿色而不是红色的味道。
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. 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:
?datatable
from DT package:
This function creates an HTML widget to display rectangular data (a matrix or data frame) using the JavaScript library 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:
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:
colors <- round(seq(255, 40, length.out = length(breaks) + 1), 0) %>%
{paste0("rgb(", ., ",255,", ., ")")}
You will get parrot green flavour instead of red.
这篇关于R中如何为数据框着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!