gpt4 book ai didi

r - 使用 clusplot 绘制以 0 为中心坐标的聚类

转载 作者:行者123 更新时间:2023-12-01 23:14:51 26 4
gpt4 key购买 nike

我正在尝试绘制 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

但是当我绘制簇时,所有点都会被平移,以便它们以原点为中心。

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

我知道 clusplots 被设计为自动执行许多功能,这限制了自定义,但我无法找到创建类似聚类图的包。

Intended plot(这是随机放置的,不准确)

Actual cluster diagram

最佳答案

这里有一种方法可以产生接近您所要求的东西。

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

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

绘制 map 和点只是为了确定方向。

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

关于r - 使用 clusplot 绘制以 0 为中心坐标的聚类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41680246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com