farrellja / URD

URD - Reconstruction of Branching Developmental Trajectories
GNU General Public License v3.0
115 stars 41 forks source link

reorder cluster of heatmap #47

Closed MichaelPeibo closed 4 years ago

MichaelPeibo commented 4 years ago

Hi, @farrellja I am using your complete code to plot a cascade heatmap of my own data.

# Hierarchical cluster based on smoothed expression
h.ss <- hclust(dist(as.matrix(ss[varying.genes.5cell, ])), method = "ward.D2")
h.ss.d <- as.dendrogram(h.ss)
k = 15 # Cluster number chosen by eye.
h.ss.clust <- cutree(h.ss, k = k)
# Get cluster order as it will be in the heatmap
clust.order <- unique(h.ss.clust[rev(h.ss$order)])
h.ss.clust.ord <- plyr::mapvalues(from = clust.order, to = 1:k, x = h.ss.clust)
# Generate cluster color vector
cluster.h <- seq(0, 1, length.out = k + 1)
cluster.s <- rep(c(1, 0.75), length = k)
cluster.v <- rep(c(1, 0.75), length = k)
cluster.colors <- hsv(h = cluster.h[1:k], s = cluster.s, v = cluster.v)
h.ss.clust.col <- plyr::mapvalues(from = as.character(1:k), to = cluster.colors,
x = h.ss.clust.ord)
# Generate the actual heatmap and save as a PDF.
pdf(paste0(main.path, "URD/GranularZymogen/GranularZymogen-Varying.pdf"), width = 17,
height = 22)
# Plot the heatmap
gplots::heatmap.2(x = as.matrix(ss[varying.genes.5cell[rev(h.ss$order)], ]), Rowv = F,
RowSideColors = h.ss.clust.col[rev(h.ss$order)], Colv = F, dendrogram = "none",
col = cols, trace = "none", density.info = "none", key = F, cexCol = 0.8, cexRow = 0.08,
margins = c(8, 8), lwid = c(0.3, 4), lhei = c(0.3, 4), labCol = NA)
# Put a title on it
title("Granular/Zymogen Expression", line = -1, adj = 0.48, cex.main = 4)
# Add tissue labels to bottom
title("Head", line = -103, adj = 0.08, cex.main = 2)
title("Foot", line = -103, adj = 0.95, cex.main = 2)
dev.off()

How to reorder the cluster of heatmap? I try to set new order of clust.order, such as clust.order <- c(3,2,1,4,5,6,7,8,9,10), but the heatmap keeps the same.

Should I reorder the matrix? Any suggestion? Thanks!

btw: sorry for pulling a 'heatmap.2-like' issue.

farrellja commented 4 years ago

@MichaelPeibo

In the end, the order of genes in the heatmap is determined by the matrix that you feed to heatmap.2. So, you could reorder ss however you like, honestly. The reason that changing clust.order is not working for you is that it is not used downstream -- the order of the rows in the code above is solely set by the hierarchical clustering function. clust.order above is reading out the order that the clusters will appear in the heatmap, and used in order to set the color bar that accompanied them.