用ggplot列创建一个热图

时间:2015-06-10 18:17:12

标签: r ggplot2 heatmap

我有以下(小)融化数据。它是R RDS文件,是传输R数据集的最佳方式!您需要data.table库。

> urlf = 'http://emboss.math.yorku.ca/results/ove_m_rds'
> ove.m = readRDS(gzcon(url(urlf))) ##download the data.table

基本上,我有以下代码来制作数据的热图:

  gg = ggplot(ove.m, aes(variable, state)) 
  gg = gg +  geom_tile(aes(fill = value), colour = "white")
  gg = gg +  scale_fill_gradient(low = "white", high = "#1f78b4")

但问题是颜色被调整为矩阵中的所有值。我希望颜色仅代表。因此,对于上述数据,得到的热图如下所示:

enter image description here

2 个答案:

答案 0 :(得分:1)

urlf = 'http://emboss.math.yorku.ca/results/ove_m_rds'
ove.m = readRDS(gzcon(url(urlf))) ##download the data.table

a           <- data.frame(matrix(nrow = 9, ncol = 7))
names(a)    <- unique(levels(ove.m$variable))
ove.m$state <- as.numeric(ove.m$state)
for(i in 1:9){
  a$genomecoverage[i] <- as.numeric(ove.m$value[ove.m$variable == "genomecoverage" & ove.m$state == i ])
  a$cpgisland[i]      <- ove.m$value[ove.m$variable == "cpgisland" & ove.m$state == i ]
  a$exon[i]           <- ove.m$value[ove.m$variable == "exon" & ove.m$state == i ]
  a$gene[i]           <- ove.m$value[ove.m$variable == "gene" & ove.m$state == i ]
  a$tes[i]            <- ove.m$value[ove.m$variable == "tes" & ove.m$state == i ]
  a$tss[i]            <- ove.m$value[ove.m$variable == "tss" & ove.m$state == i ]
  a$tss2kb[i]         <- ove.m$value[ove.m$variable == "tss2kb" & ove.m$state == i ]
}

b       <- as.data.frame(apply(a, 2, scale))
b$state <- 1:9
c       <- melt(b, id.vars = "state")

gg = ggplot(ove.m, aes(variable, state)) 
gg = gg +  geom_tile(aes(fill = value), colour = "white")
gg = gg +  scale_fill_gradient(low = "white", high = "#1f78b4")
gg

答案 1 :(得分:0)

我找不到这个问题的答案。我正在寻找一种解决方案,其中ggplot2可以识别列组并在分组上创建颜色比例。

替代解决方案是相应地缩放值。对于列x的每个条目,公式为

x - min(column) divided by max(column)