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(rown
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)
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)