Search code examples
rggplot2pcabioconductorscatter3d

Draw PCA3D plot based on colnames?


I have a table like blow: I could draw simple PCA plot with 2 dimension based on below commend but I would like to have 3D PCA based on col-names?

                   4_rep1 4_rep2     8_rep1     8_rep2     7_rep1     7_rep2   3_rep1   3_rep2 

ENSG00000000003    2202    1787       2357       2257        945        977    1362      8536
ENSG00000000005      33      15         13         12         21         37      20      15          
ENSG00000000419     557     442        696        679        359        398     279     314        
ENSG00000000457     343     251        218        215        212        219     221     254         
ENSG00000000460     276     242        189        202        123        126     206     218         

plotPCA(newSeqExpressionSet(as.matrix(data),col=colors))

Thank you for any suggestion, in advance!


Solution


  • assuming you need a scatter 3 d plot with eigenvectors as axis and the weights of the variable as coordinates, you can find several options:

    # here the data
    results <-    data.frame(matrix(c(2202,33,557,1787,15,442,2357,13,696,2257,12,679),nrow=3,ncol=4))
    colnames(results) <- c("4_rep1","4_rep2","8_rep1","8_rep2")
    rownames(results) <- c("ENSG00000000003","ENSG00000000005","ENSG00000000419")
    
    # then a small transformation
    library(data.table)
    t_results <- transpose(results)
    colnames(t_results) <- rownames(results)
    rownames(t_results) <- colnames(results)
    
    #lastly the plot
    library(scatterplot3d) 
    scatterplot3d(t_results[,1],t_results[,2],t_results[,3], main="Very    simple")
    

    enter image description here

    a bit more cool:

    library(rgl)
    plot3d(results[,1]  ,results[,2],results[,3], col="red", size=3)
    

    enter image description here

    And this one:

        p <- plot_ly(t_results, x = ~ENSG00000000003, y = ~ENSG00000000005, z = ~ENSG00000000419) %>%
      add_markers() %>%
      layout(scene = list(xaxis = list(title = 'ENSG00000000003'),
                          yaxis = list(title = 'ENSG00000000005'),
                          zaxis = list(title = 'ENSG00000000419')))
    
        p
    

    enter image description here