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)
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.
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!
Typically, you would specify the color in a (base) plotting function via the col argument.
pairs() function in R language is used to return a plot matrix, consisting of scatter plots corresponding to each data frame.
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With