0
votes

I am plotting a graph, but instead of getting the points on the graph, I would like the graph to show which cluster corresponds to those points. The graph I made can be seen in the attachment. So, instead of the graph containing points, I would like it to have the numbers corresponding to the clusters. I left another image as an example of what I want more or less. This example chart contains the numbers of the corresponding clusters. I am using hierarchical clustering. The code is below. Thanks for any help.

library(readxl)
complete_data <- read_excel('C:/Users/Jovani Souza/Word/Cluster/test2.xlsx') 
nproperty<-dim(complete_data)[2]

coordinates<- complete_data [,1:2] #matrix containing latitude and longitude
d<-dist(coordinates)
fit.average<-hclust(d,method="average") 

#########varying the value of k
mean<-matrix (nrow= nproperty-2,ncol=2)
standard_deviation<-matrix(nrow=npropriedades-2,ncol=2)

for(k in 2:nproperty-1){
clusters<-cutree(fit.average, k) # set the number of k clusters
nclusters<-matrix(table(clusters))  #to indicate how many properties are in each cluster

########inserting column with determination of clusters
complete_data$cluster <- clusters 

#########calculate center of mass
mass_center<-matrix(nrow=k,ncol=2)
for(i in 1:k){
mass_center[i,]<-c(weighted.mean(subset(complete_data,cluster==i)$Latitude,subset(complete_data,cluster==i)$Production),
weighted.mean(subset(complete_data,cluster==i)$Longitude,subset(complete_data,cluster==i)$Production))}

coordinates$cluster<-clusters #including clusters index
mass_center<-cbind(mass_center,matrix(c(1:k),ncol=1)) #including clusters index

###############calculation of clusters coverage, considered as the largest distance between properties and center of mass
coverage<-matrix(nrow=k,ncol=1)
for(i in 1:k){
aux_dist<-pdist(rbind(subset(coordinates, cluster==i),mass_center[i,]))
coverage[i,]<-max(aux_dist[nclusters[i,1]+1,])}

###########Production sum of the clusters
sum_production<-matrix(nrow=k,ncol=1)
for(i in 1:k){
sum_production[i,]<-sum(subset(complete_data,cluster==i)["Production"])
}

###########mean of coverage and biogás
mean[k-1,]<-c(mean(coverage),mean(sum_production)) #ver como nomear colunas
standard_deviation[k-1,]<-c(sd(coverage),sd(sum_production))
}

colnames(mean)<-c("Coverage","Production")
colnames(standard_deviation)<-c("Coverage","Production")

plot(mean)

enter image description here

enter image description here

1

1 Answers

2
votes

You did not provide a minimal reproducible example, but below is some mock data to demonstrate one way to achieve this:

complete_data <- mtcars[, c("mpg", "disp")]
hc <- hclust(dist(complete_data), method="average")
vec <- factor(stats::cutree(hc, k=3))
with(complete_data, plot(disp ~ mpg, type = "n"))
with(complete_data, text(disp ~ mpg, label=vec, col=c("red", "green", "blue")[vec]))

Edit: Per request: plotting only a subset of points, based on clustering:

complete_data <- mtcars[, c("mpg", "disp")]
hc <- hclust(dist(complete_data), method="average")
complete_data$vec <- factor(stats::cutree(hc, k=10))
with(subset(complete_data, vec %in% 2:8), plot(disp ~ mpg, type = "n"))
with(subset(complete_data, vec %in% 2:8), text(disp ~ mpg, label=vec, col=c("red", "green", "blue")[vec]))

Created on 2020-03-18 by the reprex package (v0.3.0)