graph - Group variables by clusters on heatmap in R -


i trying reproduce first figure of this paper on graph clustering: enter image description here

here sample of adjacency matrix:

data=cbind(c(48,0,0,0,0,1,3,0,1,0),c(0,75,0,0,3,2,1,0,0,1),c(0,0,34,1,16,0,3,0,1,1),c(0,0,1,58,0,1,3,1,0,0),c(0,3,16,0,181,6,6,0,2,2),c(1,2,0,1,6,56,2,1,0,1),c(3,1,3,3,6,2,129,0,0,1),c(0,0,0,1,0,1,0,13,0,1),c(1,0,1,0,2,0,0,0,70,0),c(0,1,1,0,2,1,1,1,0,85)) colnames(data)=letters[1:nrow(data)] rownames(data)=colnames(data) 

and these commands obtain following heatmap:

library(reshape) library(ggplot2) data.m=melt(data) data.m[,"rescale"]=round(rescale(data.m[,"value"]),3) p=ggplot(data.m,aes(x1, x2))+geom_tile(aes(fill=rescale),colour="white")  p=p+scale_fill_gradient(low="white",high="black") p+theme(text=element_text(size=10),axis.text.x=element_text(angle=90,vjust=0))  

enter image description here

this similar plot on left of figure 1 above. differences (1) nodes not ordered randomly alphabetically, , (2) instead of having binary black/white pixels, using "shades of grey" palette able show strength of co-occurrence between nodes.

but point is hard distinguish cluster structure (and more true full set of 100 nodes). so, want order vertices clusters on heatmap. have membership vector community detection algorithm:

membership=c(1,2,4,2,5,3,1,2,2,3) 

now, how can obtain heatmap similar plot on right of figure 1 above?

thanks lot in advance help

ps: have experimented r draw kmeans clustering heatmap , r: how display clustered matrix heatmap (similar color patterns grouped) not want.

turned out extremely easy. still posting solution others in case don't waste time on did.

the first part same before:

data.m=melt(data) data.m[,"rescale"]=round(rescale(data.m[,"value"]),3) 

now, trick levels of factors of melted data.frame have ordered membership:

data.m[,"x1"]=factor(data.m[,"x1"],levels=levels(data.m[,"x1"])[order(membership)]) data.m[,"x2"]=factor(data.m[,"x2"],levels=levels(data.m[,"x2"])[order(membership)]) 

then, plot heat map (same before):

p=ggplot(data.m,aes(x1, x2))+geom_tile(aes(fill=rescale),colour="white")  p=p+scale_fill_gradient(low="white",high="black") p+theme(text=element_text(size=10),axis.text.x=element_text(angle=90,vjust=0)) 

enter image description here

this time, cluster visible.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -