使用具有以0为中心的坐标的clusplot绘制聚类

时间:2017-01-16 15:56:19

标签: r coordinates gis cluster-analysis k-means

我正在尝试绘制GIS坐标,特别是英国国家网格坐标,其中东边和北边重新组合: 194630000 562220000

我可以在Cluster库中使用clusplot绘制这些: clusplot(df2,k.means.fit $ cluster,main = i,color = TRUE,shade = FALSE,labels = 0,lines = 0,bty =“7”)

其中df2是我的数据框,k.means.fit是df2上K均值分析的结果。

请注意,k均值分析后的中心坐标尚未标准化:

k.means.fit$centers
#   Grid.Ref.Northing Grid.Ref.Easting
#1          206228234        581240726

但是当我绘制聚类时,所有的点都被翻译成它们以原点为中心。

我想在背景中显示地图的上下文,但除非我能够停止翻译,或者至少知道函数使用的值,否则我无法正确对齐这些。

我了解群集图旨在自动执行大量功能,这限制了自定义,但我无法找到创建类似群集图的包。

Intended plot (这是随机安排的,并且是无害的)

Actual cluster diagram

1 个答案:

答案 0 :(得分:5)

这是一种产生你所要求的东西的方法。

因为需要在(lat,lon)和图形之间进行转换 坐标(x,y)我没有使用clusplot。相反,我使用RgoogleMaps来获取背景地图并进行坐标转换。我用汽车绘制椭圆。

library(RgoogleMaps) 
library(car)

## Some setup to get the map of the Chelmsford area.
lat <- c(51.7,51.8) 
lon <- c(0.4, 0.5) 
center = c(mean(lat), mean(lon))
zoom <- 10

Chelmsford <- GetMap(center=center, zoom=zoom, maptype= "roadmap", 
destfile = "Chelsford.png")

你没有提供任何测试点,所以我编了几个。我意识到我的点比你的点更可分,但这只影响聚类算法,而不影响映射。

##  Some Test Data
MC = structure(c(51.7965309028563, 51.794104389723, 51.7908688357699, 
51.7787334409852, 51.7633572542762, 51.7674041270742, 51.7479758289189, 
51.7649760469292, 51.7447369665147, 51.7576910228736, 51.7487855082363, 
51.7601194948316, 51.754452857092, 51.7309692105151, 51.7107148897781, 
51.6977473627376, 51.7139561908073, 51.7366387945275, 51.7325891642372, 
51.7050420540348, 51.7050420540348, 51.7285391710661, 51.6677457194661, 
51.6571998818184, 51.6466515895592, 51.6377241941241, 51.6377241941241, 
51.645028557487, 51.6636899185361, 51.6580111872422, 51.6385358481586, 
51.63528914486, 51.8789546795942, 51.8571513038925, 51.8531124817854, 
51.8514968514399, 51.8676505449041, 51.8805693240155, 51.862805045846, 
51.8506890145161, 51.8345292307446, 51.8337210892835, 51.8256388769982, 
51.812704320496, 51.8232139304917, 51.8312965778826, 51.8240222604979, 
51.8135128390641, 51.8094701011681, 51.807044284361, 51.7973397115523, 
51.7803516822409, 51.7803516822409, 51.7949132419417, 51.7949132419417, 
51.7811607811046, 51.7763059702794, 51.7787334409852, 51.9007474867743, 
51.8781473356377, 51.8910630993239, 51.8757252167833, 51.8821839104485, 
51.8821839104485, 51.8595744231562, 51.8821839104485, 51.8741103983922, 
51.8660354365472, 51.8797620090535, 51.8765326042323, 51.8652278606205, 
51.8934843918728, 51.8829911819196, 0.0895846775599907, 0.109172466823018, 
0.153571455819268, 0.144430487496514, 0.140512929643877, 0.115701729910693, 
0.109172466823018, 0.0882788249424316, 0.124842698233447, 0.171853392464776, 
0.423882947649248, 0.447388294764912, 0.477422904968252, 0.45130585261751, 
0.442164884294756, 0.468281936645498, 0.502234104701436, 0.504845809936514, 
0.487869725908525, 0.430412210736963, 0.399071747916064, 0.395154190063467, 
0.520516041346943, 0.527045304434619, 0.523127746582022, 0.511375073024189, 
0.517904336111865, 0.54010383061001, 0.550550651550283, 0.55577406202044, 
0.572750146048389, 0.508763367789111, 0.513986778259268, 0.504845809936514, 
0.515292630876787, 0.537492125374932, 0.549244798932764, 0.588420377458818, 
0.587114524841299, 0.550550651550283, 0.508763367789111, 0.493093136378682, 
0.515292630876787, 0.485258020673487, 0.508763367789111, 0.504845809936514, 
0.652407155718095, 0.669383239746084, 0.668077387128565, 0.644572040012901, 
0.640654482160303, 0.640654482160303, 0.643266187395342, 0.606702314104326, 
0.608008166721885, 0.619760840279717, 0.626290103367393, 0.594949640546534, 
0.162712424142022, 0.156183161054346, 0.194052886962881, 0.182300213405049, 
0.212334823608389, 0.217558234078545, 0.220169939313624, 0.238451875959131, 
0.25542795998708, 0.259345517839678, 0.27109819139751, 0.28546257019042, 
0.284156717572901, 0.295909391130693, 0.30113280160085), .Dim = c(73L, 
2L), .Dimnames = list(NULL, c("lat", "lon")))

绘制地图和点以获得定向。

PlotOnStaticMap(Chelmsford)
P1 = LatLon2XY.centered(Chelmsford, MC[,1], MC[,2], 10)
names(P1) = c("x", "y")
points(P1, pch=16)

First Map - no clusters

现在我们需要找到并绘制群集。

set.seed(42)        ## For reproducibility
Clust = kmeans(MC, 7)

## Convert to graphics coordinates
Points = LatLon2XY.centered(Chelmsford, MC[,1], MC[,2], 10)
names(Points) = c("x", "y")
Points = data.frame(Points)

## Replot noting clusters
PlotOnStaticMap(Chelmsford)
points(Points, pch=21, bg=Clust$cluster)

## Add ellipses
for(i in 1:length(unique(Clust$cluster))) {
    dataEllipse(Points[Clust$cluster == i,1], Points[Clust$cluster == i,2], 
        center.pch=10, levels=0.90, fill=TRUE, fill.alpha=0.1,
        plot.points=FALSE, col=i, lwd=1,)
}

瞧瞧! Second map - now with clusters