R中热图的颜色 [英] Colors for heatmap in R

查看:221
本文介绍了R中热图的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用R制作热图,但是对于R来说还是很陌生,我有一些疑问:

I'm making a heatmap in R, but being very new to R, I have some questions:

我的数据是一个由21列和89行组成的大矩阵,包含从0到16的数字.我希望以一种热图的方式将热图着色为从0(白色)到16(深红色-或该颜色的任何颜色)清酒).甚至可能更幻想,使用从0到< 10的调色板(以使具有10个"hits"以上的点获得相同的颜色).

My data is a big matrix 21 columns and 89 rows, containing numbers from 0 to 16. I would like to get the heatmap colored in a heatmappy way from 0 (white) to 16 (dark red - or any color for that sake). Or maybe even fancier, have a color palette going from 0 to <10 (so that the points having above 10 "hits" get the same color).

有人可以帮我吗?非常感谢!

Can anyone help me with this ? Thanks alot!

我的代码:

library(ggplot2)
library("RColorBrewer")

AS <- read.csv("L:/Pseudoalteromonas/Heatmap antismash/HM_phyl.csv", sep=";")

row.names(AS) <- AS$Strain

AS <- AS[,2:21]

## The colors you specify.
my_palette <- colorRampPalette(c("white", "yellow","orange", "red"))(n = 299)

AS_matrix <- data.matrix(AS)

AS_heatmap <- heatmap(AS_matrix, Rowv=NA, Colv=NA, col = my_palette, scale="row", margins=c(5,10))

我的数据如下:

tail(HM)
   Sideophore Bacteriocin Aryl.polyene Nrps T1pks T2pks T3pks T1pks.Nrps Lantipeptide Terpene Hserlactone Transatpks

S4048          0           2            0    2     0     0     0          0            1       0           0          1
S3655          1           2            2    0     0     0     0          0            0       0           0          0
S4060          0           2            0    7     0     1     1          2            1       0           0          1
S2607          0           2            0   10     1     1     1          4            1       0           0          1
S4054          0           2            1    3     0     0     0          4            1       0           1          1
S4047          0           2            1    7     0     0     0          4            1       0           1          1
  Butyrolactone Indole Thiopeptide Ladderane Pufa Resorcinol Otherks Other
S4048             0      0           0         0    0          0       0     0
S3655             0      0           0         0    0          1       0     0
S4060             0      1           0         0    0          0       0     2
S2607             0      1           0         0    0          0       0     2
S4054             0      1           0         1    0          0       0     0
S4047             0      1           0         1    0          0       0     2

推荐答案

您可以将数据保留为data.frame并使用ggplot2(自从您调用ggplot2以来,这就是您要的目的?)

You could keep the data as a data.frame and use ggplot2 (it looks like that is what you are intending since you called ggplot2?)

library(ggplot2)
library(RColorBrewer)
library(tidyverse)

set.seed(12343)
# create matrix with 21 columns and 89 rows
# with numbers between 0 - 16

AS <- runif(n= 1869, min = 0, max = 16) %>%
  matrix(., nrow = 89)

colnames(AS) <- LETTERS[1:21]

AS <- as.data.frame(AS)
AS$train <- 1:89

AS <- gather(AS, A:U, key = "colname", value="value")

ggplot(AS, aes(x = colname, y=train)) + geom_tile(aes(fill = value), 
                                                  colour = "white") +
  scale_fill_distiller(palette = "Reds", limits = c(0,10), na.value = "#de2d26",
                       direction = 1, labels = c(0.0, 2.5, 5.0, 7.5, "> 10.0"))

因此,使用您的代码,也许像这样:

So using your code, maybe something like this:

library(ggplot2)
library(RColorBrewer)
library(tidyverse)


AS <- read.csv("L:/Pseudoalteromonas/Heatmap antismash/HM_phyl.csv", sep=";")

# assume "train" is the row indicator, so we will use
# gather with -train argument to gather all columns but "train"

AS <- gather(AS, -train, key = "colname", value="value")


ggplot(AS, aes(x = colname, y=train)) + geom_tile(aes(fill = value), 
                                                  colour = "white") +
  scale_fill_distiller(palette = "Reds", limits = c(0,10), na.value = "#de2d26",
                       direction = 1, labels = c(0.0, 2.5, 5.0, 7.5, "> 10.0"))

# na.values (values >10) take maximum red color in Reds colorbrewer

这篇关于R中热图的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆