Analysis of the main components in the data color R

Hi everyone I have a simple question, but for which I could not get an answer in any textbook. Ive done a simple analysis of the main components in a dataset, and then built my data using a biplot.

CP <- prcomp(dat, scale. = T) summary(CP) biplot(CP) 

With this, I get a graph of the scatter of my data in terms of the first and second components. I want to separate my color data by pointing R to draw my first 20 data in red and the next 20 data in blue. I don't know how to say R to color these two datasets. Any help would be greatly appreciated. Thks! (im very new to R)

+6
source share
2 answers

Disclaimer: This is not a direct answer, but it can be customized to get the desired result.

 library(ggbiplot) data(wine) wine.pca <- prcomp(wine, scale. = TRUE) print(ggbiplot(wine.pca, obs.scale = 1, var.scale = 1, groups = wine.class, ellipse = TRUE, circle = TRUE)) 

enter image description here

+11
source

Using plot() will give you more flexibility - you can use it alone or with text() for text labels as belows (Thanks @flodel for useful comments):

 col = rep(c("red","blue"),each=20) plot(CP$x[,1], CP$x[,2], pch="", main = "Your Plot Title", xlab = "PC 1", ylab = "PC 2") text(CP$x[,1], CP$x[,2], labels=rownames(CP$x), col = col) 

However, if you want to use biplot() , try this code:

 biplot(CP$x[1:20,], CP$x[21:40,], col=c("red","blue")) 
+1
source

Source: https://habr.com/ru/post/927122/


All Articles