====== Network matrix visualization in R ====== July 24, 2017 **Work in progress!!!** ===== Heat map experiments ===== > setwd("C:/Users/batagelj/Documents/2017/rusija/daria") > load("W.Rdata") > palf <- colorRampPalette(c("white", "black")) > heatmap(W,Rowv=NA,Colv=NA,col=palf(6),scale="none",margins=c(10,10)) > heatmap(W,Rowv=NA,Colv=NA,col=palf(6),scale="none",margins=c(6,6)) > library(RColorBrewer) > rc <- rainbow(nrow(n2), start = 0, end = .3) > cc <- rainbow(ncol(n2), start = 0, end = .3) > heatmap(x=n2,Rowv=NA,Colv=NA,col=col,symm=TRUE,cexRow=0.3,cexCol=0.3,RowSideColors = rc, ColSideColors = cc) > p <- as.integer(read.csv("BMstruct.per",header=FALSE,skip=1)$V1) > C <- as.integer(read.csv("BMstruct.clu",header=FALSE,skip=1)$V1) > C <- C+1 > heatmap(x=n2,Rowv=p,Colv=p,col=col,symm=TRUE,cexRow=0.3,cexCol=0.3,RowSideColors=rc,ColSideColors=cc) It seems that the built-in procedure does not use the corrected dissimilarity for clustering the network. This can be repaired by setting ''distfun=netDist'' from [[ru:7iss:labs:mi#clustering_the_migration_network]] > netDist <- function(A){ n <- nrow(A) + D <- matrix(nrow=n,ncol=n,dimnames=dimnames(A)); diag(D) <- 0 + for(v in 2:n){ + for(u in 1:(v-1)) { + d <- sum((A[v,]-A[u,])**2) - (A[v,u]-A[u,u])**2 - (A[v,v]-A[u,v])**2 + + (A[v,u]-A[u,v])**2 + (A[v,v]-A[u,u])**2 + D[v,u] <- D[u,v] <- sqrt(d) + } + } + return(as.dist(D)) + } > h <- heatmap(x=n2,distfun=netDist,Rowv=p,Colv=p,col=col,symm=TRUE, + cexRow=0.3,cexCol=0.3,RowSideColors=rc,ColSideColors=cc) > h <- heatmap(x=n2,distfun=netDist,Rowv=p,Colv=p,col=col,symm=TRUE, + cexRow=0.3,cexCol=0.3,RowSideColors=rc,ColSideColors=cc,revC=TRUE) > library(gplots) > h <- heatmap.2(x=n2,distfun=netDist,Rowv=p,Colv=p,col=col,symm=TRUE, + cexRow=0.3,cexCol=0.3,RowSideColors=rc,ColSideColors=cc, + revC=TRUE,dendrogram="row") > svg("PAJheat2.svg",width=10,height=10) > h <- heatmap.2(x=n2,distfun=netDist,Rowv=p,Colv=p,col=col,symm=TRUE, + cexRow=0.5,cexCol=0.5,RowSideColors=rc,ColSideColors=cc, + revC=TRUE,dendrogram="row",trace="none") > dev.off() > cc <- c(rep("blue",27),rep("red",79)) > svg("PAJheat2.svg",width=10,height=10) > h <- heatmap.2(x=n2,distfun=netDist,Rowv=p,Colv=p,col=col,symm=TRUE, + colsep=c(20,40,50),rowsep=c(6,14,18,25,30,36,42,47), + sepcolor="blue",sepwidth=c(0.01,0.01), + cexRow=0.5,cexCol=0.5,RowSideColors=rc,ColSideColors=cc, + revC=TRUE,dendrogram="row",trace="none") > dev.off() > D <- netDist(n2) > hc <- hclust(D) > plot(hc,hang=-1,cex=0.2) > C <- cutree(hc,k=15) > dend <- as.dendrogram(hc) > hm <- heatmap.2(n2,Rowv=dend,col=col,symm=TRUE,cexRow=0.3,cexCol=0.3,revC=TRUE,trace="none") > P <- C[hm$rowInd] > Q <- c() > for(i in 1:105) if(P[i]!=P[i+1]) Q <- c(Q,i) > hm <- heatmap.2(n2,Rowv=dend,col=col,symm=TRUE, + ColSideColors=cc,cexRow=0.3,cexCol=0.3,# keysize=30, + colsep=Q,rowsep=Q,sepcolor="blue",sepwidth=c(0.01,0.01), + main="Collaboration among Russian SNA", + key=FALSE,dendrogram="row",revC=TRUE,trace="none") ===== Structural equivalence ===== > setwd("C:/Users/batagelj/Documents/2017/rusija/daria") > library(gplots) > library(xml2) > xml2utf8 <- function(str){ + t <- xml2::xml_text(xml2::read_xml(paste0("", str, ""))) + Encoding(t) <- "UTF-8" + return (t) + } > netDist <- function(A){ n <- nrow(A) + D <- matrix(nrow=n,ncol=n,dimnames=dimnames(A)); diag(D) <- 0 + for(v in 2:n){ + for(u in 1:(v-1)) { + d <- sum((A[v,]-A[u,])**2) - (A[v,u]-A[u,u])**2 - (A[v,v]-A[u,v])**2 + + (A[v,u]-A[u,v])**2 + (A[v,v]-A[u,u])**2 + D[v,u] <- D[u,v] <- sqrt(d) + } + } + return(as.dist(D)) + } > # load("W.Rdata") > N <- unlist(lapply(rownames(n2),xml2utf8)) > rownames(n2) <- N; colnames(n2) <- N > n <- length(N) > cc <- c(rep("blue",27),rep("red",79)) > col<- colorRampPalette(c("white", "black"))(4) > D <- netDist(n2) > hc <- hclust(D) > plot(hc,hang=-1,cex=0.2) > C <- cutree(hc,k=15) > dend <- as.dendrogram(hc) > hm <- heatmap.2(n2,Rowv=dend,col=col,symm=TRUE,cexRow=0.3,cexCol=0.3,revC=TRUE,trace="none") > P <- C[hm$rowInd] > Q <- c() > for(i in 1:(n-1)) if(P[i]!=P[i+1]) Q <- c(Q,i) > hm <- heatmap.2(n2,Rowv=dend,col=col,symm=TRUE, + ColSideColors=cc,cexRow=0.3,cexCol=0.3, + colsep=Q,rowsep=Q,sepcolor="blue",sepwidth=c(0.01,0.01), + main="Collaboration among Russian SNA", + key=FALSE,dendrogram="row",revC=TRUE,trace="none") > > svg("PAJheat2.svg",width=14,height=14) > hm <- heatmap.2(n2,Rowv=dend,col=col,symm=TRUE, + ColSideColors=cc,cexRow=0.65,cexCol=0.65, + colsep=Q,rowsep=Q,sepcolor="blue",sepwidth=c(0.01,0.01), + main="Collaboration among Russian SNA", + key=FALSE,dendrogram="row",revC=TRUE,trace="none") > dev.off() > col = c("khaki1","khaki3","khaki4","black") > hm <- heatmap.2(n2,Rowv=dend,col=col,symm=TRUE, + ColSideColors=cc,cexRow=0.3,cexCol=0.3, + colsep=Q,rowsep=Q,sepcolor="blue",sepwidth=c(0.01,0.01), + main="Collaboration among Russian SNA", + key=FALSE,dendrogram="row",revC=TRUE,trace="none") > svg("PAJheat2b.svg",width=14,height=14) > hm <- heatmap.2(n2,Rowv=dend,col=col,symm=TRUE, + ColSideColors=cc,cexRow=0.85,cexCol=0.85, + colsep=Q,rowsep=Q,sepcolor="blue",sepwidth=c(0.01,0.01), + lwid=c(0.5,4),lhei=c(0.5,4),margins=c(8,8), + key=FALSE,dendrogram="row",revC=TRUE,trace="none") > dev.off() {{notes:pdf:pajheat2.pdf}} {{notes:pdf:pajheat2k.pdf}} {{notes:pdf:pajheat2b.pdf}} ===== URLs ===== - [[http://digitheadslabnotebook.blogspot.si/2011/06/drawing-heatmaps-in-r.html?m=1|Drawing heatmaps in R]], June 24, 2011 - NATHAN YAU: [[https://flowingdata.com/2010/01/21/how-to-make-a-heatmap-a-quick-and-easy-solution/|How to Make a Heatmap – a Quick and Easy Solution]], Jan 21, 2010 - Sebastian Raschka: [[http://sebastianraschka.com/Articles/heatmaps_in_r.html|A short tutorial for decent heat maps in R]], Dec 8, 2013 - [[https://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/|ggplot2: Quick Heatmap Plotting]], Jan 26, 2010 - Dave Tang: [[https://davetang.org/muse/2010/12/06/making-a-heatmap-with-r/|Making a heatmap with R]], Dec 6, 2010 - J. HARRY CAUFIELD: [[http://harrycaufield.net/severalog/2016/3/1/heatmaps-in-r-two-ways|Heatmaps in R, two ways]], March 2, 2016 - [[http://brazenly.blogspot.si/2014/07/r-various-methods-heatmapheatmap2levelp.html|R : Various methods (heatmap,heatmap.2,levelplot,ggplot) to draw a heatmap]], 30 July 2014 - [[https://www.rdocumentation.org/packages/gplots/versions/3.0.1/topics/heatmap.2|heatmap.2]], from gplots v3.0.1 - [[https://www.r-bloggers.com/r-for-more-powerful-clustering/|R for more powerful clustering]], April 21, 2015 - [[https://stackoverflow.com/questions/15351575/moving-color-key-in-r-heatmap-2-function-of-gplots-package|Moving color key in R heatmap.2]], Mar 12, 2013 - [[http://mannheimiagoesprogramming.blogspot.si/2012/06/drawing-heatmaps-in-r-with-heatmap2.html|Drawing heatmaps in R with heatmap.2]], June 3, 2012 - A. Bailey: [[http://rstudio-pubs-static.s3.amazonaws.com/98032_acc2fdbb8d134d438b1beca8a42d4d62.html|A short R heatmap tutorial]], 29 July 2015 - Peter Cock: [[http://www2.warwick.ac.uk/fac/sci/moac/people/students/peter_cock/r/heatmap/|Using R to draw a Heatmap from Microarray Data]] - Jeremy Yoder: [[http://www.molecularecologist.com/2013/08/making-heatmaps-with-r-for-microbiome-analysis/|Making heatmaps with R for microbiome analysis]], 20 August, 2013 by - [[https://plot.ly/r/heatmaps/|Plotly Heatmaps in R]] - Shilin Zhao: [[https://cran.r-project.org/web/packages/heatmap3/vignettes/vignette.pdf|Introduction for heatmap3 package]], April 6, 2015 - https://cran.r-project.org/web/packages/heatmaply/vignettes/heatmaply.html - [[http://www.geo.ut.ee/aasa/LOOM02331/heatmap_in_R.html|Creating heatmaps in R with ggmap]] - http://blog.revolutionanalytics.com/2017/02/superheat-supercharged-heatmaps-for-r.html - http://www.ryanpraski.com/r-heatmap-tutorial-for-google-analytics/ - http://wiki.bits.vib.be/index.php/Use_pheatmap_to_draw_heat_maps_in_R - https://artax.karlin.mff.cuni.cz/r-help/library/seriation/html/hmap.html - http://compbio.ucsd.edu/making-heat-maps-r/ - Zuguang Gu:[[https://bioconductor.statistik.tu-dortmund.de/packages/3.1/bioc/vignettes/ComplexHeatmap/inst/doc/ComplexHeatmap.html|Making Complex Heatmaps]], 2015-04-16; [[https://bioconductor.org/packages/devel/bioc/vignettes/ComplexHeatmap/inst/doc/s9.examples.html|More Examples of Making Complex Heatmaps]], 2017-04-24 - Markus Gesmann: [[https://www.r-bloggers.com/using-svg-graphics-in-blog-posts/|Using SVG graphics in blog posts]], February 9, 2016 - Jeroen Ooms: [[https://www.r-bloggers.com/the-rsvg-package-high-quality-image-rendering-in-r/|The ‘rsvg’ Package: High Quality Image Rendering in R]], January 24, 2016; [[https://cran.r-project.org/web/packages/rsvg/index.html|CRAN/Rsvg]] - Stack Overflow: [[https://stackoverflow.com/questions/6886990/how-to-easily-visualize-a-matrix|How to easily visualize a matrix?]], May 2011 - https://datascience.stackexchange.com/questions/17370/options-available-for-visualizing-a-matrix-type-data-frame-in-r - Chris Seidel: [[http://www.phaget4.org/R/image_matrix.html|Plotting a table of numbers as an image using R]] - Taiyun Wei: [[https://uploads.cosx.org/2009/12/An-Introduction-to-Matrix-Visualization-and-corrplot-Package.pdf|An Introduction to Matrix Visualization & corrplot Package]], December 2009 - http://kateto.net/network-visualization - Matthew Lincoln: [[https://matthewlincoln.net/2014/12/20/adjacency-matrix-plots-with-r-and-ggplot2.html|Adjacency matrix plots with R and ggplot2]], 20 Dec 2014