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:

enter image description here

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") ) 

enter image description here


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -