Reputation: 13500
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:
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
Upvotes: 3
Views: 3574
Reputation: 11
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
Upvotes: -1
Reputation: 54237
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") )
Upvotes: 2