You can center the color key by adding “indentation” (in my case, “5” and “6”) to the grid on the left (see comment “#” on the last line of code:
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),
trace='none',
symkey=FALSE,
symbreaks=FALSE,
dendrogram='none',
density.info='histogram',
denscol="black",
keysize=1,
key.par=list(mar=c(3.5,0,3,0)),
lmat=rbind(c(5, 4, 2), c(6, 1, 3)), lhei=c(2.5, 5), lwid=c(1, 10, 1))

source
share