Search code examples
rplotcolorscolorbrewer

How to find corresponding color based on dataframe using Setnames


I have the following data frame:

dat <- structure(list(TPR = c(0.081, 0.945, 0.953, 0.984, 0.956, 0.031
), FPR = c(0.081, 0.545, 0.606, 0.869, 0.636, 0.01), Classifier = structure(c(1L,
3L, 3L, 3L, 3L, 4L), .Label = c("Luck", "NN", "RF", "SVM"), class = "factor"),
    Mean_AUC = c(0.5, 0.91, 0.91, 0.91, 0.91, 0.764), Classifier_with_mean_AUC = c("Luck(0.500)",
    "RF(0.910)", "RF(0.910)", "RF(0.910)", "RF(0.910)", "SVM(0.764)"
    )), .Names = c("TPR", "FPR", "Classifier", "Mean_AUC", "Classifier_with_mean_AUC"
), row.names = c(309L, 155L, 161L, 187L, 164L, 2L), class = "data.frame")

It looks like this:

      TPR   FPR Classifier Mean_AUC Classifier_with_mean_AUC
309 0.081 0.081       Luck    0.500              Luck(0.500)
155 0.945 0.545         RF    0.910                RF(0.910)
161 0.953 0.606         RF    0.910                RF(0.910)
187 0.984 0.869         RF    0.910                RF(0.910)
164 0.956 0.636         RF    0.910                RF(0.910)
2   0.031 0.010        SVM    0.764               SVM(0.764)

What I want to do is to determine the corresponding color for Classifier_with_mean_AUC.

library(RColorBrewer)
colors = brewer.pal(7, "Dark2")[1:7]
colors<-setNames(colors[1:nlevels(dat$Classifier_with_mean_AUC)], levels(dat$Classifier_with_mean_AUC))
colors

At the end of that code it only produces one color #1B9E77, I expected it to produce 3 colors. How can do it correctly?

I expect it to produce something like this.

Luck(0.500)      RF(0.910)       SVM(0.764)
"#1B9E77"        "#D95F02"       "#7570B3" 

Solution

  • with levels, you need to be using factors. Currently your Classifier_with_mean_AUC is a character column.

    library(RColorBrewer)
    colors = brewer.pal(7, "Dark2")[1:7]
    
    ## using character values
    setNames(colors[1:NROW(unique(dat$Classifier_with_mean_AUC))], unique(dat$Classifier_with_mean_AUC))
    # Luck(0.500)   RF(0.910)  SVM(0.764) 
    #  "#1B9E77"   "#D95F02"   "#7570B3" 
    
    ## to use levels, convert your column to factor
    dat$Classifier_with_mean_AUC <- as.factor(dat$Classifier_with_mean_AUC)
    setNames(colors[1:nlevels(dat$Classifier_with_mean_AUC)], levels(dat$Classifier_with_mean_AUC))
    # Luck(0.500)   RF(0.910)  SVM(0.764) 
    #  "#1B9E77"   "#D95F02"   "#7570B3"