将十六进制代码分配给数据框中因子的颜色

Assign hexcode to color for factor in a dataframe

数据输入data subset

看起来像这样

head(d2)
  drug_id  drug_name                                                          synonyms                           Pathway
1    1559 Luminespib                                AUY922, VER-52296,NVP-AUY922,  AUY Protein stability and degradation
2    1058 Pictilisib                                        GDC-0941, GDC0941, RG-7621               PI3K/MTOR signaling
3    1088 Irinotecan Camptosar, (+)-Irinotecan, Irinotecanum, irinotecan hydrochloride                   DNA replication
4    1549  Sapitinib                                                           AZD8931                    EGFR signaling
5    1558  Lapatinib                                                    Tykerb, Tyverb                    EGFR signaling
6    1050   ZM447439                                              ZM-447439, ZM 447439                           Mitosis
             targets  pubchem                               col
1              HSP90 10096043 Protein stability and degradation
2     PI3K (class 1) 17755052               PI3K/MTOR signaling
3               TOP1    60838                   DNA replication
4 EGFR, ERBB2, ERBB3 11488320                    EGFR signaling
5        EGFR, ERBB2   208908                    EGFR signaling
6       AURKA, AURKB  9914412                           Mitosis

我想在其中添加一列,可以为每个路径术语分配颜色。

在我的数据框中,我有 dim(d2) [1] 190 7 和存在的级别

unique(d2$Pathway)
 [1] Protein stability and degradation PI3K/MTOR signaling               DNA replication                  
 [4] EGFR signaling                    Mitosis                           Cell cycle                       
 [7] Other                             RTK signaling                     Apoptosis regulation             
[10] ERK MAPK signaling                Other, kinases                    WNT signaling                    
[13] Genome integrity                  Chromatin histone methylation     Chromatin other                  
[16] Metabolism                        p53 pathway                       Cytoskeleton                     
[19] Hormone-related                   Chromatin histone acetylation     IGF1R signaling                  
[22] ABL signaling                     JNK and p38 signaling            
24 Levels: ABL signaling Apoptosis regulation Cell cycle Chromatin histone acetylation ... WNT signaling

分配颜色的objective是我想将此数据框用于waffle图表

我试过了 无法正常工作。任何建议将不胜感激

我在 link 到您的数据时遇到问题。您介意检查一下它是否正常工作吗?

您可以使用其中一个函数为每个因子水平生成十六进制代码

rainbow()
heat.colors()
terrain.colors()
topo.colors()
cm.colors()

这是我想到的一种方法

lookup <- data.frame(
  color = rainbow(n = length(unique(d2$Pathway))),
  Pathway = unique(d2$Pathway))
 
# merge lookup table with original dataframe                   
d3 <- merge(d2, lookup, by = "Pathway")