ggplot2 - r ggplot show cluster labels on the plot -
i new r , trying generate series of figures clustering algorithm. right using 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 , k-means euclidean clusters")
as can see have 4 clusters results of k-means. want show text on plot. example in following image:
i need mean of each cluster (or text cluster labels) shown on in figure (for example 0.5 on green area). guess should geom_text purpose unfortunately have no idea how. appreciated.
thanks
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") )
Comments
Post a Comment