Reputation:
I would like to add a Voronoi plot to my clusters. That is, I would like to have one plot with my clusters, centroids + the Voronoi regions. Is there a simple way to do it?
I tried:
x<-c(4,7,9,2,3,3,7,7,8,8,9,9)
y<-c(6,3,3,6,5,7,2,9,4,9,2,8)
mat<-cbind(x,y)# defining matrix
Kmeans<-kmeans(mat,centers=3) # with 3 centroids
plot(x,y,col=Kmeans$cluster,pch=19,cex=2)
points(Kmeans$centers,col=1:3,pch=3,cex=3,lwd=3)
library(tripack)
test<-voronoi.mosaic(x,y)
plot(x,y,col=Kmeans$cluster,pch=19,cex=2)
plot(test)
Here I just dont know how to combine them to produce a reasonable plot.
Upvotes: 3
Views: 433
Reputation: 37879
You mean just plot one on top of the other?
You are using the voronoi.mosaic on x and y and not on the clusters. I don't know how this will help you but to plot one on top of the other you need to do the following:
library(tripack)
x<-c(4,7,9,2,3,3,7,7,8,8,9,9)
y<-c(6,3,3,6,5,7,2,9,4,9,2,8)
mat<-cbind(x,y)# defining matrix
Kmeans<-kmeans(mat,centers=3) # with 3 centroids
test<-voronoi.mosaic(x,y)
plot(x,y,col=Kmeans$cluster,pch=19,cex=2)
points(Kmeans$centers,col=1:3,pch=3,cex=3,lwd=3)
par(new=T)
plot(test)
Initially, I thought you wanted to plot the clusters and the centroids and I did something completely different. You can check that too in the edits.
Upvotes: 2