text: user285: I would like to make a 3D PCA but not sure how to label group wise which i can do for 2D PCA 
  library(ggplot2)
  test<- read.csv("NEW_RBP/TF_DISEASE.txt",header = T,row.names = 1,sep = '\t')
  
  Sample = c(rep("HSC",4),rep("Blast",11),rep("LSC",8))
  Stage=c(rep("HSC",4),rep("Blast",11),rep("LSC",8))
  
  
  df_final=data.frame(Sample,Stage)
  df_final
  
  pca_data=prcomp(t(test), center=TRUE, scale=TRUE)
  pca_data_perc=round(100*pca_data$sdev^2/sum(pca_data$sdev^2),1)
  
  df_pca_data=data.frame(PC1 = pca_data$x[,1], 
                         PC2 = pca_data$x[,2], sample = colnames(test))
  myColors <- c("red", "black", "blue2")
  sample <- as.vector(df_pca_data$sample)
  ggplot(df_pca_data, aes(PC1,PC2, colour = factor(Stage)))+#,label=rownames(t(test)))+
    geom_point(size=30)+
    geom_text(aes(label=sample),color="white",size=6,angle =0,parse = TRUE,  family="Bookman", fontface="bold")+
    labs(x=paste0("PC1 (",pca_data_perc[1],")"), y=paste0("PC2 (",pca_data_perc[2],")"))+
    theme_minimal(base_size=20) +
    theme(axis.text.x=element_text(size=rel(3), angle=90))+
    theme(axis.text.y=element_text(size=rel(3), angle=90))+
    theme(axis.title.x = element_text(colour="grey20",size=55,angle=0,hjust=.5,vjust=0,face="plain"))+
    theme(axis.title.y = element_text(colour="grey20",size=55,angle=90,hjust=.5,vjust=.5,face="plain"))+
    scale_color_manual(values=myColors)
  
  The above code give me the figure as i have attached 
  For 3D PCA I tried this 
  scores = as.data.frame(pca_data$x) 
  head(scores)
  
  plot3d(scores[,1:3], col=c(1:4), size=20, type='p', 
         xlim = c(-50,50), ylim=c(-50,50), zlim=c(-50,50))
  text3d(scores[,1]+2, scores[,2]+1, scores[,3]+1,
         texts=c(rownames(scores)), cex= 0.7, pos=3)
  
  
  I get something like this as in case of 2D PCA i can label lets say all of the HSC sample as black and so on..Im not sure how to do that for 3D as I tried using factor i couldn;t do
  Any help or suggestion would be highly appreciated 
  
  user1194: You can easily color 3D pca plots in R based on the code given below:
  library("scatterplot3d")
  colors <- c("#999999", "#E69F00", "#56B4E9") # Number of color according to the number of groups
  colors <- colors[as.numeric(iris$Species)] # you can put here the column containing the name of population or sample etc.
  pca1 <- prcomp(iris[, -5])
  s3d <-scatterplot3d(pca1$x[, 1], pca1$x[, 2],pca1$x[, 3],xlab="Sepal.length",ylab="Sepal.width", zlab="Petal.length", pch = 16,color=colors)
  legend("right", legend = levels(iris$Species),
    col =  c("#999999", "#E69F00", "#56B4E9"), pch = 16)
  
  The generated graph is given below:
  
  For further details about how to manipulate plots in 3D have a look at this site.
  Edited Response
  Using the data given below I plotted a 3D pca that may help solve your problem,
  "Col1" "Col2" "Col3" "Col4" "colend"
  "H1" 5.1 3.5 1.4 0.2 "HSC"
  "H2" 4.9 3 1.4 0.2 "HSC"
  "H3" 4.7 3.2 1.3 0.2 "HSC"
  "H4" 4.6 3.1 1.5 0.2 "HSC"
  "H5" 5 3.6 1.4 0.2 "HSC"
  "H6" 5.4 3.9 1.7 0.4 "HSC"
  "H7" 4.6 3.4 1.4 0.3 "HSC"
  "H8" 5 3.4 1.5 0.2 "HSC"
  "H9" 4.4 2.9 1.4 0.2 "HSC"
  "H10" 4.9 3.1 1.5 0.1 "HSC"
  "H11" 5.4 3.7 1.5 0.2 "HSC"
  "H12" 4.8 3.4 1.6 0.2 "HSC"
  "H13" 4.8 3 1.4 0.1 "HSC"
  "H14" 4.3 3 1.1 0.1 "HSC"
  "H15" 5.8 4 1.2 0.2 "HSC"
  "B1" 5.7 4.4 1.5 0.4 "blast"
  "B2" 5.4 3.9 1.3 0.4 "blast"
  "B3" 5.1 3.5 1.4 0.3 "blast"
  "B4" 5.7 3.8 1.7 0.3 "blast"
  "B5" 5.1 3.8 1.5 0.3 "blast"
  "B6" 5.4 3.4 1.7 0.2 "blast"
  "B7" 5.1 3.7 1.5 0.4 "blast"
  "B8" 4.6 3.6 1 0.2 "blast"
  "B9" 5.1 3.3 1.7 0.5 "blast"
  "B10" 4.8 3.4 1.9 0.2 "blast"
  "B11" 5 3 1.6 0.2 "blast"
  "B12" 5 3.4 1.6 0.4 "blast"
  "B13" 5.2 3.5 1.5 0.2 "blast"
  "B14" 5.2 3.4 1.4 0.2 "blast"
  "B15" 4.7 3.2 1.6 0.2 "blast"
  "B16" 4.8 3.1 1.6 0.2 "blast"
  "B17" 5.4 3.4 1.5 0.4 "blast"
  "B18" 5.2 4.1 1.5 0.1 "blast"
  "B19" 5.5 4.2 1.4 0.2 "blast"
  "B20" 4.9 3.1 1.5 0.2 "blast"
  "L1" 5 3.2 1.2 0.2 "LSC"
  "L2" 5.5 3.5 1.3 0.2 "LSC"
  "L3" 4.9 3.6 1.4 0.1 "LSC"
  "L4" 4.4 3 1.3 0.2 "LSC"
  "L5" 5.1 3.4 1.5 0.2 "LSC"
  "L6" 5 3.5 1.3 0.3 "LSC"
  "L7" 4.5 2.3 1.3 0.3 "LSC"
  "L8" 4.4 3.2 1.3 0.2 "LSC"
  "L9" 5 3.5 1.6 0.6 "LSC"
  "L10" 5.1 3.8 1.9 0.4 "LSC"
  "L11" 4.8 3 1.4 0.3 "LSC"
  "L12" 5.1 3.8 1.6 0.2 "LSC"
  "L13" 4.6 3.2 1.4 0.2 "LSC"
  "L14" 5.3 3.7 1.5 0.2 "LSC"
  "L15" 5 3.3 1.4 0.2 "LSC"
  
  The code is as follows:
  library("scatterplot3d")
  colors <- c("#999999", "#E69F00", "#56B4E9") # Number of color according to the number of groups
  colors <- colors[as.numeric(data.c1$colend)] # you can put here the column containing the name of population or sample etc.
  pca1 <- prcomp(data.c1[, -5]) # PCA on columns except the last column
  s3d<-scatterplot3d(pca1$x[, 1], pca1$x[, 2],pca1$x[, 3],grid=TRUE,xlab="PC1",ylab="PC2", zlab="PC3", pch = 16,color=colors)
  legend("right",legend = levels(data.c1$colend),col =  c("#999999", "#E69F00", "#56B4E9"), pch = 16,inset=-0.04,bty="n")
  text(s3d$xyz.convert(pca1$x[, 1:3]), labels = rownames(data.c1),cex= 0.7, col = "black",pos=2.5)
  
  In this way we get following plot:
  Hope this helps!!!
  
original_text: 285: <p>I would like to make a 3D PCA but not sure how to label group wise which i can do for 2D PCA <a href="https://i.stack.imgur.com/eMVAZ.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/eMVAZ.jpg" alt="I get something like this "></a></p>
  
  <pre><code>library(ggplot2)
  test&lt;- read.csv("NEW_RBP/TF_DISEASE.txt",header = T,row.names = 1,sep = '\t')
  
  Sample = c(rep("HSC",4),rep("Blast",11),rep("LSC",8))
  Stage=c(rep("HSC",4),rep("Blast",11),rep("LSC",8))
  
  
  df_final=data.frame(Sample,Stage)
  df_final
  
  pca_data=prcomp(t(test), center=TRUE, scale=TRUE)
  pca_data_perc=round(100*pca_data<span class="math-container">$sdev^2/sum(pca_data$</span>sdev^2),1)
  
  df_pca_data=data.frame(PC1 = pca_data<span class="math-container">$x[,1], 
                         PC2 = pca_data$</span>x[,2], sample = colnames(test))
  myColors &lt;- c("red", "black", "blue2")
  sample &lt;- as.vector(df_pca_data$sample)
  ggplot(df_pca_data, aes(PC1,PC2, colour = factor(Stage)))+#,label=rownames(t(test)))+
    geom_point(size=30)+
    geom_text(aes(label=sample),color="white",size=6,angle =0,parse = TRUE,  family="Bookman", fontface="bold")+
    labs(x=paste0("PC1 (",pca_data_perc[1],")"), y=paste0("PC2 (",pca_data_perc[2],")"))+
    theme_minimal(base_size=20) +
    theme(axis.text.x=element_text(size=rel(3), angle=90))+
    theme(axis.text.y=element_text(size=rel(3), angle=90))+
    theme(axis.title.x = element_text(colour="grey20",size=55,angle=0,hjust=.5,vjust=0,face="plain"))+
    theme(axis.title.y = element_text(colour="grey20",size=55,angle=90,hjust=.5,vjust=.5,face="plain"))+
    scale_color_manual(values=myColors)
  </code></pre>
  
  <p>The above code give me the figure as i have attached </p>
  
  <p>For 3D PCA I tried this </p>
  
  <pre><code>scores = as.data.frame(pca_data$x) 
  head(scores)
  
  plot3d(scores[,1:3], col=c(1:4), size=20, type='p', 
         xlim = c(-50,50), ylim=c(-50,50), zlim=c(-50,50))
  text3d(scores[,1]+2, scores[,2]+1, scores[,3]+1,
         texts=c(rownames(scores)), cex= 0.7, pos=3)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/JZ5Ro.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JZ5Ro.jpg" alt="3dPCA"></a></p>
  
  <p>I get something like this as in case of 2D PCA i can label lets say all of the HSC sample as black and so on..Im not sure how to do that for 3D as I tried using factor i couldn;t do</p>
  
  <p>Any help or suggestion would be highly appreciated </p>
   1194: <p>You can easily color 3D pca plots in R based on the code given below:</p>
  
  <pre><code>library("scatterplot3d")
  colors &lt;- c("#999999", "#E69F00", "#56B4E9") # Number of color according to the number of groups
  colors &lt;- colors[as.numeric(iris<span class="math-container">$Species)] # you can put here the column containing the name of population or sample etc.
  pca1 &lt;- prcomp(iris[, -5])
  s3d &lt;-scatterplot3d(pca1$</span>x[, 1], pca1<span class="math-container">$x[, 2],pca1$</span>x[, 3],xlab="Sepal.length",ylab="Sepal.width", zlab="Petal.length", pch = 16,color=colors)
  legend("right", legend = levels(iris$Species),
    col =  c("#999999", "#E69F00", "#56B4E9"), pch = 16)
  </code></pre>
  
  <p>The generated graph is given below:</p>
  
  <p><a href="https://i.stack.imgur.com/0pOW6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0pOW6.png" alt="Image1"></a></p>
  
  <p>For further details about how to manipulate plots in 3D have a look <a href="http://www.sthda.com/english/wiki/scatterplot3d-3d-graphics-r-software-and-data-visualization" rel="nofollow noreferrer">at this site</a>.</p>
  
  <p><strong>Edited Response</strong>
  Using the data given below I plotted a 3D pca that may help solve your problem,</p>
  
  <pre><code>"Col1" "Col2" "Col3" "Col4" "colend"
  "H1" 5.1 3.5 1.4 0.2 "HSC"
  "H2" 4.9 3 1.4 0.2 "HSC"
  "H3" 4.7 3.2 1.3 0.2 "HSC"
  "H4" 4.6 3.1 1.5 0.2 "HSC"
  "H5" 5 3.6 1.4 0.2 "HSC"
  "H6" 5.4 3.9 1.7 0.4 "HSC"
  "H7" 4.6 3.4 1.4 0.3 "HSC"
  "H8" 5 3.4 1.5 0.2 "HSC"
  "H9" 4.4 2.9 1.4 0.2 "HSC"
  "H10" 4.9 3.1 1.5 0.1 "HSC"
  "H11" 5.4 3.7 1.5 0.2 "HSC"
  "H12" 4.8 3.4 1.6 0.2 "HSC"
  "H13" 4.8 3 1.4 0.1 "HSC"
  "H14" 4.3 3 1.1 0.1 "HSC"
  "H15" 5.8 4 1.2 0.2 "HSC"
  "B1" 5.7 4.4 1.5 0.4 "blast"
  "B2" 5.4 3.9 1.3 0.4 "blast"
  "B3" 5.1 3.5 1.4 0.3 "blast"
  "B4" 5.7 3.8 1.7 0.3 "blast"
  "B5" 5.1 3.8 1.5 0.3 "blast"
  "B6" 5.4 3.4 1.7 0.2 "blast"
  "B7" 5.1 3.7 1.5 0.4 "blast"
  "B8" 4.6 3.6 1 0.2 "blast"
  "B9" 5.1 3.3 1.7 0.5 "blast"
  "B10" 4.8 3.4 1.9 0.2 "blast"
  "B11" 5 3 1.6 0.2 "blast"
  "B12" 5 3.4 1.6 0.4 "blast"
  "B13" 5.2 3.5 1.5 0.2 "blast"
  "B14" 5.2 3.4 1.4 0.2 "blast"
  "B15" 4.7 3.2 1.6 0.2 "blast"
  "B16" 4.8 3.1 1.6 0.2 "blast"
  "B17" 5.4 3.4 1.5 0.4 "blast"
  "B18" 5.2 4.1 1.5 0.1 "blast"
  "B19" 5.5 4.2 1.4 0.2 "blast"
  "B20" 4.9 3.1 1.5 0.2 "blast"
  "L1" 5 3.2 1.2 0.2 "LSC"
  "L2" 5.5 3.5 1.3 0.2 "LSC"
  "L3" 4.9 3.6 1.4 0.1 "LSC"
  "L4" 4.4 3 1.3 0.2 "LSC"
  "L5" 5.1 3.4 1.5 0.2 "LSC"
  "L6" 5 3.5 1.3 0.3 "LSC"
  "L7" 4.5 2.3 1.3 0.3 "LSC"
  "L8" 4.4 3.2 1.3 0.2 "LSC"
  "L9" 5 3.5 1.6 0.6 "LSC"
  "L10" 5.1 3.8 1.9 0.4 "LSC"
  "L11" 4.8 3 1.4 0.3 "LSC"
  "L12" 5.1 3.8 1.6 0.2 "LSC"
  "L13" 4.6 3.2 1.4 0.2 "LSC"
  "L14" 5.3 3.7 1.5 0.2 "LSC"
  "L15" 5 3.3 1.4 0.2 "LSC"
  </code></pre>
  
  <p>The code is as follows:</p>
  
  <pre><code>library("scatterplot3d")
  colors &lt;- c("#999999", "#E69F00", "#56B4E9") # Number of color according to the number of groups
  colors &lt;- colors[as.numeric(data.c1<span class="math-container">$colend)] # you can put here the column containing the name of population or sample etc.
  pca1 &lt;- prcomp(data.c1[, -5]) # PCA on columns except the last column
  s3d&lt;-scatterplot3d(pca1$</span>x[, 1], pca1<span class="math-container">$x[, 2],pca1$</span>x[, 3],grid=TRUE,xlab="PC1",ylab="PC2", zlab="PC3", pch = 16,color=colors)
  legend("right",legend = levels(data.c1<span class="math-container">$colend),col =  c("#999999", "#E69F00", "#56B4E9"), pch = 16,inset=-0.04,bty="n")
  text(s3d$</span>xyz.convert(pca1$x[, 1:3]), labels = rownames(data.c1),cex= 0.7, col = "black",pos=2.5)
  </code></pre>
  
  <p>In this way we get following plot:<a href="https://i.stack.imgur.com/Uvo31.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Uvo31.png" alt="Image2"></a></p>
  
  <p>Hope this helps!!!</p>
  