1

I'm using the Complexheatmap function (or "Heatmap") in R and was wondering if there was a way to use the Bray-Curtis method in calculating col/row distance (with ward.D2 clustering method) since it's not a supported method in Complexheatmap. I need to use this function instead of heatmap.2 and pheatmap, unfortunately.

Here is some made-up fish count data (My actual data has 47 sites (rows) and 32 seasons, but I wasn't sure how to recreate that here):

data<-matrix(rpois(30,0.9),ncol=6, nrow=5)

colnames(data) <- c("2004W", "2004D", "2005W", "2005D", "2006W", "2006D")

# I tried assigning the method this way:

d1 <- vegdist(log(data+1), method = "bray") 

d2 <- vegdist(t(log(data+1)), method = "bray")

Heatmap(data,
  row_names_side = "left",
  row_dend_side = "left",
  column_names_side = "bottom",
  row_names_gp = gpar(cex=fontsize, fontface = "bold"),
  column_names_gp = gpar(cex=0.9, fontface = "bold"),
  row_dend_width = unit(4, "cm"),
  column_dend_height = unit(3, "cm"),
  rect_gp = gpar(col = "grey"),
  column_title = "Year/Season",
  column_names_rot = 35,
  row_title = "Site",
  clustering_distance_rows = d1,
  clustering_distance_columns = d2,
  clustering_method_rows = "ward.D2",
  clustering_method_columns = "ward.D2",
  row_km = 3,
  column_km = 4
  )
Martin Rüegg
  • 815
  • 9
  • 14
Nate
  • 411
  • 2
  • 10

1 Answers1

1

You should first define a function for Bray-Curtis distance calculation (bray_dist).
Then, you set clustering_distance_rows=bray_dist and clustering_distance_rows=bray_dist in Heatmap.

library(vegan)
library(ComplexHeatmap)

set.seed(1234)
data <- matrix(rpois(30,0.9),ncol=6, nrow=5)
colnames(data) <- c("2004W", "2004D", "2005W", "2005D", "2006W", "2006D")
fontsize <- 8

bray_dist <- function(x) {
  vegdist(log(x+1), method = "bray")
}

Heatmap(data, row_names_side = "left", column_names_side = "bottom", 
        row_dend_side = "left", rect_gp = gpar(col = "grey"), 
        row_names_gp = gpar(cex=fontsize, fontface = "bold"), 
        column_names_gp = gpar(cex=0.9, fontface = "bold"), 
        row_dend_width = unit(4, "cm"), column_dend_height = unit(3, "cm"), 
        column_title = "Year/Season", column_names_rot = 35, row_title = "Site", 
        clustering_distance_rows = bray_dist, clustering_distance_columns = bray_dist, 
        clustering_method_rows = "ward.D2", clustering_method_columns = "ward.D2", 
        row_km = 3, column_km = 4)

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
  • Great! And if I wanted to transform my data with the log(x+1) method (or any method), would I transform it at the start or when assigning the function, or both? As in..."Heatmap(log(data+1)..." and leave function as is, "bray_dist <- function(x) {vegdist(log(x+1), method = "bray")}..." and leave first part alone, or both? – Nate Jul 16 '21 at 12:38
  • 1
    You should trasform your data at the beginning of the analysis. In `bray_dist` use `x` instead of `log(x+1)`. I kindly ask you to accept my answer and upvote it if you find it helpful. Thank you. – Marco Sandri Jul 17 '21 at 12:04