R中的空间聚类(简单示例) [英] spatial clustering in R (simple example)
本文介绍了R中的空间聚类(简单示例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个简单的data.frame
lat<-c(1,2,3,10,11,12,20,21,22,23)
lon<-c(5,6,7,30,31,32,50,51,52,53)
data=data.frame(lat,lon)
想法是根据距离找到空间聚类
The idea is to find the spatial clusters based on the distance
首先,我绘制地图(lon,lat):
First, I plot the map (lon,lat) :
plot(data$lon,data$lat)
所以很明显,我基于点的位置之间的距离具有三个聚类.
so clearly I have three clusters based in the distance between the position of points.
为此,我已经在R中尝试了以下代码:
For this aim, I've tried this code in R :
d= as.matrix(dist(cbind(data$lon,data$lat))) #Creat distance matrix
d=ifelse(d<5,d,0) #keep only distance < 5
d=as.dist(d)
hc<-hclust(d) # hierarchical clustering
plot(hc)
data$clust <- cutree(hc,k=3) # cut the dendrogram to generate 3 clusters
这给出了:
现在,我尝试绘制相同的点,但是使用群集中的颜色
Now I try to plot the same points but with colors from clusters
plot(data$x,data$y, col=c("red","blue","green")[data$clust],pch=19)
这里是结果
这不是我想要的.
实际上,我想找到类似这样的情节
Actually, I want to find something like this plot
感谢您的帮助.
推荐答案
这样的事情:
lat<-c(1,2,3,10,11,12,20,21,22,23)
lon<-c(5,6,7,30,31,32,50,51,52,53)
km <- kmeans(cbind(lat, lon), centers = 3)
plot(lon, lat, col = km$cluster, pch = 20)
这篇关于R中的空间聚类(简单示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文