library(circlize)
## ========================================
## circlize version 0.4.12
## CRAN page: https://cran.r-project.org/package=circlize
## Github page: https://github.com/jokergoo/circlize
## Documentation: https://jokergoo.github.io/circlize_book/book/
## 
## If you use it in published research, please cite:
## Gu, Z. circlize implements and enhances circular visualization
##   in R. Bioinformatics 2014.
## 
## This message can be suppressed by:
##   suppressPackageStartupMessages(library(circlize))
## ========================================
dataURL <- "https://pastebin.com/raw/whLr21ZA"
expData.df <- read.table(dataURL,header=TRUE,sep="\t",stringsAsFactors=FALSE)
expData.mat <- as.matrix(expData.df[-c(1)])
rownames(expData.mat) <- expData.df$Mol
head(expData.mat)
##      S1    S2
## F1 2.30 -5.64
## F2 1.01  4.69
## F3 0.37 -2.81
## F4 0.24 -2.08
## F5 0.35 -0.74
## F6 1.92 -3.66
dim(expData.mat)
## [1] 292   2
expData.mat <- expData.mat[1:50,]
useT.mat <- t(expData.mat)
mat_list = useT.mat #changed to fit 1 heatmap
dim(useT.mat)
## [1]  2 50
head(mat_list)
##       F1   F2    F3    F4    F5    F6    F7    F8    F9   F10   F11   F12  F13
## S1  2.30 1.01  0.37  0.24  0.35  1.92  0.18 -0.55  0.28 -1.31 -0.89  1.37 1.43
## S2 -5.64 4.69 -2.81 -2.08 -0.74 -3.66 -7.97 -2.68 -0.80 -0.74 -0.80 -4.32 0.65
##      F14   F15   F16   F17    F18   F19   F20   F21   F22   F23   F24   F25
## S1  1.53  1.60 -1.06  2.23   1.11  0.46 -0.04  2.34  1.38  0.24  0.88  1.43
## S2 -2.86 -7.16 -8.97 -7.16 -13.29 -1.19 -5.88 -7.16 -0.16 -5.72 -1.47 -4.61
##      F26   F27   F28   F29   F30   F31   F32   F33   F34   F35   F36   F37
## S1  0.03 -3.61 -3.61 -0.04  0.47 -2.29  1.33 -2.47  0.42 -1.64 -4.16 -4.21
## S2 -3.21 -1.58  1.79 -2.98 -0.84 -3.63 -2.68 -5.27 -1.41 -5.06 -3.86 -0.06
##      F38    F39   F40   F41  F42   F43    F44    F45   F46   F47    F48   F49
## S1 -1.15  -4.24 -1.87 -2.12  1.1  1.58  -4.47   2.44 -0.58 -3.56  -1.45 -0.84
## S2 -5.51 -13.29 -1.68 -4.44 -5.8 -1.93 -13.29 -13.29 -5.01 -0.55 -13.29 -4.24
##      F50
## S1 -2.04
## S2 -2.62
dend_list = as.dendrogram(hclust(dist(t(mat_list)))) #changed to calculate for 1 matrix
plot(dend_list,horiz = F)

#View(mat_list)
class(mat_list)
## [1] "matrix" "array"
max(mat_list)
## [1] 4.69
min(mat_list)
## [1] -13.29
col_fun = colorRamp2(breaks= c(-14, 0, 5), 
                     colors = c("darkgreen","white", "darkred")) 

#removed transparency so easier to save in EPS, very optional
#excluded 'factor' as there is only 1 heatmap

circos.par("start.degree" = 90,cell.padding = c(0, 0, 0, 0), gap.degree = 15) 
circos.initialize("a", xlim =c(0,nrow(expData.mat))) #changed to 1 heatmap setting
#adding new track for column label
circos.track(ylim = c(0, 1), bg.border = NA, track.height = 0.05, 
             panel.fun = function(x, y) {
               for(i in seq_len(ncol(useT.mat))) {
                 circos.text(i-0.5, 0, colnames(useT.mat)[order.dendrogram(dend_list)][i], adj = c(0, 0.5), 
                             facing = "clockwise", niceFacing = TRUE,
                             cex = 0.5)                
               }
             })

circos.track(ylim = c(0, 2), bg.border = NA, panel.fun = function(x, y) {
  m = mat_list 
  dend = dend_list
  #changed variable for 1 heatmap setting
  m2 = m[, order.dendrogram(dend)]
  col_mat = col_fun(m2)
  nr = nrow(m2)
  nc = ncol(m2)
  for(i in 1:nr) {
    circos.rect(1:nc - 1, rep(nr - i, nc), 
                1:nc, rep(nr - i + 1, nc), 
                border = col_mat[i, ], col = col_mat[i, ])
  }
  #adding row label
  circos.text(rep(1, 2), 1:2, 
              rownames(useT.mat), 
              facing = "downward", adj = c(2.5, 1.1), cex = 0.7) 
  
})
#Dendrogram
max_height = attr(dend_list, "height") #changed for 1 dendrogram setting
circos.track(ylim = c(0, max_height), bg.border = NA, track.height = 0.3, 
             panel.fun = function(x, y) {
               dend = dend_list
               circos.dendrogram(dend, max_height = max_height)
             })
circos.clear()

#adding legend key
library(ComplexHeatmap)
## Loading required package: grid
## ========================================
## ComplexHeatmap version 2.4.3
## Bioconductor page: http://bioconductor.org/packages/ComplexHeatmap/
## Github page: https://github.com/jokergoo/ComplexHeatmap
## Documentation: http://jokergoo.github.io/ComplexHeatmap-reference
## 
## If you use it in published research, please cite:
## Gu, Z. Complex heatmaps reveal patterns and correlations in multidimensional 
##   genomic data. Bioinformatics 2016.
## 
## This message can be suppressed by:
##   suppressPackageStartupMessages(library(ComplexHeatmap))
## ========================================
lgd_links = Legend(at=c(-15,-5,0,5),col_fun = col_fun, 
                   title_position = "topleft", title = "Value", direction = "horizontal")
draw(lgd_links, x = unit(1, "npc") - unit(2, "mm"), y = unit(4, "mm"), 
     just = c("right", "bottom"))

##############ref https://stackoverflow.com/questions/59059826/circular-heatmap-with-r-circlize-plot-area-and-row-labels