0

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

Adam Quek
  • 6,973
  • 1
  • 17
  • 23
PesKchan
  • 868
  • 6
  • 14
  • 1
    I do not see any straightforward way to do this. You should contact the package maintainer: `maintainer("FactoMineR")` for the email address. It appears that the plot function (f.draw.tree is called by plot.HCPC) does not provide the option to change the plotted groups from cluster number to some other variable. – dcarlson Jul 14 '22 at 22:14
  • Thank you for your quick response Dr @dcarlson although when I see this `res.hcpc$data.clust` i can see the column labelled..but i was wonder how the paper did the labeling this paper https://www.nature.com/articles/s41416-019-0404-y#Sec15 the authou did show the code but the data was missing to reproduce the same – PesKchan Jul 14 '22 at 22:33

1 Answers1

1

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.

Adam Quek
  • 6,973
  • 1
  • 17
  • 23
  • upto that I did get somehow ..is that any way instead of adding cluster is it possible to label them for example Species which species they belong to of course inside a cluster it can have mix of species too depending upon the cluster ..instead of `rownames` I would like to add that species column part in the toy example – PesKchan Jul 16 '22 at 15:51
  • https://www.nature.com/articles/s41416-019-0404-y#Sec15 this was the paper I did see the code but he is doing the same as you have done but instead of cluster he is getting group of factor label – PesKchan Jul 16 '22 at 15:54
  • 1
    Are you asking about changing the legend? If so, [here](https://stackoverflow.com/questions/59976007/factominer-how-to-change-the-text-of-in-hcpc-plot) is @dcarlson alternate solution. I have tried to change the hcpc object by changing the clust name to the species, e.g. `levels(hc$desc.axes$call$X$clust) <- c("setosa", "versicolor", "virginica")`. However, the plot did not reflect, these changes. I will try to take another look to see if there are additional arguments that the plotting method can take. – Adam Quek Jul 16 '22 at 16:38
  • 1
    Also, the way the nature paper is presented, it looks like the plot has been edited, so that the legends are added manually with another graphical program. Submitted code to a paper need not (and are usually unable to) replicate the intricacies of graphical representation. – Adam Quek Jul 16 '22 at 16:40
  • let me try the one you gave .. – PesKchan Jul 16 '22 at 16:53
  • "Also, the way the nature paper is presented, it looks like the plot has been edited, s" wow i thought it was rendered since the author gave the code it is the same as you have given here..so i thought there they implemented someway to do that – PesKchan Jul 16 '22 at 16:54
  • "I will try to take another look to see if there are additional arguments that the plotting method can take. " I would love to see that..thank you for the quick response – PesKchan Jul 16 '22 at 16:55
  • `levels(hc$call$X$clust) <- c("setosa", "versicolor", "virginica") plot(hc, choice="3D.map")` when I run this this doesn't work as it shows earlier ..plot so the level only works for `choice=map` – PesKchan Jul 16 '22 at 17:33
  • okay its coming from that `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)` this..thank i think i need more than enough r skiil to fork it right now for my dataset whatever solution you have given me so far is more more than good enough to put in paper – PesKchan Jul 16 '22 at 17:40
  • 1
    Good luck for your paper! – Adam Quek Jul 16 '22 at 17:41