I performed and plotted a kmeans analysis in R with the following commands:

 km = kmeans(t(mat2), centers = 4)
 plotcluster(t(mat2), km$cluster)      #from library(fpc)

Here is the result from the plot: kmeans clustering

This question is related to a previous question: Previous Question

My data matrix has dimensions $291 \times 31$ (after taking the transpose by t(mat2)) What I want to know, is how can I create a mapping from each row in the matrix to a 2D point in the plot? My idea is to get the $31$ dimensional coordinates for each point in the plot and then map and compute the 2D coordinates with discrproj().For example, I see that I should be able to find the 2D center points of all clusters by calling discrproj() on the matrix given by km$centers (which has dimensions $4 \times 31$ and hence contains the coordinates for each cluster in $31$ dimensional space).

However, where is the data for the coordinates in $31$ dimensional space for every 2D point in the plot? Is this data just my $291 \times 31$ data matrix? In summary:

  1. How can I create a mapping from each row in the $291 \times 31$ data matrix to a 2D point in the plot?
  2. Where/what is the data for the coordinates in $31$ dimensional space for every 2D point in the plot

3 Answers 3


First, let's generate some example data and cluster it:

data <- rFace(1000) 
km <- kmeans(data, 6) 

Now, we can use discrproj to find an appropriate projection that separates these clusters

dp = discrproj(data, km$clustering)

The result, dp has several fields that are potentially useful. The field dp$proj contains the coordinates of the original data points, projected onto our new space. This space has the same dimensionality as the original space, but the first two dimensions separate the clusters best (which is what plotcluster actually displays)


plot(dp$proj[,1], dp$proj[,2], pch=km$cluster+48, col=km$cluster) #+48 to get labels correct


plotcluster(data, km$clustering)

Suppose you get some new points in your original space. You can project them into your new space using the basis vectors in dp$units, like this:

newpts = newdata %*% dp$units[,1:2]

That should answer your first question. Unfortunately, I think the second part is effectively unanswerable because there are infinitely many points in the 31-d space that correspond to a given point in the 2D space.


plotcluster seems to be a wrapper for several different projection methods. By default its discriminate coordinates. See the function ?discrcoord if you want the values of dcs (which seem to be named "proj" - the columns represent the dcs values for each point) and use those values for plotting if, say, you want each point labelled with the row index.

Here's a reproducible example:

mat2 <- matrix(c(runif(100, 10, 30), runif(100, 60, 100), runif(100, 300, 350)), ncol=25, byrow=T)
plotcluster(t(mat2), matkmeans <- kmeans(t(mat2), centers= 3)$cluster)  # your usual method 
dcmat <- discrcoord(t(mat2), matkmeans)
plot(dcmat$proj[,1], dcmat$proj[,2], pch="")   # scatter plot of 1st two dcs
text(dcmat$proj[,1], dcmat$proj[,2], labels=1:25)  # label points with row index

Is this what you wanted to do?


I solved my question here: Previous Question

Idea is:

  1. use discrproj to get the 2D coordinates
  2. plot these 2D coordinates using plot
  3. Then use identify

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.