heatmap.2,顶部带有颜色键

时间:2014-07-07 22:41:25

标签: r heatmap

我有以下代码来显示热图上方的颜色键。但是热图的顶部(稍微向右移动)的颜色键并不准确。有谁知道如何使颜色不移动?另外,如何去除热图右侧的空白区域?感谢。

library(gplots)
heatmap.2(
  matrix(rnorm(100*10), nrow=100)
  , dendrogram='none'
  , Colv = F
  , Rowv = F
  , trace='none'
  , col = colorRampPalette(c('blue', 'yellow'))(12)
  , labRow=NA
  , labCol=NA
  , density.info='none'
  , lmat=rbind(c(4, 2), c(1, 3)), lhei=c(2, 8), lwid=c(4, 1)
)

heatmap.2 example http://i62.tinypic.com/143grat.png

3 个答案:

答案 0 :(得分:6)

可以通过在左边的点阵中添加“填充部分”(“5”和“6”,在我的特定情况下)来使颜色键居中(请参阅代码最后一行的“#”注释:

heatmap.2(x=matrix(rnorm(20*10), nrow=10), Rowv=NULL,Colv=NULL, 
          col = rev(rainbow(20*10, start = 0/6, end = 4/6)), 
          scale="none",
          margins=c(3,0), # ("margin.Y", "margin.X")
          trace='none', 
          symkey=FALSE, 
          symbreaks=FALSE, 
          dendrogram='none',
          density.info='histogram', 
          denscol="black",
          keysize=1, 
          #( "bottom.margin", "left.margin", "top.margin", "left.margin" )
          key.par=list(mar=c(3.5,0,3,0)),
          # lmat -- added 2 lattice sections (5 and 6) for padding
          lmat=rbind(c(5, 4, 2), c(6, 1, 3)), lhei=c(2.5, 5), lwid=c(1, 10, 1))

centered legend of heatmap.2()

答案 1 :(得分:1)

不完全是您所要求的,但这是使用ggplot创建或多或少相同情节的方法。

library(ggplot2)
library(reshape2)      # for melt(...)
library(grid)          # for unit(...)

set.seed(1)            # for reproducible example
df <- data.frame(matrix(rnorm(100*10), nr=10))
df.melt <- melt(cbind(x=1:nrow(df),df),id="x")
ggplot(df.melt,aes(x=factor(x),y=variable,fill=value)) +
  geom_tile() +
  labs(x="",y="")+
  scale_x_discrete(expand=c(0,0))+
  scale_fill_gradientn(name="", limits=c(-3,3),
                       colours=colorRampPalette(c('blue', 'yellow'))(12))+
  theme(legend.position="top", 
        legend.key.width=unit(.1,"npc"),legend.key.height=unit(.05,"npc"),
        axis.text=element_blank(),axis.ticks=element_blank())

答案 2 :(得分:0)

我能够用颜色键相对位置来解决我自己的问题,但摆弄了与key.par相关的边距值

key.par=list(mar=c(bottom, left, top, right))

只需替换&#39; bottom&#39;,&#39; left&#39;,&#39; top&#39;并且&#39;对&#39;要调整的颜色键的边距(每个的默认边距为4,这为任何标签提供了空间)。

key.par=list(mar=c(4,4,4,4)

使用默认边距。

key.par=list(mar=c(4,4,4,10))

会将其从右侧移开。您必须看到10以外的值对您的情节最有效。

相关问题