0

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!

star
  • 743
  • 1
  • 7
  • 19

1 Answers1

3


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

s__
  • 9,270
  • 3
  • 27
  • 45
  • @ nihil, Thanks alot for your command, buy I do not want all individuals in plot, I like to have col names in PCA (4_rep1,4_rep2 and ...) and PCA1 ,PCA2 and PCA 3 as X,Y and Z axes. – star Jun 11 '18 at 10:16
  • @star, so you need variables as dots and their weights on PCA1,2,3 as coordinates? Is it possible to have a reproducible example of your data? Something that it's possible to copy and paste in R. – s__ Jun 11 '18 at 10:17
  • thanks a lot. Yes I like to have col names as variable an PCA as coordinates. 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") – star Jun 11 '18 at 11:16
  • @ star, I've edited the answer with your data and request, try to see if it fits! – s__ Jun 11 '18 at 12:06