Search code examples
rwaffle-chart

Assign hexcode to color for factor in a dataframe


data dput data subset

which looks like this

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

I would like to add a column to it which can assign color to each pathway terms.

In my dataframe I have dim(d2) [1] 190 7 and the levels present

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

The objective of assigning colors is I want to use this dataframe for waffle chart

I tried this link couldn't make it work. Any suggestion would be really appreciated


Solution

  • I am having trouble with the link to your data. Would you mind checking that it is working?

    You could use one of these functions to generate hex codes for each factor level

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

    Here's an approach that comes to mind

    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")