12

After reading a lot of posts and websites I can't find out how to create a polygon shapefile from my lon-lat data table. I would like to create maps like this one. enter image description here

My csv data file contains longitude, latitude and attribute of 1000 points in the Mediterranean. I would like to create shapefiles subsetting data by attribute and then plot with ggplot2 geom_polygon, filling with attribute.

I have tried this R code (from https://stackoverflow.com/a/25608162/709777) but without success

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

That produces this output that's not obviously a shapefile/polygon

enter image description here

plot(sp_poly_df) gives this:

enter image description here

Do I need to previously fortify sp_poly_df to plot with ggplot?

How can I create a shapefile or SpatialPolygon? I would like to merge all points with the same cluster attribute to map the different areas/polygons in the data set and then fill the polygons with the cluster value. Maybe this, creating polygons/shapefiles, is not the best approach to produce this type of map.

Have also tried geom_map but it is not exactly what I want:

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

enter image description here

Thanks in advance for your help

Data used:

> 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,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 5L, 5L, 5L, 4L,
4L, 4L, 4L, 2L, 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")
9
  • 3
    Have you tried geom_map? Commented Dec 13, 2016 at 10:15
  • 1
    @RomanLuštrik Added code and output of geom_map. Not exactly what I want, I would prefer a map of filled polygons rather than this points map. Commented Dec 13, 2016 at 10:42
  • 1
    I don't see geom_map call or putput anywhere? Commented Dec 13, 2016 at 10:44
  • 1
    Have added code and map just before dput command on the post Commented Dec 13, 2016 at 10:50
  • 2
    It seems to me that your data are in truth a "raster" map, and not a list of polygons. If your data are representing a regular grid (as it seems), I'd suggest creating a raster object using rasterFromXYZ() and then converting to polygons using rasterToPolygons. By the way: the data you shared are clearly not what you used to create the "ggmap" figure, since they cover a smaller extent and number of classes, so it is not possible to test any solution. Commented Dec 15, 2016 at 13:45

1 Answer 1

7

Thanks to the comment of @lorenzo-busetto and the blog post from Mazama Science (for the conversion of data to ggplot2 readable format) I could get the desired output.

Here is the final R code, hope it can help some other R users

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

and the map enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.