简体   繁体   中英

spatial geodesic location clustering - ggplot2 error

I am using a code earlier posted by @jlhoward at Approaches for spatial geodesic latitude longitude clustering in R with geodesic or great circle distances

However, I am trying to implement the same thing for Sweden. When I load the shp file(s) I get an error from ggplots. How can I solve this? My code is below:

map.SE  <- readOGR(dsn="sweden-latest", layer="places")
map.df  <- fortify(map.SE)

ggplot(map.df)+
  geom_path(aes(x=long, y=lat, group=group))+
  geom_point(data=df, aes(x=long, y=lat, color=factor(clust)), size=4)+
  scale_color_discrete("Cluster")+
  coord_fixed()

The error that I get is at the "fortify" step - ggplot2 doesn't know how to deal with data of class SpatialPointsDataFrame

The problem is that your shapefile is point data, not polygons as in the linked example (that uses the polygon as an outline). fortify() is used in this context to convert polygons into a series of vertices for plotting in ggplot2 , so will never work for point data.

If you want to replicate their results, you need a polygon shapefile for Sweden to use in place of the Californian one used in the example.

One of the easy ways to get GIS shapefiles is to use the raster package. You can download the GADM data. Then, you can do all sorts of ggplot business later. As you intended, you can add dots using geom_point() , for example.

library(raster)
library(ggplot2)

sweden <- getData("GADM", country = "Sweden", level = 1)
map <- fortify(sweden)

ggplot() + 
geom_map(data = map, map = map, aes(x = long, y = lat, map_id = id, group = group)) +
coord_map()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM