我有以下代码来显示热图上方的颜色键。但是颜色键在热图的顶部(稍微向右移动了一点)并不精确。有谁知道怎么让颜色不变吗?另外,如何删除热图右侧的空白?谢谢。
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)
)

发布于 2016-01-05 17:22:40
可以通过在左边的网格中添加“填充部分”(在我的特殊情况下是“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))

发布于 2014-07-08 11:05:11
虽然不完全符合您的要求,但这里提供了一种使用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())

发布于 2014-07-31 05:32:40
我可以解决自己的色键相对位置问题,但要处理与key.par相关的边距值
key.par=list(mar=c(bottom, left, top, right))只需替换你想要调整的颜色键的'bottom','left','top‘和'right’边距的值(每一个的默认边距是4,这为任何标签提供了空间)。
key.par=list(mar=c(4,4,4,4)使用默认边距。
key.par=list(mar=c(4,4,4,10))会把它从右边移过来。您必须查看除10之外的哪个值最适合您的绘图。
https://stackoverflow.com/questions/24621070
复制相似问题