简体   繁体   中英

Plot points and polygons in single window in spatstat

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.

As an alternative you might plot your polygons first using

plot(regions)
points("Plants1")

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

在此处输入图片说明

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)

在此处输入图片说明

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.

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