R中针对K群集功能的代码细节方面需要帮助

时间:2019-06-26 08:24:21

标签: r machine-learning plot

我对编码来说相对较新,对R而言则是全新的,希望获得一些帮助。我正在尝试使我的图表具有一些其他信息,但我不确定是否要添加什么代码才能获得所需的结果。我希望每个群集在它们周围都有一个椭圆形(如附图所示),并定义了质心。如果有人可以帮助我,我将不胜感激!

我已经尝试过添加代码这样的代码,我不是100%地确定使用什么功能来显示椭圆。不幸的是,我是这个网站的新手,没有足够的观点来添加图像,因此我附加了两个示例链接来描述我在说什么。

理想图= https://www.mailman.columbia.edu/research/population-health-methods/cluster-analysis-using-k-means

当前图表= https://pierpaololucarelli.com/2018/02/09/k-means-clustering-in-simple-words/

library(ggplot2)

ggplot(data=df, aes(x=x, y=y, color=cluster )) + 
 geom_point() + 
  geom_point(data=centers, aes(x=V1,y=V2, color=’Center’)) +
 geom_point(data=centers, aes(x=V1,y=V2, color=’Center’), size=52, 
  alpha=.3, legend=FALSE)


df = read.csv("C:/Users/chelsie/Desktop/Results.csv")

df = df[,-1]

#Clustering script:

set.seed(20)
k = 15
clusters <- kmeans(df[2:3], k)

#Save the cluster number in the dataset as column 'Borough'

df$clusterId <- as.factor(clusters$cluster)
plotcluster(df[,1], df$clusterId)

library(ggplot2)
library(ggthemes)
library(ggplot2)
library(ggthemes)

ggplot(df, aes(x = X, y = Y,color = clusterId)) + 
  geom_point()+ theme_economist() + 
  scale_color_economist()
ggplot(df, aes(x = X, y = Y,color = clusterId)) + 
  geom_point()+ theme_economist() + 
  scale_color_economist()

期望的结果是想要的,但是,我希望能够使图定义质心,并在聚类数据点周围有一个圆/椭圆(我已经附上了两张照片,其中一张是我的数据看起来像而另一个就是我想要的样子)

0 个答案:

没有答案