如何在R中着色数据框

时间:2017-12-08 14:42:14

标签: r dataframe plot

我有以下类型的数据框

> 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按任何调色板着色值。但不幸的是,我完全没有想法如何去做。论坛找到了两种颜色着色问题的答案。

类似的东西

enter image description here

2 个答案:

答案 0 :(得分:3)

如果我正确理解了您的问题,那么您可以使用library(DT)为您的列着色,如下所示:

来自DT包的

?datatable

  

此函数创建一个HTML小部件以显示矩形数据(a   矩阵或数据框)使用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") 

enter image description here

OP添加图片后的新编辑

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))

enter image description here

您可以从here查看更多内容:

以上示例直接取自上述地方,请注意,如果您想更改颜色,则必须更改&#34;颜色&#34;具有不同rgb值的对象,例如,如果您使用更改上面的字符串 这样:

colors <- round(seq(255, 40, length.out = length(breaks) + 1), 0) %>% 
  {paste0("rgb(", ., ",255,", ., ")")}

你会得到鹦鹉绿色而不是红色。

答案 1 :(得分:0)

热图怎么样?

Heatmap(mat)

使用默认设置绘制热图。热图的默认样式与其他类似热图功能生成的样式完全相同。

using System;

namespace MyApp {

class KeyBoardInputTest {
static void Main(string [] args) {    // This line has been changed
    Console.Write("Enter a character >\t");
    char ch = Convert.ToChar(args[0])   // This line has been changed
    Console.WriteLine("");

    if(Char.IsLetter(ch)) {
        if(Char.IsUpper(ch))
            Console.WriteLine("You have entered an upper case character");
        else
            Console.WriteLine("You have entered a lower case character");
    }
    else if(Char.IsNumber(ch))
        Console.WriteLine("You have entered a numeric character");
    else
        Console.WriteLine("You have entered a non alpha-numeric character");
    Console.Read();
}
}
} 

enter image description here

https://bioconductor.org/packages/devel/bioc/vignettes/ComplexHeatmap/inst/doc/s2.single_heatmap.html