Question: Reorder issue when combining heatmap with dendrogram
gravatar for iraun
4.9 years ago by
iraun3.6k wrote:

Hi there,

I'm trying to figure out a heatmap with gene expression under 3 different conditions. Also I want to add a dendrogram with the clusterization of those genes. These are the commands I'm using in ggplot2 to do the heatmap:

data_r <- read.table("gene_table.txt", sep="\t", header=TRUE, row.names=1)
data <- data_r[order(data_r$cond1, data_r$cond2, data_r$cond3),]   # Order columns highest to lowest
data$gene <- row.names(data)
data$gene <- with(data, factor(gene, levels=gene, ordered=TRUE))
melt_data <- melt(data, id.vars="gene")
ggplot(melt_data, aes(x=variable, y=gene)) + geom_tile(aes(fill=value)) + scale_fill_gradient(low = "yellow",high = "blue")

Using the previous commands, I get a heatmap with the highest values on the top. The problem is that when I try to figure out a dendrogram, the heatmap is reordered:

x <- as.matrix(data)
dd.col <- as.dendrogram(hclust(method="average",dist((x),method = "euclidean")))
col.ord <- order.dendrogram(dd.col)
xx <- (data)[col.ord,]
xx_names <- attr(xx, "dimnames")
df <-
df$gene <- row.names(df)
df$gene <- with(df, factor(gene, levels=gene, ordered=TRUE))
mdf <- melt(df, id.vars="gene")
ddata_y <- dendro_data(dd.col)


When I figure out the heatmap with the dendrogram, the heatmap is reordered, I think due to xx <- (data)[col.ord,] command. My question is, there is anyway to produce the dendrogram without reordering the heatmap?

Thanks in advance,


heatmap ggplot2 R • 3.2k views
ADD COMMENTlink modified 4.9 years ago • written 4.9 years ago by iraun3.6k

The whole point of a dendrogram is to reorder things by similarity. The correct solution is to either keep things reordered or don't use a dendrogram to begin with.

ADD REPLYlink written 4.9 years ago by Devon Ryan91k

Thanks Devon, I know that the goal of a dendrogram is to reorder things but, if I have "my own" order, it is possible to create a dendro without reordering?(even it makes no sense...) Thanks!

ADD REPLYlink written 4.9 years ago by iraun3.6k

I've never looked, but even if so the result could be a mess of lines.

ADD REPLYlink written 4.9 years ago by Devon Ryan91k

Thanks a lot Devon ;)

ADD REPLYlink written 4.9 years ago by iraun3.6k
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2323 users visited in the last hour