【发布时间】:2019-04-24 05:57:18
【问题描述】:
我一直在努力定位 heatmap.2 输出的组件。
I found this old answer@IanSudbery 解释了元素定位的工作原理,这似乎很清楚,我认为它给了我所需的理解,但我仍然没有掌握一些东西。
我知道这些元素基本上都放在了一个格子窗中,但它们的行为方式与我理解的不同。
这是我的代码和当前输出(最底部是对图形元素进行排序的有趣部分):
for(i in 1:length(ConditionsAbbr)) {
# creates its own colour palette
my_palette <- colorRampPalette(c("snow", "yellow", "darkorange", "red"))(n = 399)
# (optional) defines the colour breaks manually for a "skewed" colour transition
col_breaks = c(seq(0,0.09,length=100), #white 'snow'
seq(0.1,0.19,length=100), # for yellow
seq(0.2,0.29,length=100), # for orange 'darkorange'
seq(0.3,1,length=100)) # for red
# creates a 5 x 5 inch image
png(paste(SourceDir, "Heatmap_", ConditionsAbbr[i], "XYZ.png"), # create PNG for the heat map
width = 5*600, # 5 x 600 pixels
height = 5*600,
res = 300, # 300 pixels per inch
pointsize = 8) # smaller font size
heatmap.2(ConditionsMtx[[ConditionsAbbr[i]]],
cellnote = ConditionsMtx[[ConditionsAbbr[i]]], # same data set for cell labels
main = paste(ConditionsAbbr[i], "XYZ"), # heat map title
notecol="black", # change font color of cell labels to black
density.info="none", # turns off density plot inside color legend
trace="none", # turns off trace lines inside the heat map
margins =c(12,9), # widens margins around plot
col=my_palette, # use on color palette defined earlier
breaks=col_breaks, # enable color transition at specified limits
dendrogram="none", # No dendogram
srtCol = 0 , #correct angle of label numbers
asp = 1 , #this overrides layout methinks and for some reason makes it square
adjCol = c(NA, -35) ,
adjRow = c(53, NA) ,
keysize = 1.2 ,
Colv = FALSE , #turn off column clustering
Rowv = FALSE , # turn off row clustering
key.xlab = paste("Correlation") ,
lmat = rbind( c(0, 3), c(2,1), c(0,4) ),
lhei = c(0.9, 4, 0.5) )
dev.off() # close the PNG device
}
如您所见,key在矩阵的右边,矩阵、上面的标题和下面的key之间有大量的空白,而且标题和矩阵不是在PNG中居中吗?
我对自己说“好吧,我将创建一个易于理解和编辑的 3x3”,例如
| |
| | (3)
| |
--------------------------
| (1) |
(2) | Matrix |
| |
--------------------------
| (4) |
| Key |
| |
然后我可以去掉空白,让它更像这样。
| |(3)
------------------
| (1) |
(2)| Matrix |
| |
------------------
|(4) Key |
我这样做是:
lmat = rbind( c(0, 0, 3), c(2, 1, 0), c(0, 4, 0) ),
lhei = c(0.9, 4, 0.5) ,
lwid = c(1, 4, 1))
尽管我的矩阵位于中心,但我的钥匙仍然对齐矩阵的右侧,并且我的标题是沿着丝绸之路东行?更不用说所有多余的空白了?
如何让它们对齐并一起移动,以使图形组件紧密贴合在一起?
编辑:减少我的边距有助于减少空白,但仍然过多。
【问题讨论】:
标签: r position alignment element heatmap