Order of Rows in Heatmap

Order of rows in heatmap?

Thanks for the feedback, Jesse and Paolo. I wrote the following ordering function which will hopefully be useful to others:

data        = data.matrix(data)
distance = dist(data)
cluster = hclust(distance, method="ward")
dendrogram = as.dendrogram(cluster)
Rowv = rowMeans(data, na.rm = T)
dendrogram = reorder(dendrogram, Rowv)

## Produce the heatmap from the calculated dendrogram.
## Don't allow it to re-order rows because we have already re-ordered them above.

reorderfun = function(d,w) { d }
png("heatmap.png", res=150, height=22,width=17,units="in")

heatmap(data,col=colors,breaks=breaks,scale="none",Colv=NA,Rowv=dendrogram,labRow=NA, reorderfun=reorderfun)

dev.off()

## Re-order the original data using the computed dendrogram
rowInd = rev(order.dendrogram(dendrogram))
di = dim(data)
nc = di[2L]
nr = di[1L]
colInd = 1L:nc
data_ordered <- data[rowInd, colInd]
write.table(data_ordered, "rows.txt",quote=F, sep="\t",row.names=T, col.names=T)

heatmap.2 specify row order OR prevent reorder?

You are not specifying Rowv=FALSE and by default the rows are reordered (in heatmap.2 help, for parameter Rowv :

determines if and how the row dendrogram should be reordered. By
default, it is TRUE, which implies dendrogram is computed and
reordered based on row means. If NULL or FALSE, then no dendrogram is
computed and no reordering is done.

So if you want to have the rows ordered according to the last columns, you can do :

mat_data<-mat_data[order(mat_data[,ncol(mat_data)],decreasing=T),]

and then

heatmap.2(mat_data,
density.info="none",
trace="none",
margins =c(12,9),
col=my_palette,
breaks=col_breaks,
dendrogram='none',
Rowv=FALSE,
Colv=FALSE)

You will get the following image :
Sample Image

How does R heatmap order rows by default?

From heatmap help you can read:

Typically, reordering of the rows and columns according to some set of
values (row or column means) within the restrictions imposed by the
dendrogram is carried out.

In fact the reorder using Rowmeans/Colmeans is applied to the clustres. This is done internally in 2 steps. I will plot the dendogramm in each step to show how clusters are reordred.

hcr <- hclust(dist(m))
ddr <- as.dendrogram(hcr)
plot(ddr)

Sample Image

Now If you reorder the dendrogram this by rowmenas we get the same OP order.

Rowv <- rowMeans(m, na.rm = T)
ddr <- reorder(ddr, Rowv)
plot(ddr)

Sample Image

Of course this order can be changed , if you provide a new Clustering function or order function. Here I am using the default ones : hclust and reorder.

How can I individualize the order of rows in heatmap.2 (gplots)?

Sebastian Raschka [http://sebastianraschka.com] was so kind and helped me out with a solution to my problem that I am happy to share.

If you want to avoid that the rows (here: names of countries) are alphabetically ordered, you should write a function that orders the countries in the DataFrame as desired. For the sake of simplicity and to stick with the example, you can also quickly do it manually with such a small number of countries:

data.w <- data.w[c("Cyprus","Czech Republic","Estonia","Bulgaria","Croatia","Armenia","Azerbaijan","Georgia")]

We then overwrite the heatmap.2 function reorderfun with a placeholder function, which, however, won't "do" anything so that we keep the order of the DataFrame.

Overall, this is what the code should look like in order to produce a heat map as shown above (2nd image).

heatmap.2(as.matrix(t(data.w)), trace="none",
Rowv=NULL, Colv=NULL, dendrogram="none",
reorderfun=function(d,w) { d },
labCol=data.w$year,labRow=data.w$cname,
lhei=c(0.1,3), lwid=c(0.1,3), margins=c(5,8),
colsep=1:16, rowsep=1:22, sepcolor="white",
col=gray.colors(8, start=1, end=0)
)

Thanks a lot Sebastian!

Reordering heatmap rows in Python using a custom categorical index order

You can set df['DoW'] as a category first:

import seaborn as sns
import pandas as pd

days_order = ["Mon", "Tue", "Wed","Thu","Fri","Sat",'Sun']
df = pd.DataFrame({'Dow':np.random.choice(days_order,100),
'Hr':np.random.randint(0,4,100)})
df['Dow'] = pd.Categorical(df['Dow'],
categories=days_order,
ordered=True)

sns.heatmap(pd.crosstab(df['Dow'],df['Hr']))

Sample Image

Why heatmap row order is not the same as the data row order?

One option is to refactor it in your data.frame, or you can provide the order using limits= option in scale_y_discrete()

df$Value = replace(as.character(df$Value),df$Value=="<NA>",NA)
list_correct_order=c("SP4","SP5","SP7","SP2","SP3","SP6","SP8","SP1")

ggplot(df, aes(x=Categories, y=label))+
geom_tile(aes(fill=Value)) +
scale_y_discrete(limits=rev(list_correct_order))

Sample Image

Avoid Re-Ordering of Rows and Columns in a Heatmap (r)

I think the thing is how you properly set the variable that splits the heatmap.

Heatmap( (MyData), name = "namehere", 
column_names_rot = 0,
col = my_palette,
cluster_rows = FALSE,
cluster_columns = FALSE,
row_split = rep(1:4, each = 6),
column_split = rep(1:4, each = 6),
heatmap_legend_param = list(
title = "Legend",
col = my_palette,
at = (c(0.05,0.03,0.01,0.000001)),
legend_height = unit(6, "cm")),
border = TRUE
)

Sample Image



Related Topics



Leave a reply



Submit