#pheatmap fraw heatmap
rm(list = ls())
library(pheatmap)
#################data
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
dim(test) #[1] 20 10
## [1] 20 10
head(test)
##          Test1      Test2     Test3       Test4    Test5      Test6    Test7
## Gene1 4.346670  0.4390334 4.2204902 -0.08586223 2.350942  1.1762784 3.116173
## Gene2 3.659478  1.8613520 2.8665830 -0.46817054 3.457590 -0.3987435 3.069913
## Gene3 3.491742 -0.7360575 2.8922225 -0.81696120 3.378185  1.4715903 4.227275
## Gene4 3.469093 -0.8283316 0.6002364  0.15905335 2.194965 -1.4236847 6.031756
## Gene5 3.567546 -0.1687894 4.1441206  1.37350655 1.308806 -0.2973308 2.122799
## Gene6 2.665930 -0.7867037 1.6955183 -1.17729223 2.053494  0.8654249 1.786659
##            Test8    Test9     Test10
## Gene1  0.8506096 2.948845  0.3320269
## Gene2 -0.4212113 2.867723  0.6474985
## Gene3 -1.2093191 2.149610 -1.4299511
## Gene4 -0.9057285 2.899553 -1.1077344
## Gene5  0.7828739 2.542864 -0.5275598
## Gene6  1.3539340 2.621057  0.1497603
####################draw plot
#1.1 default parameter
pheatmap(test,scale = "row")

#clustering_method
pheatmap(test, scale = "row", 
         clustering_distance_rows = "correlation",
         color = colorRampPalette(c("navy", "white", "firebrick3"))(50),
         legend_breaks = c(-2:2), legend_labels = c("-2A","-1A","0","1B","2B"),
         border_color = "red",
         show_rownames=T,show_colnames=T,
         treeheight_row = 50, treeheight_col = 5,
         display_numbers = TRUE,number_color = "blue",number_format = "%.1e",
         cellwidth = 25, cellheight = 12, main = "Example heatmap")

############1.2 annotation
###########column
annotation_col = data.frame(
  CellType = factor(rep(c("CT1", "CT2"), 5)), 
  Time = 1:5)
rownames(annotation_col) = paste("Test", 1:10, sep = "")
annotation_col
##        CellType Time
## Test1       CT1    1
## Test2       CT2    2
## Test3       CT1    3
## Test4       CT2    4
## Test5       CT1    5
## Test6       CT2    1
## Test7       CT1    2
## Test8       CT2    3
## Test9       CT1    4
## Test10      CT2    5
##########row
annotation_row = data.frame(
  GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6))))
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
annotation_row
##        GeneClass
## Gene1      Path1
## Gene2      Path1
## Gene3      Path1
## Gene4      Path1
## Gene5      Path1
## Gene6      Path1
## Gene7      Path1
## Gene8      Path1
## Gene9      Path1
## Gene10     Path1
## Gene11     Path2
## Gene12     Path2
## Gene13     Path2
## Gene14     Path2
## Gene15     Path3
## Gene16     Path3
## Gene17     Path3
## Gene18     Path3
## Gene19     Path3
## Gene20     Path3
pheatmap(test,
  scale = "row", 
         clustering_distance_rows = "correlation",
         color = colorRampPalette(c("navy", "white", "firebrick3"))(50),
         legend_breaks = c(-2:2), legend_labels = c("-2A","-1A","0","1B","2B"),
         border_color = "red",
         show_rownames=T,show_colnames=T,
         treeheight_row = 50, treeheight_col = 5,
         display_numbers = TRUE,number_color = "blue",number_format = "%.1e",
         cellwidth = 25, cellheight = 12, main = "Example heatmap",
  annotation_row = annotation_row,
     annotation_col = annotation_col, annotation_legend = T)

##########annotate color
ann_colors = list(
  Time = c("white", "firebrick"),
  CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
  GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)
ann_colors
## $Time
## [1] "white"     "firebrick"
## 
## $CellType
##       CT1       CT2 
## "#1B9E77" "#D95F02" 
## 
## $GeneClass
##     Path1     Path2     Path3 
## "#7570B3" "#E7298A" "#66A61E"
pheatmap(test,
         scale = "row", 
         clustering_distance_rows = "correlation",
         color = colorRampPalette(c("navy", "white", "firebrick3"))(50),
         legend_breaks = c(-2:2), legend_labels = c("-2A","-1A","0","1B","2B"),
         border_color = "red",
         show_rownames=T,show_colnames=T,
         treeheight_row = 50, treeheight_col = 5,
         display_numbers = TRUE,number_color = "blue",number_format = "%.1e",
         cellwidth = 25, cellheight = 12, main = "Example heatmap",
         annotation_col = annotation_col, annotation_legend = T,
         annotation_row = annotation_row,
         annotation_colors = ann_colors)

#########1.3 split col an row
pheatmap(test,
         scale = "row", 
         clustering_distance_rows = "correlation",
         color = colorRampPalette(c("navy", "white", "firebrick3"))(50),
         legend_breaks = c(-2:2), legend_labels = c("-2A","-1A","0","1B","2B"),
         border_color = "red",
         show_rownames= T, show_colnames=T,
         treeheight_row = 50, treeheight_col = 5,
         display_numbers = F,number_color = "blue",number_format = "%.1e",
         cellwidth = 25, cellheight = 12, main = "Example heatmap",
         annotation_col = annotation_col, annotation_legend = T,
         annotation_row = annotation_row,
         annotation_colors = ann_colors,
         cluster_rows = F, 
         gaps_row = c(3,9, 14), cutree_col = 2)

###########################1.4 label special rowname
labels_row = c("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "Il10", "Il15", "Il1b")
# labels_row
pheatmap(test, 
         scale = "row", 
         clustering_distance_rows = "correlation",
         color = colorRampPalette(c("navy", "white", "firebrick3"))(50),
         legend_breaks = c(-2:2), legend_labels = c("-2A","-1A","0","1B","2B"),
         border_color = "red",
         show_rownames= T, show_colnames= T,
         treeheight_row = 50, treeheight_col = 5,
         display_numbers = F,number_color = "blue",number_format = "%.1e",
         cellwidth = 25, cellheight = 12, main = "Example heatmap",
         annotation_col = annotation_col, annotation_legend = T,
         annotation_row = annotation_row,
         annotation_colors = ann_colors,
         cluster_rows = F, 
         gaps_row = c(3,9, 14), cutree_col = 2,
         labels_row = labels_row)

####################1.4 
aa <- pheatmap(test,scale="row")

summary(aa)
##          Length Class  Mode   
## tree_row 7      hclust list   
## tree_col 7      hclust list   
## kmeans   1      -none- logical
## gtable   6      gtable list
order_row = aa$tree_row$order  
order_col = aa$tree_col$order    
order_row;order_col
##  [1]  2  8  4  9  6  7  1  5  3 10 12 15 18 20 16 17 14 19 11 13
##  [1]  5  7  9  1  3  8  2  4  6 10
#dev.off()
####################1.5FONT SIZE and saveas pdf
#pheatmap(test, cellwidth = 15, cellheight = 12, fontsize = 8, filename = "test.pdf")