1

I have a dataframe like so:

set.seed(123)    
test_df<-data.frame(Sample=c(LETTERS),var1=rnorm(26),var2=rnorm(26),color=rep(sample(c("High","low"),26,replace=TRUE)))

I can generate a dendrogram from it using library(dendextend):

d<-dist(scale(test_df[,2:3]))
clust<-hclust(d)%>%as.dendrogram()

I want to use this dendrogram clust to order the samples in another dataframe. This dataframe has the same samples and looks like:

test_df_extend<-data.frame(Sample=c(LETTERS),var1=rnorm(26),var2=rnorm(26),var3=rnorm(26),var4=rnorm(26),var5=rnorm(26),var6=rnorm(26),color=rep(c("High","low"),13)) 

I can generate the heatmap for test_df_extend using the following code:

row.names(test_df_extend)<-test_df_extend$Sample
aheatmap(as.matrix(test_df_extend[,2:7]), color = "-RdBu:50", scale = "col", breaks = 0,Colv = NA,annColors = c("rainbow"),annRow=test_df_extend[,8])

However when I am trying to pass the dendrogram clust into aheatmap I am having problems comprehending the output. I can get a reordered dendrogram but unsure what the difference between 1) and 2) below means. One has Rowv specified and the other has reorderfun specified with a random w !?. I guess I don't fully understand the reorderfun arguments specially the weight (w) argument and its relationship to Rowv. Can anyone help me with an explanation? Thanks!

1)

aheatmap(as.matrix(test_df_extend[,2:7]), color = "-RdBu:50", scale = "col", breaks = 0,Colv = NA,annColors = c("rainbow"),annRow=test_df_extend[,8],Rowv=clust)

2)

aheatmap(as.matrix(test_df_extend[,2:7]), color = "-RdBu:50", scale = "col", breaks = 0,Colv = NA,annColors = c("rainbow"),annRow=test_df_extend[,8], reorderfun=function(d,w) reorder(clust,1))
thisisrg
  • 596
  • 3
  • 12
  • Have you looked at the dendextend R package? https://cran.r-project.org/web/packages/dendextend/vignettes/introduction.html#gplots – Tal Galili Feb 11 '17 at 18:43
  • @TalGalili Yes ..thanks for the package ..it has been immensely helpful. I had to use the rotate function to get the proper order I needed. I have recently started using it a lot. as you can see from the unnecessary as.dendrogam from dendextend in the question. For the heatmap, I got a better understanding of Rowv from http://stackoverflow.com/questions/15388082/understanding-heatmap-dendogram-clustering-in-r but still do not have a good intuition for the reorder function. – thisisrg Feb 11 '17 at 20:58

0 Answers0