0

I downloaded three different shapefiles of the city chicago. Now i wanted to combine them and plot them together. Only the ward-offices do not plot. '''Error: data must be a data frame, or other object coercible by fortify(), not an S4 object with class SpatialPointsDataFrame'''

The first two are polygons and the third is point oriented...

this is my code:

#setting directiories
chicagodir = 'Community Areas/'
chicagoshp = paste(chicagodir, "geo_export_b982773b-13a7-4b96-827d-d969f0695777.shp", sep = "")
chicagoread =readOGR(chicagoshp)
plot(chicagoread)


#Wardprecints facilities 
wardprecinctsdir = 'WardPrecincts/'
wardprecinctsshp = paste(wardprecinctsdir, "WardPrecincts.shp", sep = "")
wardprecinctsshpread =readOGR(wardprecinctsshp)
plot(wardprecinctsshpread)

#Ward Offices - Map - Exportable
wardofficesdir = 'Ward Offices - Map - Exportable/'
wardofficesshp = paste(wardofficesdir, "geo_export_b47221aa-0cc1-45db-8a3c-3e6ba72dccf1.shp", sep = "")
wardofficesshpread = readOGR(wardofficesshp)
plot(wardofficesshpread)


chicago_transform <- spTransform(chicagoread, CRS("+proj=longlat +init=epsg:4326"))
wardpre_transform <-spTransform(wardprecinctsshpread, CRS("+proj=longlat +init=epsg:4326"))
wardoff_transform <-spTransform(wardofficesshpread, CRS("+proj=longlat +init=epsg:4326"))


combinedplot = ggplot() +
  geom_path(data = chicago_transform, aes(x = long, y = lat, group = group)) +
  coord_fixed() +
  labs(title = "Plot of Chicago",
       x = "long", y = "lat") +# Because we don't need x and y labels do we?
  #add specific wardprecinctsshpread and wardoffices
  geom_polygon(data=wardpre_transform, aes(x = long, y = lat, group = group, color="red")) +
  geom_polygon(data=wardoff_transform, aes(x = long, y = lat, group = group, color="blue"))
combinedplot
'''

How do I resolve this, the plotting of points to the 2 polygons? thankyou in advance

1 Answers1

0

Indeed the ward office data is different as points. You can just use as.data.frame on SpatialPointsDataFrame for plotting.

Note that instead of lat and long you need latitude and longitude (really y and x coordinates).

Otherwise, let me know if this is what you had in mind.

library(rgdal)
library(ggplot2)

chicagoread =readOGR("geo_export_210c4709-68df-496d-8b9c-8f7484608bc9.shp")
plot(chicagoread)

wardprecinctsshpread =readOGR("WardPrecincts.shp")
plot(wardprecinctsshpread)

wardofficesshpread = readOGR("geo_export_10f18171-8b13-4022-8d57-08d3b9bb0027.shp")
plot(wardofficesshpread)

chicago_transform <- spTransform(chicagoread, CRS("+proj=longlat +init=epsg:4326"))
wardpre_transform <- spTransform(wardprecinctsshpread, CRS("+proj=longlat +init=epsg:4326"))
wardoff_transform <- spTransform(wardofficesshpread, CRS("+proj=longlat +init=epsg:4326"))

combinedplot = ggplot() +
  geom_path(data = chicago_transform, aes(x = long, y = lat, group = group)) +
  coord_fixed() +
  labs(title = "Plot of Chicago", x = "long", y = "lat") +
  geom_polygon(data=wardpre_transform, aes(x = long, y = lat, group = group), color="red") +
  geom_point(data=as.data.frame(wardoff_transform), aes(x = longitude, y = latitude), color="blue") +
  theme_bw()

combinedplot

chicago ward offices map

Ben
  • 28,684
  • 5
  • 23
  • 45