Search code examples
rggplot2labelgeom-text

r ggplot show cluster labels on the plot


I am new to R and I am trying to generate a series of figures for my clustering algorithm. Right now I am using the following code:

ggplot(df,aes(x=V1,y=V2)) + 
geom_point(aes(colour = factor(cluster)),alpha=0.7) +
scale_colour_manual(values=c("purple", "green","orange","black")) +
ggtitle("Visualizing users and their K-Means Euclidean Clusters")

As you can see I have four clusters which are results of k-means. Now I want to show some text over my plot. For example in the following image:

enter image description here

I need the mean of each cluster (or any text like cluster labels) shown over it in this figure (for example 0.5 over the green area). I guess I should geom_text for this purpose but unfortunately I have no idea how. Any help is much appreciated.

Thanks


Solution

  • Try this

    library(ggplot2)
    cl <- kmeans(iris[, 1:2], 3, nstart = 25)
    ggplot(transform(iris[, 1:2], cl = factor(cl$cluster)), 
           aes(x = Sepal.Length, y = Sepal.Width, colour = cl)) +
      geom_point() + 
      scale_colour_manual(values=c("purple", "green","orange")) + 
      annotate("point", x = cl$centers[, 1], y = cl$centers[, 2], size = 5, colour = c("purple", "green","orange")) + 
      annotate("text", x = cl$centers[, 1], y = cl$centers[, 2], font = 2, size = 10,
               label = apply(cl$centers, 1, function(x) paste(sprintf('%02.2f', x), collapse = ",") ), 
               colour = c("purple", "green","orange") )
    

    enter image description here