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))