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!

4

1 回答 1

1

你这样做的方式是正确的。如果您想检查颜色是否确实与您的组相对应,例如,您可以这样做(这里有一个可重现的示例):

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

col=a[,1]实际上是什么palette()[a[,1]](如果列的内容是因子或整数,则有效),让我们看看:

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

您真正需要担心的唯一一件事是 的内容Indices[,1]是一个因子,其级别与相应的颜色列表的顺序相同。

于 2013-11-09T09:33:14.570 回答