jokergoo / ComplexHeatmap

Make Complex Heatmaps
https://jokergoo.github.io/ComplexHeatmap-reference/book/
Other
1.3k stars 230 forks source link

Error: annotations should have names. #779

Closed Frankshi5011 closed 3 years ago

Frankshi5011 commented 3 years ago

Hi, there, I am trying to plot a stacked barplot using the following code. I want to simplify it with a for loop. How can I name the anno_barplot() in the for loop? test.pdf Thank you for your help.

WT_RA_OIR.sub1 <- readRDS("WT_RA_OIR.sub1.rds")
WT_RA_OIR.sub1 <- UpdateSeuratObject(WT_RA_OIR.sub1)
Trackplot_genes <- c('Bcl2a1b', 'Gadd45b',  'Ier5', 'Btg2', 'Cd83', 'Il1a', 'Lpl'   ,'Csf1',    'Rps27',    'Rps29', 'Ccr1','Bmp2','Ccr2','Plac8','Ckb','Ldhb','Cdk1','Pclaf','Pkm','Fabp5','Ndufa2','Atp5e','Fh1','Idh2')
Trackplot_color <- c("#F8766D","#F8766D", "#DE8C00","#DE8C00", "#B79F00","#B79F00","#7CAE00","#7CAE00", "#00BA38","#00BA38", "#00C08B","#00C08B", "#00BFC4","#00BFC4", "#00B4F0", "#00B4F0","#619CFF","#619CFF", 
                     "#C77CFF","#C77CFF", "#F564E3","#F564E3","#FF64B0","#FF64B0")
data2use <- data.frame(FetchData(object = WT_RA_OIR.sub1, vars = Trackplot_genes, slot = "counts"), check.names = F)
metadata.df <- as.data.frame (WT_RA_OIR.sub1@meta.data)
metadata.df$orig.ident <- rownames (metadata.df)
all (rownames(data2use) == rownames(metadata.df))
all (rownames(data2use) %in% rownames(metadata.df))
data2use <- data2use %>% mutate (orig.ident = rownames(data2use))
merge_data <- merge (metadata.df[, c(1, 10:11)], data2use, by = "orig.ident")
merge_data <- merge_data %>% arrange (seurat_clusters)                                 
merge_data$seurat_clusters <- as.character(as.vector (merge_data$seurat_clusters))

par(mfrow = c(2, 2))
Hm_list = HeatmapAnnotation(Bcl2a1b = anno_barplot(merge_data$Bcl2a1b, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Bcl2a1b))))), gp = gpar (col= "#F8766D")), annotation_name_gp = (gpar (col ="#F8766D", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Gadd45b = anno_barplot(merge_data$Gadd45b, border = FALSE, height = unit(1, "cm"), axis_param = list (at=(c(0,max(as.vector (merge_data$Gadd45b))))), gp = gpar (col= "#F8766D")), annotation_name_gp = (gpar (col ="#F8766D", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Ier5 = anno_barplot(merge_data$Ier5, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Ier5))))), gp = gpar (col= "#DE8C00")), annotation_name_gp = (gpar (col ="#DE8C00", fontsize=12, fontface = c("italic", "bold")))) %v%      
  HeatmapAnnotation(Btg2 = anno_barplot(merge_data$Btg2, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Btg2))))), gp = gpar (col= "#DE8C00")), annotation_name_gp = (gpar (col ="#DE8C00", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Cd83 = anno_barplot(merge_data$Cd83, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Cd83))))), gp = gpar (col= "#B79F00")), annotation_name_gp = (gpar (col ="#B79F00", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Il1a = anno_barplot(merge_data$Il1a, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Il1a))))), gp = gpar (col= "#B79F00")), annotation_name_gp = (gpar (col ="#B79F00", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Lpl = anno_barplot(merge_data$Lpl, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Lpl))))), gp = gpar (col= "#7CAE00")), annotation_name_gp = (gpar (col ="#7CAE00", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Csf1 = anno_barplot(merge_data$Csf1, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Csf1))))), gp = gpar (col= "#7CAE00")),annotation_name_gp = (gpar (col ="#7CAE00", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Rps27 = anno_barplot(merge_data$Rps27, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Rps27))))), gp = gpar (col= "#00BA38")),annotation_name_gp = (gpar (col ="#00BA38", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Rps29 = anno_barplot(merge_data$Rps29, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Rps29))))), gp = gpar (col= "#00BA38")),annotation_name_gp = (gpar (col ="#00BA38", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Ccr1 = anno_barplot(merge_data$Ccr1, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Ccr1))))), gp = gpar (col= "#00C08B")),annotation_name_gp = (gpar (col ="#00C08B", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Bmp2 = anno_barplot(merge_data$Bmp2, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Bmp2))))), gp = gpar (col= "#00C08B")),annotation_name_gp = (gpar (col ="#00C08B", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Ccr2 = anno_barplot(merge_data$Ccr2, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Ccr2))))), gp = gpar (col= "#00BFC4")),annotation_name_gp = (gpar (col ="#00BFC4", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Plac8 = anno_barplot(merge_data$Plac8, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Plac8))))), gp = gpar (col= "#00BFC4")),annotation_name_gp = (gpar (col ="#00BFC4", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Ckb = anno_barplot(merge_data$Ckb, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Ckb))))), gp = gpar (col= "#00B4F0")),annotation_name_gp = (gpar (col ="#00B4F0", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Ldhb = anno_barplot(merge_data$Ldhb, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Ldhb))))), gp = gpar (col= "#00B4F0")),annotation_name_gp = (gpar (col ="#00B4F0", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Cdk1 = anno_barplot(merge_data$Cdk1, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Cdk1))))), gp = gpar (col= "#619CFF")),annotation_name_gp = (gpar (col ="#619CFF", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Pclaf = anno_barplot(merge_data$Pclaf, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Plac8))))), gp = gpar (col= "#619CFF")),annotation_name_gp = (gpar (col ="#619CFF", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Pkm = anno_barplot(merge_data$Pkm, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Pkm))))), gp = gpar (col= "#C77CFF")),annotation_name_gp = (gpar (col ="#C77CFF", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Fabp5 = anno_barplot(merge_data$Fabp5, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Fabp5))))), gp = gpar (col= "#C77CFF")),annotation_name_gp = (gpar (col ="#C77CFF", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Ndufa2 = anno_barplot(merge_data$Ndufa2, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Ndufa2))))), gp = gpar (col= "#F564E3")),annotation_name_gp = (gpar (col ="#F564E3", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Atp5e = anno_barplot(merge_data$Atp5e, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Atp5e))))), gp = gpar (col= "#F564E3")),annotation_name_gp = (gpar (col ="#F564E3", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Fh1 = anno_barplot(merge_data$Fh1, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Fh1))))), gp = gpar (col= "#FF64B0")),annotation_name_gp = (gpar (col ="#FF64B0", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Idh2 = anno_barplot(merge_data$Idh2, border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data$Idh2))))), gp = gpar (col= "#FF64B0")),annotation_name_gp = (gpar (col ="#FF64B0", fontsize=12, fontface = c("italic", "bold")))) %v%
  HeatmapAnnotation(Clusters=merge_data$seurat_clusters,
                  col = list(Clusters = c ("0"="#F8766D","1"="#DE8C00","2"= "#B79F00","3"= "#7CAE00","4"= "#00BA38","5"= "#00C08B","6"= "#00BFC4","7"= "#00B4F0", 
                                           "8"= "#619CFF","9"= "#C77CFF","10"= "#F564E3","11"= "#FF64B0")), show_annotation_name = TRUE)
tiff(filename = "test.tiff", width = 320, height = 320, units = "mm", res = 300, pointsize = 3.0)
draw (Hm_list)
dev.off()  
Frankshi5011 commented 3 years ago

This is the best I can get, but I could not figure out how to replace the "anno" with gene names, which is a string in Trackplot_genes[I]. The following code can plot the figure I want, but I can not label the rows with gene names. Any suggestion? Thank you very much.

`Hm_list = NULL for (i in 1:length(Trackplot_genes)) { heatmap = HeatmapAnnotation(anno = anno_barplot(merge_data[, i+3], border = FALSE, height = unit(1, "cm"),axis_param = list (at=(c(0,max(as.vector (merge_data[, i+3]))))), gp = gpar (col= Trackplot_color[i])), annotation_name_gp = (gpar (col =Trackplot_color[i], fontsize=12, fontface = c("italic", "bold")))) Hm_list = Hm_list %v% heatmap } Hm_list = Hm_list %v% HeatmapAnnotation(Clusters=merge_data$seurat_clusters, col = list(Clusters = c ("0"="#F8766D","1"="#DE8C00","2"= "#B79F00","3"= "#7CAE00","4"= "#00BA38","5"= "#00C08B","6"= "#00BFC4","7"= "#00B4F0", "8"= "#619CFF","9"= "#C77CFF","10"= "#F564E3","11"= "#FF64B0")), show_annotation_name = TRUE)

tiff(filename = "test.tiff", width = 320, height = 320, units = "mm", res = 300, pointsize = 3.0) draw (Hm_list) dev.off()`

Frankshi5011 commented 3 years ago

annotation_label did the trick.