0

I'm a complete beginner to R and have this question. I'm using the following code to generate a colour list and then create a massive scatterplot matrix. I want to assign specific colours to the first column of my matrix(categorical with 4 categories). Running this code works fine but how do I verify that the colours that I intend to specify for each of the categorical variables is correct?

Basically I want to achieve green for 'control', orange for 'low', brown for 'medium' and black for 'high'.

col.list<-c("green","orange","brown","black")

palette(col.list)

pairs(Indices[,4:17], col=Indices[,1])

Thank you for any help!

zero323
  • 322,348
  • 103
  • 959
  • 935
VGu
  • 386
  • 5
  • 23

1 Answers1

1

The way you're doing it is correct. If you want to check that indeed the colours correspond to your group, you can, for example do it that way (here with a reproducible example):

set.seed(1)
a <- data.frame(Group=factor(sample(c("control","low","medium","high"),20,TRUE),
                             levels= c("control","low","medium","high")),
                x=rnorm(20),y=rnorm(20))
col.list <- c("green","orange","brown","black")
palette(col.list)
pairs(a[,2:3], col=a[,1])

What col=a[,1] does is actually palette()[a[,1]] (which works IF the content of the column is a factor or an integer), so let's see:

palette()[a[,1]]
[1] "orange" "orange" "brown"  "black"  "green"  "black"  "black"  "brown"  "brown"  "green"  "green"  "green"  "brown"  "orange"
[15] "black"  "orange" "brown"  "black"  "orange" "black" 

table(a[,1], palette()[a[,1]])
         black brown green orange
  control     0     0     4      0
  low         0     0     0      5
  medium      0     5     0      0
  high        6     0     0      0

The only thing you really have to worry about is that the content of Indices[,1] is a factor whose levels are ordered in the same order as the corresponding color list.

plannapus
  • 18,529
  • 4
  • 72
  • 94
  • Hi Plannapus, thank you for your response. I understand the logic however I'm unable to replicate it with my data. For some odd reason the high is getting coloured as orange. Am I missing something obvious? – VGu Nov 10 '13 at 08:13
  • Have you check that the order of your levels match? (when you type `Indices[1,1]` for instance you should see your first element but also a list of levels in a specific order. If the order doesn't match the order of your colors, either change the order of your colors :) or reorder your levels using function `relevel` for instance. – plannapus Nov 11 '13 at 08:01
  • Thank you Plannapus. I realised the orders were not in sync with one another! Cheers. – VGu Nov 21 '13 at 02:47