12

I would like to color points in a pairs plot based of certain row indexes. Here is the code I used for plotting 1 variable against another.

cases<-which(rownames(data_no_na) %in% colnames(tumor_data))
controls<-which(rownames(data_no_na) %in% colnames(control_data))

plot(y=range(pca[,1]),x=range(pca[,2]),type='n',xlab="Principle Component 2",ylab="Principle Component 1", main="Iterative Thresholding Sparse PCA")

points(y=pca[cases,1], x=pca[cases,2], col = 'red' )
points(y=pca[controls,1], x=pca[controls,2], col = 'blue' );

A simple pairs plot is something like:

pairs(pca[,1:3])

EDIT: EXAMPLE:

cases<-1:10
controls<-11:20

pca<-matrix(c(rnorm(3*10,0,1),rnorm(3*10,5,1)),nrow=20,ncol=3)
bdeonovic
  • 4,130
  • 7
  • 40
  • 70
  • 3
    You should give a reproducible example. – agstudy Mar 24 '13 at 14:37
  • Which means we need to see the output of either your real data or some dummy data which can illustrate the plot above. Try pasting the output of `dput( head( cases ) )` & `dput( head( controls ) )` if your data does not contain many many columns. – Simon O'Hanlon Mar 24 '13 at 14:47
  • 1
    Hi there! Please make your post reproducible by having a look at [**How to make a great reproducible example**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for us to help you. Thank you. – Arun Mar 24 '13 at 14:58
  • Thanks guys, I put up a reproducible example. – bdeonovic Mar 24 '13 at 17:31

2 Answers2

25

Something like this?

cols <- character(nrow(iris))
cols[] <- "black"

cols[iris$Species %in% c("setosa","versicolor")] <- "blue"
cols[iris$Species == "virginica"] <- "red"
pairs(iris,col=cols)
Roland
  • 127,288
  • 10
  • 191
  • 288
3

I'm not sure if @Roland 's answer works in some version, but at least in my Windows R 3.4.2, it doesn't.

The function pairs takes many arguments. Some of this are used to indicate what function to map to the diagonal, upper and lower panels. By default, it uses the plot (points) function.

This function has a parameter bg used to specify the fill color of markers that take it, like pch = 21.

Also, the color mapping can be done much more efficiently with unclass. For example, with a two-levels factor variable:

colors <- c('black', 'red')[unclass(factor_variable)]

Then, this does the magic:

pairs(data, bg=colors)
Mr. Duhart
  • 927
  • 12
  • 11
  • 1
    colors <- c('black', 'red')[unclass(factor_variable)] is a good option. But using bg=colors, did not allocate the colors to markers, col=colors did. I was using pch=19 – rahul-ahuja Feb 10 '21 at 09:30