1

I am having trouble using RGB colours present in a dataframe.

color.table
    V1  V2  V3    
    1   Red 255,0,0
    2   Orange Red  255,69,0
    3   LimeGreen   50,205,50
    4   Green   0,128,0
    5   DarkGreen   0,100,0
    6   GreenYellow 194,225,5
    7   Yellow  255,255,0
    8   Aquamarine  102,205,170
    9   PaleTurquoise   138,145,208
    10  IndianRed   205,92,92
    11  DarkSalmon  233,150,122
    12  DarkKhaki   189,183,107
    13  Silver  128,128,128
    14  Gainsboro   192,192,192
    15  White   255,255,255

It seems that I cannot use the RGB present in this dataframe.

barplot(seq(1:15) , col=color.table$V3 )
Error in rect(y1, x1, y2, x2, ...) : 
  invalid color specification "255,0,0"

Even if I try to convert the RGB in Hexadecimal value, there is something odd.

rgb(color.table$V3[1], 50, m=255)
Error in rgb(color.table$V3[1], 50, m = 255) : 
  argument "blue" is missing, with no default

Where as this works:

> rgb(255,0,0, 50, m=255)
[1] "#FF000032"

There is something fishy here, but I can't put my finger on it. Any advice much appreciated.

5
  • When you look up the documentation at ?rgb, can you explain which arguments you think you are passing values to when you run rgb(color.table$V5[1], 50, m=255)?
    – joran
    Mar 31, 2015 at 15:18
  • I think I am passing this : rgb(255,0,0, 50, m=255)
    – Benoit B.
    Mar 31, 2015 at 15:20
  • Well, color.table$V5[1] is just a single value, not three. So you've essentially done rgb(red = color.table$V5[1],green = 50,maxColorValues = 1), due to positional and then partial argument matching.
    – joran
    Mar 31, 2015 at 15:21
  • I actually did not mean anything by that. I was trying to help you get there yourself, but it wasn't working.
    – joran
    Mar 31, 2015 at 15:32
  • Done! list = unlist(strsplit(all$V5[1],",") ) then rgb(list[1], list[2], list[3], 50, m=255) [1] "#FF000032" Maybe not very elegant, but I got there. Thanks joran for getting me there...
    – Benoit B.
    Mar 31, 2015 at 15:42

1 Answer 1

2

You need to use the rgb function to extract colors from the V3 variable:

cols <- sapply(strsplit(as.character(color.table$V3), ","), function(x) {
  rgb(x[1], x[2], x[3], m=255)
})
cols
#  [1] "#FF0000" "#FF4500" "#32CD32" "#008000" "#006400" "#C2E105" "#FFFF00" "#66CDAA" "#8A91D0"
# [10] "#CD5C5C" "#E9967A" "#BDB76B" "#808080" "#C0C0C0" "#FFFFFF"

Now you can add these colors as a new column in your data frame, pass them to plotting functions through the col argument, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.