对热图的修改

时间:2016-11-29 16:49:28

标签: r graphics heatmap

我正在制作热图,但是想要分隔列并在每行之间添加一条线。我很清楚,这样做很好,而不是热图。但这就是我老板设想的方式。

下面是我当前热图的代码。关于分离柱子的任何建议&在每个“人”之间添加一条线将非常感激。

x11 <- c(0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1) 
x22 <- c(1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1)
x <- rbind(x11, x22) 
hv <- heatmap(t(x), col = c("cornflowerblue", "hotpink"), margins = c(4, 12), Colv = NA, Rowv = NA, scale = "none", xlab ="", ylab ="", main = "",  labCol=c("BP", "Cx"), cexCol =2)
legend("topright", c("No Osteomyelitis", "Osteomyelitis"), col=c("cornflowerblue", "hotpink"), bty="n", fill=c("cornflowerblue", "hotpink"))

是的,我最终使用下面的代码。谢谢你的回答。我使用了gplots&amp;摆脱了颜色键和&amp;直方图和添加了我自己的传奇。

hv <- heatmap.2(t(x), key=FALSE, trace="none", colsep = seq(1,nrow(x)-1),
      rowsep = seq(1,ncol(x)-1),
      sepcolor = "white",
      sepwidth = c(0.1, 0.0005), col = c("cornflowerblue", "hotpink"), margins = c(4, 12), Colv = NA, Rowv = NA, scale = "none", xlab ="", ylab ="", main = "",  labCol=c("BP", "Cx"), cexCol =2)
legend("topleft", c("No Osteomyelitis", "Osteomyelitis"), col=c("cornflowerblue", "hotpink"), bty="n", fill=c("cornflowerblue", "hotpink"))

1 个答案:

答案 0 :(得分:0)

我建议您切换到gplots::heatmap.2(),这样可以更好地控制主要使用相同参数的绘图。

通过添加colseprowsepsepcolorsepwidth参数来控制行和列之间的分隔,以您的良好示例(+1 btw)为基础(和trace = 'none',因为我不喜欢它)给出:

x11 <- c(0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1) 
x22 <- c(1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1)
x <- rbind(x11, x22) 
hv <- heatmap(t(x), col = c("cornflowerblue", "hotpink"), margins = c(4, 12), Colv = NA, Rowv = NA, scale = "none", xlab ="", ylab ="", main = "",  labCol=c("BP", "Cx"), cexCol =2)
legend("topright", c("No Osteomyelitis", "Osteomyelitis"), col=c("cornflowerblue", "hotpink"), bty="n", fill=c("cornflowerblue", "hotpink"))

library(gplots)
heatmap.2(t(x), 
      col = c("cornflowerblue", "hotpink"), 
      margins = c(4, 12), 
      Colv = NA, Rowv = NA, 
      scale = "none", 
      xlab ="", 
      ylab ="", 
      main = "",
      labCol=c("BP", "Cx"), 
      cexCol = 2,
      trace = 'none',
      colsep = seq(1,nrow(x)-1),
      rowsep = seq(1,ncol(x)-1),
      sepcolor = "white",
      sepwidth = c(0.1, 0.05))

heatmap with separation

要更多地分隔列,请增加sepwidth的第一个元素,并为行添加类似的内容。