How to add a Voronoi diagram?

I would like to add the Voronoi plot to my clusters. That is, I would like to have one plot with my clusters, centroids + Voronoi region. Is there an easy way to do this?

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 don't know how to combine them to create a sane plot.

+3


source to share


1 answer


Do you mean just build one on top of the other?

You are using voronoi.mosaic on x and y, not clusters. I don't know how this will help you, but to build 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)

      



enter image description here

At first I thought you wanted to build clusters and centroids and I did something completely different. You can also check this in edit.

+2


source







All Articles