在阅读了很多帖子和网站之后,我无法找到如何从我的lon-lat数据表创建多边形shapefile。我想创建像这样的地图。
我的csv数据文件包含地中海1000点的经度,纬度和属性。我想按属性创建shapefile子集化数据,然后用ggplot2 geom_polygon
绘图,填充属性。
我试过这个R代码(来自https://stackoverflow.com/a/25608162/709777),但没有成功
library("sp")
library("rgdal")
clust1 <- subset(datos, datos$cluster == 1)
coords<-cbind(clust1$longitud,clust1$latitud)
sp_poly <- SpatialPolygons(list(Polygons(list(Polygon(coords)), ID=1)))
sp_poly_df <- SpatialPolygonsDataFrame(sp_poly, data=data.frame(ID=1))
writeOGR(sp_poly_df, "chull", layer="chull", driver="ESRI Shapefile")
ggplot() + geom_polygon(data=sp_poly_df, aes(x=long, y=lat, group=group))
产生的输出显然不是shapefile / polygon
plot(sp_poly_df)
给出了这个:
我是否需要先使用ggplot强化sp_poly_df?
如何创建shapefile或SpatialPolygon?我想将所有具有相同聚类属性的点合并,以映射数据集中的不同区域/多边形,然后使用聚类值填充多边形。也许这样,创建多边形/ shapefile,并不是生成这种类型地图的最佳方法。
还试过geom_map
,但这不是我想要的:
ggplot() +
geom_point(data=datos, aes(x=longitud, y=latitud, color=id),
alpha=1, na.rm=TRUE) + scale_size(range=c(1,12)) +
scale_color_identity() +
labs(title= "med cluster",
x="Longitude", y= "Latitude") + coord_map()
提前感谢您的帮助
使用的数据:
> dput(dato)
structure(list(longitud = c(18.875, 19.125, 19.375, 18.375, 18.625,
18.875, 19.125, 19.375, 19.625, 19.875, 17.875, 18.125, 18.375,
18.625, 18.875, 19.125, 19.375, 19.625, 19.875, 20.125, 28.875,
29.125, 29.375, 17.125, 17.375, 17.625, 17.875, 18.125, 18.375,
18.625, 18.875, 19.125, 19.375, 19.625, 19.875, 20.125, 27.875,
28.125, 28.375, 28.625, 28.875, 29.125, 29.375, 29.625, 29.875,
32.625, 32.875, 33.625, 33.875, 15.875, 16.125, 16.375, 16.625,
16.875, 17.125, 17.375, 17.625, 17.875, 18.125, 18.375, 18.625,
18.875, 19.125, 19.375, 19.625, 19.875, 20.125, 27.125, 27.375,
27.625, 27.875, 28.125, 28.375, 28.625, 28.875, 29.125, 29.375,
29.625, 29.875, 30.125, 30.375, 32.125, 32.375, 32.625, 32.875,
33.125, 33.375, 33.625, 33.875, 34.125, 15.625, 15.875, 16.125,
16.375, 16.625, 16.875, 17.125, 17.375, 17.625, 17.875, 18.125,
18.375, 18.625, 18.875, 19.125, 19.375, 19.625, 19.875, 25.375,
25.625, 25.875, 26.125, 26.375, 26.625, 26.875, 27.125, 27.375,
27.625, 27.875, 28.125, 28.375, 28.625, 28.875, 29.125, 29.375,
29.625, 29.875, 30.125, 30.375, 30.625, 30.875, 31.125, 31.375,
31.625, 31.875, 32.125, 32.375, 32.625, 32.875, 33.125, 33.375,
33.625, 33.875, 34.125, 34.375, 15.375, 15.625, 15.875, 16.125,
16.375, 16.625, 16.875, 17.125, 17.375, 17.625, 17.875, 18.125,
18.375, 18.625, 18.875, 19.125, 19.375, 19.625, 19.875, 25.125,
25.375, 25.625, 25.875, 26.125, 26.375, 26.625, 26.875, 27.125,
27.375, 27.625, 27.875, 28.125, 28.375, 28.625, 28.875, 29.125,
29.375, 29.625, 29.875, 30.125, 30.375, 30.625, 30.875, 31.125,
31.375, 31.625, 31.875, 32.125, 32.375, 32.625, 32.875, 33.125,
33.375, 33.625, 33.875, 34.125, 34.375, 34.625, 15.375, 15.625,
15.875, 16.125, 16.375, 16.625, 16.875, 17.125, 17.375, 17.625,
17.875, 18.125, 18.375, 18.625, 18.875, 19.125, 19.375, 19.625,
19.875, 23.875, 24.125, 24.375, 24.625, 24.875, 25.125, 25.375,
25.625, 25.875, 26.125, 26.375, 26.625, 26.875, 27.125, 27.375,
27.625, 27.875, 28.125, 28.375, 28.625, 28.875, 29.125, 29.375,
29.625, 29.875, 30.125, 30.375, 30.625, 30.875, 31.125, 31.375,
31.625, 31.875, 32.125, 32.375, 32.625, 32.875, 33.125, 33.375,
33.625, 33.875, 34.125, 34.375, 34.625, 15.125, 15.375, 15.625,
15.875, 16.125, 16.375, 16.625, 16.875, 17.125, 17.375, 17.625,
17.875, 18.125, 18.375, 18.625, 18.875, 19.125, 19.375, 19.625,
19.875, 20.125, 20.375, 23.125, 23.375, 23.625, 23.875, 24.125,
24.375, 24.625, 24.875, 25.125, 25.375, 25.625, 25.875, 26.125,
26.375, 26.625, 26.875, 27.125, 27.375, 27.625, 27.875, 28.125,
28.375, 28.625, 28.875, 29.125, 29.375, 29.625, 29.875, 30.125,
30.375, 30.625, 30.875, 31.125, 31.375, 31.625, 31.875, 32.125,
32.375, 32.625, 32.875, 33.125, 33.375, 33.625, 33.875, 34.125,
34.375, 34.625, 34.875, 14.375, 14.625, 14.875, 15.125, 15.375,
15.625, 15.875, 16.125, 16.375, 16.625, 16.875, 17.125, 17.375,
17.625, 17.875, 18.125, 18.375, 18.625, 18.875, 19.125, 19.375,
19.625, 19.875, 20.125, 20.375, 20.625, 20.875, 23.125, 23.375,
23.625, 23.875, 24.125, 24.375, 24.625, 24.875, 25.125, 25.375,
25.625, 25.875, 26.125, 26.375, 26.625, 26.875, 27.125, 27.375,
27.625, 27.875, 28.125, 28.375, 28.625, 28.875, 29.125, 29.375,
29.625, 29.875, 30.125, 30.375, 30.625, 30.875, 31.125, 31.375,
31.625, 31.875, 32.125, 32.375, 32.625, 32.875, 33.125, 33.375,
33.625, 33.875, 34.125, 34.375, 34.625, 34.875, 12.375, 12.625,
12.875, 13.125, 13.375, 13.625, 13.875, 14.125, 14.375, 14.625,
14.875, 15.125, 15.375, 15.625, 15.875, 16.125, 16.375, 16.625,
16.875, 17.125, 17.375, 17.625, 17.875, 18.125, 18.375, 18.625,
18.875, 19.125, 19.375, 19.625, 19.875, 20.125, 20.375, 20.625,
20.875, 21.125, 21.375, 21.625, 22.375, 22.625, 22.875, 23.125,
23.375, 23.625, 23.875, 24.125, 24.375, 24.625, 24.875, 25.125,
25.375, 25.625, 25.875, 26.125, 26.375, 26.625, 26.875, 27.125,
27.375, 27.625, 27.875, 28.125, 28.375, 28.625, 28.875, 29.125,
29.375, 29.625, 29.875, 30.125, 30.375, 30.625, 30.875, 31.125,
31.375, 31.625, 31.875, 32.125, 32.375, 32.625, 32.875, 33.125,
33.375, 33.625, 33.875, 34.125, 34.375, 34.625, 34.875, 35.125,
11.625, 11.875, 12.125, 12.375, 12.625, 12.875, 13.125, 13.375,
13.625, 13.875, 14.125, 14.375, 14.625, 14.875, 15.125, 15.375,
15.625, 15.875, 16.125, 16.375, 16.625, 16.875, 17.125, 17.375,
17.625, 17.875, 18.125, 18.375, 18.625, 18.875, 19.125, 19.375,
19.625, 19.875, 20.125, 20.375, 20.625, 20.875, 21.125, 21.375,
21.625, 21.875, 22.125, 22.375, 22.625, 22.875, 23.125, 23.375,
23.625, 23.875, 24.125, 24.375, 24.625, 24.875, 25.125, 25.375,
25.625, 25.875, 26.125, 26.375, 26.625, 26.875, 27.125, 27.375,
27.625, 27.875, 28.125, 28.375, 28.625, 28.875, 29.125, 29.375,
29.625, 29.875, 30.125, 30.375, 30.625, 30.875, 31.125, 31.375,
31.625, 31.875, 32.125, 32.375, 32.625, 32.875, 33.125, 33.375,
33.625, 33.875, 34.125, 34.375, 34.625, 34.875, 35.125, 11.125,
11.375, 11.625, 11.875, 12.125, 12.375, 12.625, 12.875, 13.125,
13.375, 13.625, 13.875, 14.125, 14.375, 14.625, 14.875, 15.125,
15.375, 15.625, 15.875, 16.125, 16.375, 16.625, 16.875, 17.125,
17.375, 17.625, 17.875, 18.125, 18.375, 18.625, 18.875, 19.125,
19.375, 19.625, 19.875, 20.125, 20.375, 20.625, 20.875, 21.125,
21.375, 21.625, 21.875, 22.125, 22.375, 22.625, 22.875, 23.125,
23.375, 23.625, 23.875, 24.125, 24.375, 24.625, 24.875, 25.125,
25.375, 25.625, 25.875, 26.125, 26.375, 26.625, 26.875, 27.125,
27.375, 27.625, 27.875, 28.125, 28.375, 28.625, 28.875, 29.125,
29.375, 29.625, 29.875, 30.125, 30.375, 30.625, 30.875, 31.125,
31.375, 31.625, 31.875, 32.125, 32.375, 32.625, 32.875, 33.125,
33.375, 33.625, 33.875, 34.125, 34.375, 34.625, 34.875, 35.125,
10.875, 11.125, 11.375, 11.625, 11.875, 12.125, 12.375, 12.625,
12.875, 13.125, 13.375, 13.625, 13.875, 14.125, 14.375, 14.625,
14.875, 15.125, 15.375, 15.625, 15.875, 16.125, 16.375, 16.625,
16.875, 17.125, 17.375, 17.625, 17.875, 18.125, 18.375, 18.625,
18.875, 19.125, 19.375, 19.625, 19.875, 20.125, 20.375, 20.625,
20.875, 21.125, 21.375, 21.625, 21.875, 22.125, 22.375, 22.625,
22.875, 23.125, 23.375, 23.625, 23.875, 24.125, 24.375, 24.625,
24.875, 25.125, 25.375, 25.625, 25.875, 26.125, 26.375, 26.625,
26.875, 27.125, 27.375, 27.625, 27.875, 28.125, 28.375, 28.625,
28.875, 29.125, 29.375, 29.625, 29.875, 30.125, 30.375, 30.625,
30.875, 31.125, 31.375, 31.625, 31.875, 32.125, 32.375, 32.625,
32.875, 33.125, 33.375, 33.625, 33.875, 34.125, 34.375, 34.625,
34.875, 35.125, 35.375, 10.125, 10.375, 10.625, 10.875, 11.125,
11.375, 11.625, 11.875, 12.125, 12.375, 12.625, 12.875, 13.125,
13.375, 13.625, 13.875, 14.125, 14.375, 14.625, 14.875, 15.125,
15.375, 15.625, 15.875, 16.125, 16.375, 16.625, 16.875, 17.125,
17.375, 17.625, 17.875, 18.125, 18.375, 18.625, 18.875, 19.125,
19.375, 19.625, 19.875, 20.125, 20.375, 20.625, 20.875, 21.125,
21.375, 21.625, 21.875, 22.125, 22.375, 22.625, 22.875, 23.125,
23.375, 23.625, 23.875, 24.125, 24.375, 24.625, 24.875, 25.125,
25.375, 25.625, 25.875, 26.125, 26.375, 26.625, 26.875, 27.125,
27.375, 27.625, 27.875, 28.125, 28.375, 28.625, 28.875, 29.125,
29.375, 29.625, 29.875, 30.125, 30.375, 30.625, 30.875, 31.125,
31.375, 31.625, 31.875, 32.125, 32.375, 32.625, 32.875, 33.125,
33.375, 33.625, 33.875, 34.125, 34.375, 34.625, 34.875, 35.125,
35.375, 10.125, 10.375, 10.625, 10.875, 11.125, 11.375, 11.625,
11.875, 12.125, 12.375, 12.625, 12.875, 13.125, 13.375, 13.625,
13.875, 14.125, 14.375, 14.625, 14.875, 15.125, 15.375, 15.625,
15.875, 16.125, 16.375, 16.625, 16.875, 17.125, 17.375, 17.625,
17.875, 18.125, 18.375, 18.625, 18.875, 19.125, 19.375, 19.625,
19.875, 20.125, 20.375, 20.625, 20.875, 21.125, 21.375, 21.625,
21.875, 22.125, 22.375, 22.625, 22.875, 23.125, 23.375, 23.625,
23.875, 24.125, 24.375, 24.625, 24.875, 25.125, 25.375, 25.625,
25.875, 26.125, 26.375, 26.625, 26.875, 27.125, 27.375, 27.625,
27.875, 28.125, 28.375, 28.625, 28.875, 29.125, 29.375, 29.625,
29.875, 30.125, 30.375, 30.625, 30.875, 31.125, 31.375, 31.625,
31.875, 32.125, 32.375, 32.625, 32.875, 33.125, 33.375, 33.625,
33.875, 34.125, 34.375, 34.625, 34.875, 35.125, 35.375, 35.625,
10.375, 10.625, 10.875), latitud = c(30.375, 30.375, 30.375,
30.625, 30.625, 30.625, 30.625, 30.625, 30.625, 30.625, 30.875,
30.875, 30.875, 30.875, 30.875, 30.875, 30.875, 30.875, 30.875,
30.875, 30.875, 30.875, 30.875, 31.125, 31.125, 31.125, 31.125,
31.125, 31.125, 31.125, 31.125, 31.125, 31.125, 31.125, 31.125,
31.125, 31.125, 31.125, 31.125, 31.125, 31.125, 31.125, 31.125,
31.125, 31.125, 31.125, 31.125, 31.125, 31.125, 31.375, 31.375,
31.375, 31.375, 31.375, 31.375, 31.375, 31.375, 31.375, 31.375,
31.375, 31.375, 31.375, 31.375, 31.375, 31.375, 31.375, 31.375,
31.375, 31.375, 31.375, 31.375, 31.375, 31.375, 31.375, 31.375,
31.375, 31.375, 31.375, 31.375, 31.375, 31.375, 31.375, 31.375,
31.375, 31.375, 31.375, 31.375, 31.375, 31.375, 31.375, 31.625,
31.625, 31.625, 31.625, 31.625, 31.625, 31.625, 31.625, 31.625,
31.625, 31.625, 31.625, 31.625, 31.625, 31.625, 31.625, 31.625,
31.625, 31.625, 31.625, 31.625, 31.625, 31.625, 31.625, 31.625,
31.625, 31.625, 31.625, 31.625, 31.625, 31.625, 31.625, 31.625,
31.625, 31.625, 31.625, 31.625, 31.625, 31.625, 31.625, 31.625,
31.625, 31.625, 31.625, 31.625, 31.625, 31.625, 31.625, 31.625,
31.625, 31.625, 31.625, 31.625, 31.625, 31.625, 31.875, 31.875,
31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875,
31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875,
31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875,
31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875,
31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875,
31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875,
31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875, 31.875,
32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125,
32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125,
32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125,
32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125,
32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125,
32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125,
32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125,
32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.125, 32.375,
32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375,
32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375,
32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375,
32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375,
32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375,
32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375,
32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375,
32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375, 32.375,
32.375, 32.375, 32.375, 32.375, 32.375, 32.625, 32.625, 32.625,
32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625,
32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625,
32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625,
32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625,
32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625,
32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625,
32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625,
32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625,
32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625, 32.625,
32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875,
32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875,
32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875,
32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875,
32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875,
32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875,
32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875,
32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875,
32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875,
32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875,
32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875, 32.875,
32.875, 32.875, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125,
33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125,
33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125,
33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125,
33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125,
33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125,
33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125,
33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125,
33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125,
33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125,
33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125,
33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125, 33.125,
33.125, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375,
33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375,
33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375,
33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375,
33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375,
33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375,
33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375,
33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375,
33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375,
33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375,
33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375,
33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375, 33.375,
33.375, 33.375, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625,
33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625,
33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625,
33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625,
33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625,
33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625,
33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625,
33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625,
33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625,
33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625,
33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625,
33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625, 33.625,
33.625, 33.625, 33.625, 33.625, 33.625, 33.875, 33.875, 33.875,
33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875,
33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875,
33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875,
33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875,
33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875,
33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875,
33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875,
33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875,
33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875,
33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875,
33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875,
33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875, 33.875,
33.875, 33.875, 33.875, 34.125, 34.125, 34.125, 34.125, 34.125,
34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125,
34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125,
34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125,
34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125,
34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125,
34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125,
34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125,
34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125,
34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125,
34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125,
34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125,
34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125, 34.125,
34.125, 34.125, 34.375, 34.375, 34.375), cluster = c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L,
3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L,
1L, 1L, 1L, 3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L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 2L, 2L, 4L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 1L,
1L, 1L, 1L, 5L, 5L, 5L)), .Names = c("longitud", "latitud", "cluster"
), row.names = c(NA, 1000L), class = "data.frame")
答案 0 :(得分:6)
感谢@ lorenzo-busetto和来自Mazama Science的blog post的评论(用于将数据转换为ggplot2可读格式),我可以获得所需的输出。
这是最终的R代码,希望它可以帮助其他一些R用户
# Packages
library(stringr)
library(ggplot2)
library(mapdata)
library(maptools)
library("gpclib")
library(rgeos)
library(raster)
library(sp)
library(rgdal)
# Path to data
ruta_datos<-"/home/meteo/PROJECTES/VERSUS/OUTPUT/DATA/CLUSTER_MED/"
# List of data files
files <- list.files(path = ruta_datos, pattern = "SST-cluster-mitja-mensual-")
# read data for i=8. Originally a for loop to read a bunch of files
i=8
datos<-read.csv(paste0(ruta_datos,files[i],sep=""),header=TRUE,na.strings = "NA")
# Create raster from xyz data
dat.raster<-rasterFromXYZ(datos)
# Create Polygon
dat.poly <- rasterToPolygons(dat.raster, dissolve=TRUE)
# add to data a new column termed "id" composed of the rownames of data
dat.poly@data$id <- rownames(dat.poly@data)
# create a data.frame from our spatial object
datPoints <- fortify(dat.poly, region = "id")
# merge the "fortified" data with the data from our spatial object
datDF <- merge(datPoints, dat.poly@data, by = "id")
dat.poly@data$id <- rownames(dat.poly@data)
datPoints <- fortify(dat.poly, region = "id")
datDF <- merge(datPoints, dat.poly@data, by = "id")
# Map settings
# Prepare map coastline
if (!rgeosStatus()) gpclibPermit()
# path to the GSHHS maps on my computer
costa <- "/home/meteo/PROJECTES/VERSUS/DATA/GEO/gshhs_f.b"
shore <- getRgshhsMap(costa, xlim = c(-15, 45), ylim = c(30, 50))
# Labels
ewbrks <- seq(-15,45,5)
nsbrks <- seq(30,50,5)
# Color palette
sst_paleta <- c("#4eb400","#a0ce00","#f7e400","#f8b600","#f88700","#f85900","#e82c0e","#d8001d","#ff0099","#b54cff","#998cff")
# Legend breaks
sst_breaks <- c(1,2,3,4,5,6,7,8,9,10,11)
# Plot map
ggplot(data = datDF, aes(x=long, y=lat, group = group, fill = cluster)) +
geom_polygon() +
geom_polygon(data = shore, aes(x=long, y = lat, group = group), size=0.2, color = "black", fill = "burlywood2") +
theme_bw() +
coord_fixed(1.3) + scale_x_continuous(breaks = ewbrks,expand = c(0, 0)) +
scale_y_continuous(breaks = ewbrks,expand = c(0, 0)) +
scale_fill_gradientn(colours = sst_paleta, na.value = NA, limits=c(1,11), breaks = sst_breaks)