使用 ggplot column wise 创建热图

creating a heatplot with ggplot column wise

我有以下(小)融化数据。它是一个 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")

但问题是颜色会根据矩阵中的所有值进行调整。我希望颜色仅代表 。因此,对于上述数据,生成的热图如下所示:

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

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

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

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