Use ComplexHeatmap to display calculator results in multiple columns

For calculators with a large number of components like Eurogenes K36 or MDLP K27, maybe a heatmap will be more clear. And a heatmap also has the advantage that you can easily see which components have small percentages like 0% or 1%.

If you have a heatmap with a large number of columns and rows, then I think it's easier to read if you have row and column names on all four sides of the heatmap or if you split the heatmap in multiple columns. Neither of those is supported by the pheatmap package I used earlier, but you can use the ComplexHeatmap package instead:

In order to reorder the columns of the heatmap, I did an MDS of the FST matrix of the calculator and I ordered the components based on their position in the first dimension of the MDS. I reordered the rows of the heatmap based on the position of the populations in a hierarchical clustering tree, where I reordered the branches of the tree so that populations with a high percentage of the first columns were placed first and populations with a high percentage of the last columns were placed last. Another approach to ordering the rows and columns would be to use the Seriation package: https://jokergoo.github.io/ComplexHe...tmap-seriation.

I also made a script which uses ComplexHeatmap to display the results of an ADMIXTURE run with multiple K values side by side: https://anthrogenica.com/showthread....l=1#post770253.Code:library(ComplexHeatmap) library(circlize) # for `colorRamp2` library(vegan) # for `reorder.hclust` library(colorspace) # for `hex` t=read.csv("https://pastebin.com/raw/0qDaQ8HL",row.names=1,check.names=F) # MDLP K27 fst=read.csv("https://pastebin.com/raw/6t0gCQVh",row.names=1) mds=cmdscale(fst,ncol(fst)-1) t2=as.matrix(t)%*%mds t=t[,order(-mds[,1])] # reorder columns based on position in first dimension of MDS matrix of FST matrix t=t[reorder(hclust(dist(t2)),as.matrix(t)%*%seq(ncol(t))^2)$order,] # order rows based on position in hierarchical clustering tree so populations with a high percentage of the first columns are placed first # t=t[order(prcomp(t2)$x[,1]),] # order rows based on position in first dimension of PCA # t=t[reorder(hclust(dist(t2)),prcomp(t2)$x[,1])$order,] # order rows based on position in hierarchical clustering tree weighed by PC1 of PCA # ser=seriation::seriate(t,method="BEA_TSP");t=t[get_order(ser,1),get_order(ser,2)] # use the `seriation` package to reorder rows and columns (this doesn't take FST into account) rows=40 mats=lapply(seq(1,nrow(t),rows),function(i)as.matrix(t[i:(i+rows-1),])) cellsize=30 png("1.png",w=length(mats)*(ncol(t)*cellsize+1000),h=rows*cellsize+1000,res=72) maps=lapply(mats,function(mat){ Heatmap( mat, show_heatmap_legend=F, show_column_names=F, show_row_names=F, cluster_columns=F, cluster_rows=F, width=ncol(mat)*unit(cellsize,"pt"), height=nrow(mat)*unit(cellsize,"pt"), top_annotation=columnAnnotation(text=anno_text(gt_render(colnames(mat),padding=unit(c(2,2,2,2),"mm")),just="left",rot=90,location=unit(0,"npc"),gp=gpar(fontsize=17))), bottom_annotation=columnAnnotation(text=anno_text(gt_render(colnames(mat),padding=unit(c(2,2,2,2),"mm")),just="left",rot=270,gp=gpar(fontsize=17))), left_annotation=rowAnnotation(text1=anno_text(gt_render(rownames(mat),padding=unit(c(2,2,2,2),"mm")),just="right",location=unit(1,"npc"),gp=gpar(fontsize=17))), right_annotation=rowAnnotation(text1=anno_text(gt_render(rownames(mat),padding=unit(c(2,2,2,2),"mm")),just="left",location=unit(0,"npc"),gp=gpar(fontsize=17))), col=colorRamp2(seq(0,100,length.out=7),hex(HSV(c(210,210,130,60,40,20,0),c(0,rep(.5,6)),1))), cell_fun=function(j,i,x,y,w,h,fill)grid.text(sprintf("%.0f",mat[i,j]),x,y,gp=gpar(fontsize=15)) ) }) draw(Reduce(`+`,maps)) dev.off() system("mogrify -gravity center -trim -border 16 -bordercolor white 1.png")