Nick Wray
2024-Sep-17 12:00 UTC
[R] Getting individual co-ordinate points in k medoids cluster
Hello I am using k medoids in R to generate sets of clusters for datasets through time. I can plot the individual clusters OK but what I cannot find is a way of pulling out the co-ordinates of the individual points in the cluster diagrams - none of the kmed$... info sets seems to be this. Beneath is an example of a k medoid prog using the built in US arrests dataset - this is not the data I am working with but it illustrates the issue easily library(factoextra) library(cluster) set.seed(170924) df <- USArrests df <- na.omit(df) df <- scale(df) kmed <- pam(df, k = 4) fviz_cluster(kmed, data = df) Thanks for any help or pointers Nick Wray [[alternative HTML version deleted]]
Sarah Goslee
2024-Sep-17 14:03 UTC
[R] Getting individual co-ordinate points in k medoids cluster
Hi Nick, If you're asking about the coordinates used in the plot produced by fviz_cluster(), you're looking in the wrong place. The 2D plot isn't anything intrinsic to the clustering. Instead, that plot is of the first two principal components. From the help: "Observations are represented by points in the plot, using principal components if ncol(data) > 2." The easiest way to get the coordinates is probably to use prcomp yourself, as fviz_cluster does. Specifically, it does: pca <- stats::prcomp(data, scale = FALSE, center = FALSE) That's also where the percentage values in the axis labels come from: those are the variance explained by each of the first two PCA axes. Sarah On Tue, Sep 17, 2024 at 8:01?AM Nick Wray <nickmwray at gmail.com> wrote:> > Hello I am using k medoids in R to generate sets of clusters for datasets > through time. I can plot the individual clusters OK but what I cannot find > is a way of pulling out the co-ordinates of the individual points in the > cluster diagrams - none of the kmed$... info sets seems to be this. > > Beneath is an example of a k medoid prog using the built in US arrests > dataset - this is not the data I am working with but it illustrates the > issue easily > > library(factoextra) > library(cluster) > set.seed(170924) > df <- USArrests > df <- na.omit(df) > df <- scale(df) > kmed <- pam(df, k = 4) > fviz_cluster(kmed, data = df) > > Thanks for any help or pointers > Nick Wray > > [[alternative HTML version deleted]] >-- Sarah Goslee (she/her) http://www.numberwright.com