3

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

ahajib
  • 12,838
  • 29
  • 79
  • 120

2 Answers2

2

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

lukeA
  • 53,097
  • 5
  • 97
  • 100
-1
    library(dplyr);library(purrr);library(ggplot2)
data.plot %>% 
    group_by(Class) %>% 
    do(model = kmeans(.[c('x', 'y')], 1)) %>% 
    ungroup() %>% group_by(Class) %>% do(map_df(.$model, broom::tidy)) %>% ungroup() %>% 
    select(Class,x,y ) %>% data.frame() %>% dplyr::rename(x.center=x,y.center=y,Class=Class) ->label.data
  • It's not clear how this answer solves the problem that the question poses. What is `data.plot`? What is the output of this code? How is it used to label the regions? Narrative text/documentation as to what is happening and why is needed. – Brian Diggs Jul 11 '22 at 22:51