#Heatmap code starts here data1 <- data.frame(data, row.names = "Sites") df <- scale(data1) head(data1) #install.packages("heatmaply") library(ggplot2) library(pheatmap) library(ggforce) library("RColorBrewer") # Default plot dev.off() heatmap(df, scale = "none") library("ComplexHeatmap") library(grid) verbose = ht_opt("verbose") dev.off() Heatmap(df, name = "Key") Heatmap(df, name = "mat1", col = f1, column_title = "LAB color space") Heatmap(df, name = "mat2", col = f2, column_title = "RGB color space") # Use LAB and RGB colour pellets library(circlize) f1 = colorRamp2(seq(min(df), max(df), length = 3), c("blue", "#EEEEEE", "red")) f2 = colorRamp2(seq(min(df), max(df), length = 3), c("blue", "#EEEEEE", "red"), space = "RGB") #Use clolur pallet with even # of columns and even # of rows in dendrogram {library("RColorBrewer") col <- colorRampPalette(brewer.pal(10, "RdYlBu"))(256) heatmap(df, scale = "none", col = col, RowSideColors = rep(c("blue", "pink"), each = 2),# divide number of rows by 2 ColSideColors = c(rep("purple", 9), rep("orange", 9))) # divide number of colums equally in two parts } #Use blue and Red colour pallet {library("gplots") heatmap.2(df, scale = "none", col = bluered(20), trace = "none", density.info = "none") } dev.off() # Use custom colours {library(circlize) col_fun = colorRamp2(c(-2, 0, 2), c("green", "white", "red")) col_fun(seq(-3, 3)) Heatmap(df, name = "Sites", col = col_fun) } #Use other types of coloured cells library("RColorBrewer") Heatmap(df, name = "df", col = col_fun, column_title = "df") Heatmap(df/4, name = "df", col = col_fun, column_title = "df/4") Heatmap(abs(df), name = "df", col = col_fun, column_title = "abs(df)") #Separate outliars {df2 = df df2[1, 1] = 100000 Heatmap(df2, name = "df", col = col_fun, column_title = "a dfrix with outliers") } #Create heatmap in soft blue/red colours {library("RColorBrewer") Heatmap(df, name = "Key", col = colorRamp2(c(-2, 0, 2), brewer.pal(n=3, name="RdBu"))) } library(ComplexHeatmap) pheatmap(df, legend = TRUE, display_numbers = TRUE) pheatmap(df, legend = TRUE, display_numbers = matrix(ifelse(df > 0.25, "*", ""), nrow(df))) pheatmap(df, legend = TRUE, cellwidth = 15, cellheight = 12, main = "") pheatmap(df, name = "Sites", col = colorRampPalette(c("navy", "white", "firebrick3"))(50)) pheatmap(df, name = "Sites", row_km = 3, row_gap = unit(3, "mm")) pheatmap(df, name = "Sites", column_km = 3, column_gap = unit(3, "mm")) pheatmap(df, name = "Sites", row_km = 3, row_gap = unit(3, "mm"), column_km = 3, column_gap = unit(3, "mm")) pheatmap(df, name = "Scale", row_split = 3, row_gap = unit(3, "mm"), column_split = 3, column_gap = unit(3, "mm"), column_title = " ", column_title_side = "top", row_title = " ", row_title_side = "left" ) #Combine pheatmaps p1 = pheatmap(df, name = "mat1") p2 = rowAnnotation(foo = anno_barplot(1:nrow(df))) p3 = pheatmap(df, name = "mat2", col = colorRampPalette(c("navy", "white", "firebrick3"))(50)) # or you can simply specify as # p3 = pheatmap(test, name = "mat2", col = c("navy", "white", "firebrick3")) p1 + p2 p1 + p2 + p3 # Order for columns: We must transpose the data in data file and reupload {library(gplots) library(dendextend) Colv <- data1 %>% scale %>% t %>% dist %>% hclust %>% as.dendrogram %>% set("branches_k_color", k = 2, value = c("orange", "blue")) %>% set("branches_lwd", 2) %>% ladderize heatmap(scale(data1), Rowv = Rowv, Colv = Colv, scale = "none") } # Create heatmap with red blue pallet and with coloured custom line width {library(gplots) heatmap.2(scale(data1), scale = "none", col = bluered(50), Rowv = Rowv, Colv = Colv, trace = "none", density.info = "none") } #Create Red/blue heatmap with custom row and column font sizes {library(grid) library(ComplexHeatmap) library(circlize) Heatmap(df, name = "Key", #title of legend column_title = "Parameters", row_title = "Sites", row_names_gp = gpar(fontsize = 10), # Text size for row names column_names_gp = gpar(fontsize = 10) # Text size for row names ) } #Crete heatmap with custom box colour and font sizes {library(circlize) mycols <- colorRamp2(breaks = c(-2, 0, 2), colors = c("green", "white", "orange")) Heatmap(df, name = "Key", col = mycols) } #Red/blue heatmap with coloured dendrogram {library(dendextend) library(gplots) row_dend = hclust(dist(df)) # row clustering col_dend = hclust(dist(t(df))) # column clustering Heatmap(df, name = "Key", row_names_gp = gpar(fontsize = 10), cluster_rows = color_branches(row_dend, k = 4), cluster_columns = color_branches(col_dend, k = 4)) } # Label row cluster 1 and 2 {set.seed(2) Heatmap(df, name = "Key", k = 2) } # split by a vector specifying rowgroups Heatmap(df, name = "Key", split = data$Sites, row_names_gp = gpar(fontsize = 8)) # Split by combining multiple variables {Heatmap(df, name ="data", split = data.frame(Sites = data$Sites, Sites = data$Sites), row_names_gp = gpar(fontsize = 8)) Heatmap(df, name ="data", km = 2, split = data$Sites) } # Use custom names of clusters {library("cluster") set.seed(2) pa = pam(df, k = 3) Heatmap(df, name = "data", split = paste0("pam", pa$clustering)) } # A heatmap with Violin, boxplot, density and histogram {.hist = anno_histogram(df, gp = gpar(fill = "lightblue")) .density = anno_density(df, type = "line", gp = gpar(col = "blue")) ha_mix_top = HeatmapAnnotation( hist = .hist, density = .density, height = unit(3.8, "cm") ) .violin = anno_density(df, type = "violin", gp = gpar(fill = "lightblue"), which = "row") .boxplot = anno_boxplot(df, which = "row") ha_mix_right = HeatmapAnnotation(violin = .violin, bxplt = .boxplot, which = "row", width = unit(4, "cm")) Heatmap(df, name = "key", column_names_gp = gpar(fontsize = 10), row_names_gp = gpar(fontsize = 10), top_annotation = ha_mix_top) + ha_mix_right } # Combine 2 or more heatmaps {ht1 = Heatmap(df, name = "ht1", km = 2, column_names_gp = gpar(fontsize = 10)) # Heatmap 2 ht2 = Heatmap(df, name = "ht2", col = circlize::colorRamp2(c(-2, 0, 2), c("green", "white", "red")), column_names_gp = gpar(fontsize = 10)) # Combine the two heatmaps ht1 + ht2 draw(ht1 + ht2, row_title_gp = gpar(col = "red"), column_title_side = "bottom", # Gap between heatmaps gap = unit(0.5, "cm")) } #Create a density map {densityHeatmap(scale(data1)) } #rainbow heatmap {colorRamp2(seq(min(df), max(df), length = 10), rev(rainbow(10))) Heatmap(df, name = "df", col = rev(rainbow(10)), column_title = "set a color vector for a continuous dfrix") } # Use blanks (shown as black) forNA values {data1 <- data.frame(data, row.names = "Sites") #takes the dataset called WLR1 and uses Gen column as row names df <- scale(data1) df_with_na = df na_index = sample(c(TRUE, FALSE), nrow(df)*ncol(df), replace = TRUE, prob = c(1, 9)) df_with_na[na_index] = NA Heatmap(df_with_na, name = "df", na_col = "black", column_title = "a dfrix with NA values") } # heatmap borders Heatmap(df, name = "df", border_gp = gpar(col = "black", lty = 2), column_title = "set heatmap borders") # empty heatmap Heatmap(df, name = "df", rect_gp = gpar(type = "none"), column_title = "nothing is drawn in the heatmap body") # Change row dendrogram colour with numbering {dend = hclust(dist(df)) dend = color_branches(dend, k = 2) Heatmap(df, name = "Sites", cluster_rows = dend, row_split = 2) } # Change row dendrogram colour without numbering {library(dendextend) row_dend = as.dendrogram(hclust(dist(df))) row_dend = color_branches(row_dend, k = 2) # `color_branches()` returns a dendrogram object Heatmap(df, name = "df", cluster_rows = row_dend) } # Use one custom colour in row dendrogram Heatmap(df, name = "df", cluster_rows = row_dend, row_dend_gp = gpar(col = "red")) #Use row nodes on dendrogram row_dend = dendrapply(row_dend, function(d) { attr(d, "nodePar") = list(cex = 0.8, pch = sample(20, 1), col = rand_color(1)) return(d) }) Heatmap(df, name = "df", cluster_rows = row_dend, row_dend_width = unit(2, "cm")) # Reorder row and colomn Heatmap(df, name = "df", row_order = order(as.numeric(gsub("row", "", rownames(df)))), column_order = order(as.numeric(gsub("column", "", colnames(df)))), column_title = "reorder dfrix") #Reorder dfrix by rown and column names Heatmap(df, name = "df", row_order = sort(rownames(df)), column_order = sort(colnames(df)), column_title = "reorder dfrix by row/column names") ## Setting row and column titles # Set row and coloumn titles Heatmap(df, name = "df", column_title = "I am a column title", row_title = "I am a row title") # Column title at bottom Heatmap(df, name = "df", column_title = "I am a column title at the bottom", column_title_side = "bottom") # Big column title Heatmap(df, name = "df", column_title = "I am a big column title", column_title_gp = gpar(fontsize = 20, fontface = "bold")) # Horizontal row title Heatmap(df, name = "df", row_title = "row title", row_title_rot = 0) # row title would be cluster_1 and cluster_2 Heatmap(df, name = "df", row_km = 2, row_title = "cluster_%s") # Colour Filled column title Heatmap(df, name = "df", column_title = "I am a column title", column_title_gp = gpar(fill = "red", col = "white", border = "blue")) # Column title with different boarder colour ht_opt$TITLE_PADDING = unit(c(8.5, 8.5), "points") Heatmap(df, name = "df", column_title = "I am a column title", column_title_gp = gpar(fill = "red", col = "white", border = "blue")) #Show equation in title ht_opt(RESET = TRUE) Heatmap(df, name = "df", column_title = expression(hat(beta) == (X^t * X)^{-1} * X^t * y)) # Reverse row and column titles and dendrogram Heatmap(df, name = "df", row_names_side = "left", row_dend_side = "right", column_names_side = "top", column_dend_side = "bottom") #Omit rown or column names Heatmap(df, name = "df", show_row_names = FALSE) #Change row or colum font Heatmap(df, name = "df", row_names_gp = gpar(fontsize = 20)) #Change row lable colours according to groups Heatmap(df, name = "df", row_names_gp = gpar(col = c(rep("red", 2), rep("blue", 2)))) #Control row and colum clustring Heatmap(df, name = "df", row_names_centered = TRUE, column_names_centered = TRUE) #Rotate colum labels Heatmap(df, name = "df", column_names_rot = 45) Heatmap(df, name = "df", column_names_rot = 45, column_names_side = "top", column_dend_side = "bottom") ## Spliting Cells and dendrogram #Separate cells Heatmap(df, name = "Sites", rect_gp = gpar(col = "white", lwd = 2), column_title = "set cell borders") # Separate row clusters dostance and write row cluster number Heatmap(df, name = "Sites", row_km = 3, row_gap = unit(5, "mm")) # Separate column clusters dostance and write column cluster number Heatmap(df, name = "sites", column_km = 3, column_gap = unit(5, "mm")) #Use different gaps in row clustring Heatmap(df, name = "df", row_km = 3, row_gap = unit(c(2, 4), "mm")) #Use custom gaps in row and column clusting Heatmap(df, name = "df", row_km = 3, row_gap = unit(c(2, 4), "mm"), column_km = 3, column_gap = unit(c(2, 4), "mm")) # Separate rown and column clusters dostance and write row column cluster number Heatmap(df, name = "Sites", row_km = 3, row_gap = unit(5, "mm"), column_km = 3, column_gap = unit(5, "mm")) #Split in rows and columns into blocks Heatmap(df, name = "Sites", row_split = 2, column_split = 3) #Split alongwith cluster names of rows pa = cluster::pam(df, k = 2) Heatmap(df, name = "df", row_split = paste0("pam", pa$clustering)) # Row and column split Heatmap(df, name = "df", row_split = 2, column_split = 5) ## Change Clustrering properties # Turn off row clustring Heatmap(df, name = "sites", cluster_rows = FALSE) # turn off row clustering # Turn off column clustring Heatmap(df, name = "sites", show_column_dend = FALSE) # hide column dendrogram # Clustering with coloum and row labels Heatmap(df, name = "Sites", row_dend_side = "right", column_dend_side = "bottom") #Change dendro column width Heatmap(df, name = "df", column_dend_height = unit(2, "cm"), row_dend_width = unit(2, "cm")) #No reordering Heatmap(df, name = "Sites", row_dend_reorder = FALSE, column_title = "no reordering") #Apply reordering Heatmap(df, name = "Sites", row_dend_reorder = TRUE, column_title = "apply reordering") # Adjust height of branches Heatmap(df, name = "df", row_km = 2, column_km = 3, show_parent_dend_line = FALSE) #Group cluters colours ht_opt$TITLE_PADDING = unit(c(4, 4), "points") Heatmap(df, name = "df", row_km = 2, row_title_gp = gpar(col = c("red", "blue"), font = 1:2), row_names_gp = gpar(col = c("green", "orange"), fontsize = c(10, 14)), column_km = 3, column_title_gp = gpar(fill = c("red", "blue", "green"), font = 1:3), column_names_gp = gpar(col = c("green", "orange", "purple"), fontsize = c(10, 14, 8))) #Use slice border colours Heatmap(df, name = "df", row_km = 2, column_km = 3, border = TRUE) #No gap but slice border colours Heatmap(df, name = "df", row_km = 2, column_km = 3, row_gap = unit(0, "mm"), column_gap = unit(0, "mm"), border = TRUE) ###Heatmap annotations Heatmap(df, name = "df", row_km = 2, column_km = 3, top_annotation = HeatmapAnnotation(foo1 = 1:41, bar1 = anno_points(runif(41))), right_annotation = rowAnnotation(foo2 = 4:1, bar2 = anno_barplot(runif(4))) ) ### Add values to heatmap library(circlize) library("ComplexHeatmap") df = df[1:9, 1:9] col_fun = colorRamp2(c(-2, 0, 2), c("green", "white", "red")) Heatmap(df, name = "df", col = col_fun, cell_fun = function(j, i, x, y, width, height, fill) { grid.text(sprintf("%.1f", df[i, j]), x, y, gp = gpar(fontsize = 10)) }) #Show only positive values in heatmap Heatmap(df, name = "df", col = col_fun, cell_fun = function(j, i, x, y, width, height, fill) { if(df[i, j] > 0) grid.text(sprintf("%.1f", df[i, j]), x, y, gp = gpar(fontsize = 10)) }) #split heatmap into slices alongwith values Heatmap(df, name = "df", col = col_fun, row_km = 3, column_km = 3, cell_fun = function(j, i, x, y, width, height, fill) { grid.text(sprintf("%.1f", df[i, j]), x, y, gp = gpar(fontsize = 10)) }) Heatmap(df, name = "df", col = col_fun, row_km = 2, column_km = 2, layer_fun = function(j, i, x, y, width, height, fill, slice_r, slice_c) { v = pindex(df, i, j) grid.text(sprintf("%.1f", v), x, y, gp = gpar(fontsize = 10)) if(slice_r != slice_c) { grid.rect(gp = gpar(lwd = 2, fill = "transparent")) } }) #Join cells if values in the neighbouring two columns have the same sign Heatmap(df, name = "mat", col = col_fun, row_km = 2, column_km = 2, layer_fun = function(j, i, x, y, w, h, fill) { # restore_matrix() is explained after this chunk of code ind_mat = restore_matrix(j, i, x, y) for(ir in seq_len(nrow(ind_mat))) { # start from the second column for(ic in seq_len(ncol(ind_mat))[-1]) { ind1 = ind_mat[ir, ic-1] # previous column ind2 = ind_mat[ir, ic] # current column v1 = df[i[ind1], j[ind1]] v2 = df[i[ind2], j[ind2]] if(v1 * v2 > 0) { # if they have the same sign col = ifelse(v1 > 0, "darkred", "darkgreen") grid.segments(x[ind1], y[ind1], x[ind2], y[ind2], gp = gpar(col = col, lwd = 2)) grid.points(x[c(ind1, ind2)], y[c(ind1, ind2)], pch = 16, gp = gpar(col = col), size = unit(4, "mm")) } } } } ) #Clustering based on coefficients Heatmap(df, name = "mat", clustering_distance_rows = "pearson", column_title = "pre-defined distance method (1 - pearson)") Heatmap(df, name = "mat", clustering_distance_rows = function(m) dist(m), column_title = "a function that calculates distance matrix") Heatmap(df, name = "mat", clustering_distance_rows = function(x, y) 1 - cor(x, y), column_title = "a function that calculates pairwise distance") # Lable even number of rows and columns with letters Heatmap(df, name = "mat", row_split = rep(LETTERS[1:3], 6), # number of groups and units in each group column_split = rep(letters[1:6], 4)) # number of groups and units in each group # clustering is similar as previous heatmap with branches in some nodes in the dendrogram flipped Heatmap(df, name = "mat", row_split = factor(rep(LETTERS[1:3], 6), levels = LETTERS[3:1]), column_split = factor(rep(letters[1:6], 4), levels = letters[6:1])) # The order is exactly what we set Heatmap(df, name = "mat", row_split = factor(rep(LETTERS[1:3], 6), levels = LETTERS[3:1]), column_split = factor(rep(letters[1:6], 4), levels = letters[6:1]), cluster_row_slices = FALSE, cluster_column_slices = FALSE) # Name slices horizontally Heatmap(df, name = "mat", row_split = 3, row_title = c("top_slice", "middle_top_slice", "bottom_slice"), row_title_rot = 0) #Give Title to row slices Heatmap(df, name = "mat", row_split = 3, row_title = "there are three slices") #colurfull heatmap with box and violin plots {library(ComplexHeatmap) library(circlize) library(colorspace) library(GetoptLong) ha_mix_top = HeatmapAnnotation(histogram = anno_histogram(df, gp = gpar(fill = rep(2:3, each = 5))), density_line = anno_density(df, type = "line", gp = gpar(col = rep(2:3, each = 5))), violin = anno_density(df, type = "violin", gp = gpar(fill = rep(2:3, each = 5))), heatmap = anno_density(df, type = "heatmap")) ha_mix_right = HeatmapAnnotation(histogram = anno_histogram(df, gp = gpar(fill = rep(2:3, each = 5)), which = "row"), density_line = anno_density(df, type = "line", gp = gpar(col = rep(2:3, each = 5)), which = "row"), violin = anno_density(df, type = "violin", gp = gpar(fill = rep(2:3, each = 5)), which = "row"), heatmap = anno_density(df, type = "heatmap", which = "row"), which = "row", width = unit(8, "cm")) Heatmap(df, name = "foo", top_annotation = ha_mix_top, height = unit(8, "cm")) + ha_mix_right } #Split in two blocks Heatmap(df, name = "foo", km = 2) # Split in two blocks (Works only on even number of rows) Heatmap(df, name = "foo", split = rep(c("A", "B"), 5)) Heatmap(df, name = "foo", split = data.frame(rep(c("A", "B"), 6), rep(c("C", "D"), each = 6))) Heatmap(mat, name = "foo", split = data.frame(rep(c("A", "B"), 6), rep(c("C", "D"), each = 6)), combined_name_fun = function(x) paste(x, collapse = "\n")) Heatmap(mat, name = "foo", km = 2, split = rep(c("A", "B"), 6), combined_name_fun = function(x) paste(x, collapse = "\n")) Heatmap(mat, name = "foo", km = 2, split = rep(c("A", "B"), 6), combined_name_fun = NULL) ht1 = Heatmap(df, name = "ht1") ht2 = Heatmap(df, name = "ht2") class(ht1) class(ht2) ht1 + ht2