Plotting PCA scores with color

前端 未结 1 1870
失恋的感觉
失恋的感觉 2021-01-03 09:54

I\'m doing PCA and I would like to plot first principal component vs second in R:

pca<-princomp(~.,data=data, na.action=na.omit
plot(pca$scores[,1],pca$sc         


        
相关标签:
1条回答
  • 2021-01-03 10:38

    Here are some example plots of PCA. Taken from the here.

    z1 <- rnorm(10000, mean=1, sd=1); z2 <- rnorm(10000, mean=3, sd=3); z3 <- rnorm(10000, mean=5, sd=5); z4 <- rnorm(10000, mean=7, sd=7); z5 <- rnorm(10000, mean=9, sd=9); mydata <- matrix(c(z1, z2, z3, z4, z5), 2500, 20, byrow=T, dimnames=list(paste("R", 1:2500, sep=""), paste("C", 1:20, sep=""))) 
    
    summary(pca) 
    summary(pca)$importance[, 1:6] 
    
    x11(height=6, width=12, pointsize=12); par(mfrow=c(1,2)) 
    
    mycolors <- c("red", "green", "blue", "magenta", "black") # Define plotting colors. plot(pca$x, pch=20, col=mycolors[sort(rep(1:5, 500))]) 
    
    plot(pca$x, type="n"); text(pca$x, rownames(pca$x), cex=0.8, col=mycolors[sort(rep(1:5, 500))]) 
    

    You can use pairs

    pairs(pca$x[,1:5], col = mycolors) 
    

    Plots a scatter plot for the first two principal components plus the corresponding eigen vectors that are stored in pca$rotation.

    library(scatterplot3d) 
    scatterplot3d(pca$x[,1:3], pch=20, color=mycolors[sort(rep(1:5, 500))]) 
    

    Same as above, but plots the first three principal components in 3D scatter plot.

    library(rgl); rgl.open(); offset <- 50; par3d(windowRect=c(offset, offset, 640+offset, 640+offset)); rm(offset); rgl.clear(); rgl.viewpoint(theta=45, phi=30, fov=60, zoom=1); spheres3d(pca$x[,1], pca$x[,2], pca$x[,3], radius=0.3, color=mycolors, alpha=1, shininess=20); aspect3d(1, 1, 1); axes3d(col='black'); title3d("", "", "PC1", "PC2", "PC3", col='black'); bg3d("
    

    The later creates an interactive 3D scatter plot with Open GL. The rgl library needs to be installed for this. To save a snapshot of the graph, one can use the command rgl.snapshot("test.png").

    require(GGally)
    ggpairs(pca$x[,1:5])
    
    0 讨论(0)
提交回复
热议问题