0

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

Community
  • 1
  • 1
dsauce
  • 592
  • 2
  • 14
  • 36
  • actually the data 'df' in the grom_point is another data set with the clusters and lat/longs. i used the exact code from the other post that i mentioned (link in the question). Anyway, the error that i get at the fortify step that is before ggplot – dsauce Mar 20 '15 at 01:17
  • Is there any way you can provide the shapefiles? – jazzurro Mar 20 '15 at 01:19
  • yes - i download it from here - http://download.geonames.org/export/zip/SE.zip – dsauce Mar 20 '15 at 01:22
  • I see two text files, but not shapefiles. – jazzurro Mar 20 '15 at 01:24
  • sorry, provided the wrong link. here is the correct file - http://www.filedropper.com/sweden-places-shape – dsauce Mar 20 '15 at 01:29
  • One way to get sweden shapefiles would be like this. You use the `raster` package. `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()` You can add `geom_point()` to this if you want. – jazzurro Mar 20 '15 at 01:31
  • thanks! however, the website it's pulling data from (http://data.biogeo.ucdavis.edu/data/gadm2/R/) is not responding / not connecting / timing out. Any other similar solution? – dsauce Mar 20 '15 at 07:53
  • I see. It does not usually take that much time. You may want to try again. Otherwise, you can directly go to GADM and download shapefiles. http://www.gadm.org – jazzurro Mar 20 '15 at 07:56

2 Answers2

1

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.

AJD
  • 301
  • 2
  • 9
1

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

enter image description here

jazzurro
  • 23,179
  • 35
  • 66
  • 76