Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coloring points in a pairs plot

Tags:

plot

r

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)
like image 880
bdeonovic Avatar asked Mar 24 '13 14:03

bdeonovic


People also ask

How do you specify colors in a plot?

To specify colors with either approach, call the desired plotting function with an output argument so you can access the individual plot objects. Then set properties on the plot object you want to change. For example, create a scatter plot with 100-point filled markers.

What does a pairs plot show?

A pairs plot allows us to see both distribution of single variables and relationships between two variables . Pair plots are a great method to identify trends for follow-up analysis and, fortunately, are easily implemented in Python!

What is color argument in graph?

Typically, you would specify the color in a (base) plotting function via the col argument.

What does Pairs () do in R?

pairs() function in R language is used to return a plot matrix, consisting of scatter plots corresponding to each data frame.


2 Answers

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)
like image 138
Roland Avatar answered Sep 21 '22 09:09

Roland


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)
like image 29
Mr. Duhart Avatar answered Sep 19 '22 09:09

Mr. Duhart