使用clusplot以0为中心的坐标绘制聚类 [英] Plotting Clusters using clusplot with coordinates centered around 0

查看:160
本文介绍了使用clusplot以0为中心的坐标绘制聚类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制GIS坐标,特别是绘制东西向组合的英国国家网格坐标: 194630000 562220000

I am trying to plot GIS coordinates, specifically UK national Grid Coordinates which eastings and northings ressemble: 194630000 562220000

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

I can plot these using clusplot in the Cluster library: clusplot (df2,k.means.fit$cluster,main=i,color=TRUE,shade=FALSE,labels=0,lines=0,bty="7")

其中df2是我的数据帧,而k.means.fit是对df2进行K均值分析的结果.

where df2 is my data frame and k.means.fit is the result of the K means analysis on df2.

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

Note that the coordinates of the centers after the k means analysis have not been normalised:

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

但是当我绘制聚类时,所有点都被平移,使得它们以原点为中心.

But when I plot the clusters, all the points are translated such that they are centered around the origin.

我想在图的背景中显示地图,但除非能够停止翻译,或者至少不知道所使用的函数的值,否则我将无法正确使用它们.

I am wanting to show a map in the backround for context of the plots, but unless I am able to stop the translation, or at least know the values the function used, I cannot allign these properly.

我了解clusplots旨在自动执行很多功能,这限制了自定义,但是我找不到能够创建相似聚类图的程序包.

I understand clusplots is designed to do a lot of feature automatically, which limits customisation, but I am not able to find a package that creates similar cluster plots.

预期的情节 (此操作是随机放置的,不准确)

Intended plot (this was done at a random placement and is innaccurate)

实际群集图

推荐答案

这里是一种产生与您所要的东西接近的东西的方法.

Here is a way to produce something close what you are asking for.

由于需要在(纬度,经度)和图形之间进行翻译 坐标(x,y)我没有使用clusplot.相反,我使用RgoogleMaps来获取背景图并进行坐标转换.我用车来画椭圆.

Because of the need to translate between (lat, lon) and the graphics coordinates (x,y) I did not use clusplot. Instead, I am using RgoogleMaps to get the background map and do the coordinate translations. I use car to plot the ellipses.

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")

您没有提供任何要测试的要点,因此我提出了几点.我意识到我的观点比你的观点更可分离,但这只会影响聚类算法,而不会影响映射.

You did not provide any points to test on, so I made up a few. I realize that my points are more separable than yours, but that only affects the clustering algorithm, not the mapping.

##  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")))

绘制地图并指向仅用于定向.

Plot the map and points just to get oriented.

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

现在我们需要查找和绘制聚类.

Now we need to find and plot the 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,)
}

瞧!

这篇关于使用clusplot以0为中心的坐标绘制聚类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆