How to show annotation in ComplexHeatmap in R

Viewed 265

I have 31 samples,and get 31 type of samples, such "a","b","c","d",but when i draw heatmap,it say the subscript out.I have check the columns of the matrix is 31 ,which is the same as the number of types.

ha1 = HeatmapAnnotation( bar = c("b","d","d","b","b","c","b","a","b"
                     ,"b","a","d","b","b","c","c","a","b"
                     ,"d","c","c","d","b","b","b","d","a"
                     ,"c","c","b","c"),
col = list(bar = c("a" = "red", "b" = "green", "c" = "blue","d"="black"))
 )
ha = rowAnnotation(foo = anno_mark(at = seq(0,7000,1000), labels = seq(0,7000,1000)))
hist_major <- Heatmap(major,
                  column_title = "Statistics for a list of SNP.Frequency.major",
                  column_title_gp = gpar(fontsize = 15, fontface = "bold"),
                  column_names_gp = gpar(fontsize = 10),
                  row_title = "Genome Postion(bp)",
                  row_title_gp = gpar(fontsize = 10),
                  name = "Variant.Frequency", 
                  cluster_rows = FALSE,
                  column_order=order(as.numeric(gsub("SAMN041259", "", colnames(major)))),
                  show_column_dend = FALSE,
                  right_annotation = ha,
                  bottom_annotation = ha1,
                  show_row_names = FALSE,
                  use_raster= TRUE,
                  raster_resize_mat = max,
                  col = cols,
                  border_gp = gpar(col = "black", lty = 2)
)
1 Answers

This is a simple example that works with your code. Maybe it can help you.

library(ComplexHeatmap)

# Data matrix
major <- matrix(rnorm(3100),ncol=31)
colnames(major) <- paste0("Sample",1:31)

# Your code
ha1 = HeatmapAnnotation( bar = c("b","d","d","b","b","c","b","a","b"
                                 ,"b","a","d","b","b","c","c","a","b"
                                 ,"d","c","c","d","b","b","b","d","a"
                                 ,"c","c","b","c"),
                         col = list(bar = c("a"="red", "b"="green", "c"="blue", "d"="black"))
)
ticks <- seq(0,100,10)
ticks[1] <- 1
ha = rowAnnotation(foo = anno_mark(at = ticks, labels = ticks))
cols <- heat.colors(20)

hist_major <- Heatmap(major,
                      column_title = "Statistics for a list of SNP.Frequency.major",
                      column_title_gp = gpar(fontsize = 15, fontface = "bold"),
                      column_names_gp = gpar(fontsize = 10),
                      row_title = "Genome Postion(bp)",
                      row_title_gp = gpar(fontsize = 10),
                      name = "Variant\nFrequency", 
                      cluster_rows = FALSE,
                      column_order=order(as.numeric(gsub("SAMN041259", "", colnames(major)))),
                      show_column_dend = FALSE,
                      right_annotation = ha,
                      bottom_annotation = ha1,
                      show_row_names = FALSE,
                      use_raster= TRUE,
                      raster_resize_mat = max,
                      col = cols,
                      border_gp = gpar(col = "black", lty = 2)
)
print(hist_major)

enter image description here

Related