If you're committed to using thespatstat
package, this probably will not be very helpful. If not, read on...
If all you want do to is plot the polygons as separate layers, use ggplot
library(ggplot2)
library(sp)
library(maptools)
setwd("<directory with all your files...>")
poly1 <- readShapeSpatial("Polygon_One")
poly2 <- readShapeSpatial("Polygon_Two")
# plot polygons as separate layers,,,
poly1.df <- fortify(poly1)
poly2.df <- fortify(poly2)
ggplot() +
geom_path(data=poly1, aes(x=long,y=lat, group=group))+
geom_path(data=poly2, aes(x=long,y=lat, group=group), colour="red")+
coord_fixed()
If you need to combine them into one spatialPolygonDataFrame, use this. The nuance here is that you can't use spRbind(...)
if the two layers have common polygon IDs. So the call to spChFIDs(...)
changes the ID of the one polygon in poly2
(the circle) to "R.3km".
# combine polygons into a single shapefile
poly.combined <- spRbind(poly1,spChFIDs(poly2,"R.3km"))
# plot polygons using ggplot aesthetic mapping
poly.df <- fortify(poly.combined)
ggplot(poly.df) +
geom_path(aes(x=long, y=lat, group=group, color=group)) +
scale_color_discrete("",labels=c("Center", "3km Radius")) +
coord_fixed()
# plot using plot(...) method for spatialObjects
plot(poly.combined)
You'll notice that, on these plots, the "circle" isn't. This is because we're using long/lat and you're pretty far south of the equator. The data needs to be reprojected. Turns out the appropriate CRS for South Africa is utm-33.
wgs.84 <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
utm.33 <- "+proj=utm +zone=33 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
proj4string(poly.combined) <- CRS(wgs.84)
poly.utm33 <- spTransform(poly.combined,CRS(utm.33))
poly.df <- fortify(poly.utm33)
ggplot(poly.df) +
geom_path(aes(x=long, y=lat, group=group, color=group)) +
scale_color_discrete("",labels=c("Center", "3km Radius")) +
theme(axis.text=element_blank()) + labs(x=NULL,y=NULL) +
coord_fixed()
Now the circle is.