Search code examples
rpcafactominer

Adding group information to 3d plot in FactoMineR


My code this is what Im running

library(FactoMineR); library(factoextra)

res.pca <- PCA(t(data),ncp = 10, graph = FALSE)

res.hcpc <- HCPC(res.pca, graph = FALSE)

fviz_dend(res.hcpc, 
          cex = 0.7,                     # Label size
          palette = "jco",               # Color palette see ?ggpubr::ggpar
          rect = TRUE, rect_fill = TRUE, # Add rectangle around groups
          rect_border = "jco",           # Rectangle color
          labels_track_height = 0.8      # Augment the room for labels
)

tiff("plot.tiff",width=3000,height=1856,res=600)
plot(res.hcpc, choice = "3D.map",ind.names=TRUE,cex.axis=3.5,cex.symbols=3.5,xlab="",ylab="")
dev.off()

My data and metadata file

What I get is this

my output

Here One thing is I dont want to add the cluster information into my graph,I would rather like to label it in terms of my metadata info which in my case woold be FAB which is a column in my metadata file

I would like to see something like this where the samples are annotated based on the cell line groups.

How do I don't see any option in my plot function to add the same

Any suggestion or help would be really appreciated

figure a


Solution

  • Here's a toy example of how you can plot the 3D map for hierarchical clustering on principle component (HCPC).

    1. add desired individual labels as rownames
    library(FactoMineR)
    
    iris <- data(iris)
    rownames(iris) <- sprintf("A%03d", 1:150)
    head(iris)
    
         Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    A001          5.1         3.5          1.4         0.2  setosa
    A002          4.9         3.0          1.4         0.2  setosa
    A003          4.7         3.2          1.3         0.2  setosa
    A004          4.6         3.1          1.5         0.2  setosa
    A005          5.0         3.6          1.4         0.2  setosa
    A006          5.4         3.9          1.7         0.4  setosa
    
    
    1. running PCA, HCPC and plotting.
    res.pca <- PCA(iris[,1:4], graph=FALSE)
    hc <- HCPC(res.pca, nb.clust=-1)
    
    plot(hc, choice="3D.map", angle=60)
    

    enter image description here

    Update #1: Managed to change the label for 2D factor map for plot.HCPC, by adjusting the cluster level for the HCPC call object

    #before changes
    plot(hc, choice="map")
    

    enter image description here

    levels(hc$call$X$clust) <- c("setosa", "versicolor", "virginica")
    plot(hc, choice="map")
    

    enter image description here

    Note: I have changed the rownames to species name (plus row number) to help identify the grouping. Adding argument ind.names=FALSE will remove the individual label in the plot. Note also that the words "cluster" are present by default in the legend.

    Update #2: Found the source code for plot.HCPC.

    Note that the legend behavior is defined on line 54 and 55:

    for(i in 1:nb.clust) leg=c(leg, paste("cluster",levs[i]," ", sep=" "))
    legend("topleft", leg, text.col=as.numeric(levels(X$clust)),cex=0.8)
    

    What this mean is that by design, the legend will take the number of cluster created and insert the legend on the top left of the plot as "cluster 1", "cluster 2", etc.

    You may want to fork the repo, and repurpose the code.