如何在R中创建热图矩阵?

时间:2018-10-16 17:41:21

标签: r

CSV file image 有谁知道R代码将此CSV转换为热图矩阵?我希望最常用的构造是深红色,而只使用一次的构造几乎是白色。 任何帮助将不胜感激。

我尝试过:

heat <- read.csv("formations.csv", header = TRUE, sep = ",")
m <- as.matrix(heat[, -1])
rownames(m) <- heat$Formation
heatmap(m)

1 个答案:

答案 0 :(得分:0)

library(ggplot2)
library(reshape2)
d <- melt(d)
plt <- ggplot(data = d, aes(x = Formation, y = variable, fill = value))
plt <- plt + geom_tile() + scale_fill_gradient2(low ='white', high = 'red')
plt + theme_minimal()

SPA

数据

d <- structure(list(Formation = structure(c(7L, 8L, 4L, 6L, 9L, 1L, 
                                           2L, 5L, 10L, 3L, 7L, 8L, 4L, 6L, 9L, 1L, 2L, 5L, 10L, 3L, 7L, 
                                           8L, 4L, 6L, 9L, 1L, 2L, 5L, 10L, 3L, 7L, 8L, 4L, 6L, 9L, 1L, 
                                           2L, 5L, 10L, 3L, 7L, 8L, 4L, 6L, 9L, 1L, 2L, 5L, 10L, 3L, 7L, 
                                           8L, 4L, 6L, 9L, 1L, 2L, 5L, 10L, 3L), 
                                         .Label = c("3-4-1-2", "3-4-2-1", "3-5-1-1", "3-5-2", 
                                                    "4-1-2-1-2", "4-1-4-1", "4-2-3-1", "4-3-3", "4-4-2",
                                                    "5-3-2"), class = "factor"), 
                   variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
                                          2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
                                          4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L,
                                          5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), 
                                        .Label = c("13/14", "114/15", "115/16", "116/17", "117/18", 
                                                   "118/19"), class = "factor"),
                   value = c(33, 5, 0, 0, 0, 0, 0, 0, 0, 0, 38, 0, 0, 0, 0, 
                             0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 17, 1, 
                             1, 1, 0, 0, 0, 0, 0, 22, 10, 0, 0, 0, 3, 1, 1, 1, 0, 0, 6, 
                             0, 0, 0, 0, 0, 0, 1, 1)), 
              row.names = c(NA, -60L), 
              .Names = c("Formation", "variable", "value"), class = "data.frame")