R:树状图的颜色分支,同时保留颜色图例

R: Color branches of dendrogram while preserving the color legend

Objective:创建一个树状图,其中分支由一个因子变量着色,结果图包含一个图例,用于将分支颜色转换为每个因子变量值。

我有一些带有因子变量的数据,后面是我正在创建树状图的数值数据:

> cleaned_mayo[1:5,1:20]
          patient                Source         Tissue RIN Diagnosis Gender  AgeAtDeath ApoE   FLOWCELL PMI N_unmapped N_multimapping N_noFeature N_ambiguous ENSG00000223972
1924_TCX 1924_TCX MayoBrainBank_Dickson TemporalCortex 5.6   Control      F 90_or_above   33 AC5R6PACXX   2    2773880        9656114     8225967     2876479               1
1926_TCX 1926_TCX MayoBrainBank_Dickson TemporalCortex 7.8   Control      F          88   33 AC44HKACXX   2    2279283       12410116     9503353     3600252               2
1935_TCX 1935_TCX MayoBrainBank_Dickson TemporalCortex 8.6   Control      F          88   33 AC5T2GACXX   3    3120169        8650081     9640468     4603751               0
1925_TCX 1925_TCX MayoBrainBank_Dickson TemporalCortex 6.6   Control      F          89   33 BC6178ACXX   4    2046886       10627577     7533671     3361385               1
1963_TCX 1963_TCX MayoBrainBank_Dickson TemporalCortex 9.7   Control      M 90_or_above   33 AC5T1WACXX   4    1810116        9611375     5343437     2983079               2
         ENSG00000227232 ENSG00000278267 ENSG00000243485 ENSG00000274890 ENSG00000237613
1924_TCX              80               7               1               0               0
1926_TCX             113              22               9               0               0
1935_TCX             181              21               2               0               0
1925_TCX              75               9               5               0               0
1963_TCX              73              14               1               0               0

数据维度为:161 x 60,739。有了这些数据,我得到了一个带有彩色分支但没有图例的树状图,以及一个带有彩色标签(不是分支)和图例的树状图。我想把两者结合起来。

创建带有彩色分支但没有图例的树状图:

# Create the dendrogram for visualization
dend_expr<- cleaned_mayo[,15:60739] %>% # Isolate expression data
                  scale %>% # Normalize
                  dist  %>% # Compute distance measure
                  hclust %>% # Cluster hierarchically
                  as.dendrogram()

# Arrange labels in order with tree
tree_labels<- cleaned_mayo[order.dendrogram(dend_expr),]

# Color branches by diagnosis
dend_expr<- assign_values_to_leaves_edgePar(dend_expr, value= tree_labels$Diagnosis, edgePar= "col") %>%
            as.ggdend()

# Plot dendrogram
ggplot(dend_expr, horiz= T, theme= NULL, labels= F) +
  ggtitle("Mayo Cohort: Hierarchical Clustering of Patients Colored by Diagnosis")

创建带有彩色标签(不是分支)和图例的树状图:

# Create the dendrogram for visualization
dend_expr<- cleaned_mayo[,15:60739] %>% # Isolate expression data
                  scale %>% # Normalize
                  dist  %>% # Compute distance measure
                  hclust %>% # Cluster hierarchically
                  as.dendrogram()

tree_labels<- dendro_data(dend_expr, type = "rectangle")
tree_labels$labels<- merge(x= tree_labels$labels, y= cleaned_mayo, by.x= "label", by.y= "patient")

ggplot() +
  geom_segment(data=segment(tree_labels), aes(x=x, y=y, xend=xend, yend=yend)) +
  geom_text(data = label(tree_labels), aes(x=x, y=y, label=label, colour = Diagnosis, hjust=0), size=3) +
  #geom_point(data = label(tree_labels), aes(x=x, y=y), size=2, shape = 21) +
  coord_flip() +
  scale_y_reverse(expand=c(0.2, 0)) +
  scale_colour_brewer(palette = "Dark2") + 
  theme_dendro() +
  ggtitle("Mayo Cohort: Hierarchical Clustering of Patients Colored by Diagnosis")

各自输出的例子: Colored branches; Colored labels with legend.

感谢任何帮助。谢谢!

下面是一个关于如何实现所需着色的示例:

library(tidyverse)
library(ggdendro)
library(dendextend)

一些数据:

matrix(rnorm(1000), ncol = 10) %>%
  scale %>% 
  dist  %>% 
  hclust %>% 
  as.dendrogram() -> dend_expr

tree_labels<- dendro_data(dend_expr, type = "rectangle")
tree_labels$labels <- cbind(tree_labels$labels, Diagnosis = as.factor(sample(1:2, 100, replace = T)))

剧情:

ggplot() +
  geom_segment(data = segment(tree_labels), aes(x=x, y=y, xend=xend, yend=yend))+
  geom_segment(data = tree_labels$segments %>%
                 filter(yend == 0) %>%
                 left_join(tree_labels$labels, by = "x"), aes(x=x, y=y.x, xend=xend, yend=yend, color = Diagnosis)) +
  geom_text(data = label(tree_labels), aes(x=x, y=y, label=label, colour = Diagnosis, hjust=0), size=3) +
  coord_flip() +
  scale_y_reverse(expand=c(0.2, 0)) +
  scale_colour_brewer(palette = "Dark2") + 
  theme_dendro() +
  ggtitle("Mayo Cohort: Hierarchical Clustering of Patients Colored by Diagnosis")

关键在我做的第二个 geom_segment 调用中:

tree_labels$segments %>%
     filter(yend == 0) %>%
     left_join(tree_labels$labels, by = "x")

过滤所有叶子 yend == 0 并通过 x

左连接 tree_labels$labels