0

I am attempting to plot coordinate data that includes both polygons and points (in separate files) in a single window so that I can later run tests to see what patterns exist. I am fairly new to R (and very new to spatstat) so I really appreciate any advice on how best to create a single plot with multiple types of spatial data.

library(sp)
library(maptools)
library(mgcv)
library(spatstat)

##read in the shapefiles (from Pathfinder)
data<-readShapeSpatial("SouthC1")
regions<-slot(data, "polygons")
regions<-lapply(regions, function(data){SpatialPolygons(list(data))})
windows<-lapply(regions, as.owin)
spatstat.options(checkpolygons=FALSE)
y<-as(data, "owin")
spatstat.options(checkpolygons=TRUE)
points<-readShapeSpatial("Plants1")

##Define points and polygons as objects that can be read into owin?

I suspect that I'm suffering from novice-itis and that reading different types of spatial data into a single window is not difficult. Sorry.

Side note: some of the polygons do overlap, which is why I don't want spatstat to check the polygons. I am aware that this creates complication, but that is not a pressing concern.

EML
  • 1
  • 1
  • 1

3 Answers3

1

As an alternative you might plot your polygons first using

plot(regions)
points("Plants1")
jsta
  • 3,216
  • 25
  • 35
0

If you use spplot from the sp package, you can use the sp.layout argument. Note the examples below combine a spatial grid with spatial points, but the exact same techniques can be used for points and polygons.

library(sp)
library(lattice)
trellis.par.set(sp.theme()) # sets bpy.colors() ramp
data(meuse)
coordinates(meuse) <- ~x+y
data(meuse.grid)
gridded(meuse.grid) <- ~x+y

spplot(meuse.grid, c("ffreq"), sp.layout = list("sp.points", meuse))

enter image description here

or use ggplot2 (my preference):

library(ggplot2)
# Note that you can use `fortify` to transform a SpatialPolygons* object to a data.frame
# for `ggplot2`.
pt_data = as.data.frame(meuse)
grid_data = as.data.frame(meuse.grid)
ggplot(grid_data, aes(x = x, y = y)) + geom_tile(aes(fill = ffreq)) + 
                            geom_point(data = pt_data)

enter image description here

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
0

In spatstat you can plot objects on top of each other using the layered class.

In your example, regions is a list of windows (class owin). Just type

plot(as.layered(as.solist(regions)))

Here as.solist converts a vanilla list to a list of spatial objects; as.layered converts this to a layered object.

Adrian Baddeley
  • 1,956
  • 1
  • 8
  • 7