SpatialLines 而不是线段(spplot)

SpatialLines instead of segments (spplot)

我想使用 spplot + sp.lines(格)而不是 plot + segments。你知道一个简单的方法来实现这一点,例如

library(dismo)  
require(rgdal)
require(FNN)

laurus <- gbif("Laurus", "nobilis")
locs <- subset(laurus, !is.na(lat) & !is.na(lon),
               select = c("country", "lat", "lon"))

locs.uk  <- subset(locs, locs$country=="United Kingdom")
locs.ire <- subset(locs, locs$country=="Ireland")

uk_coord <- SpatialPoints(locs.uk[,c("lon","lat")])
ire_coord <- SpatialPoints(locs.ire[,c("lon","lat")])
crs.geo<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")  
proj4string(uk_coord) <- crs.geo 
proj4string(ire_coord) <- crs.geo 

uk_coord  <- spTransform(uk_coord, CRS("+init=epsg:27700"))
ire_coord <- spTransform(ire_coord, CRS("+init=epsg:27700"))


g = get.knnx(coordinates(uk_coord), coordinates(ire_coord),k=1)

形象化

plot(uk_coord, col=2, xlim=c(-1e5,6e5))
plot(ire_coord, add=TRUE)
segments(coordinates(ire_coord)[,1], 
         coordinates(ire_coord)[,2], 
         coordinates(uk_coord[g$nn.index[,1]])[,1], 
         coordinates(uk_coord[g$nn.index[,1]])[,2])

可能会转换成类似

的东西
ire <- list("sp.points", ire_coord)

spplot(uk_coord, sp.layout=list(ire))

但是有没有一种简单的方法可以将 segments 转换为 SpatialLineslist("sp.lines", Lines(...))

尝试 panel.segments() 来自 lattice-package:

library("lattice")
spplot(rbind(uk_coord, ire_coord), auto.key=FALSE,
       panel=function(...) {
         panel.xyplot(...)
         panel.segments(coordinates(ire_coord)[,1], 
                        coordinates(ire_coord)[,2],
                        coordinates(uk_coord[g$nn.index[,1]])[,1],
                        coordinates(uk_coord[g$nn.index[,1]])[,2])
       })

了解面板函数比依赖 spplot 中的 sp.layout 更强大——直接使用 latticegrid 函数也是如此。 sp.layout 的解决方案可能如下所示:

spplot(uk_coord, auto.key=FALSE, col.regions = 'black', 
  sp.layout = list(ire, 
    list("panel.segments", 
        coordinates(ire_coord)[,1],
        coordinates(ire_coord)[,2],
        coordinates(uk_coord[g$nn.index[,1]])[,1],
        coordinates(uk_coord[g$nn.index[,1]])[,2])), 
    xlim = c(-140000,700000))

注意不限于sp.lines等函数;在即将发布的 sp 1.1-0 中,函数名称周围的引号也可以省略。

spplot 尝试默认绘制颜色特征的属性,这在这里没有意义,所以你基本上想要的是一个 xyplot 具有受控纵横比(asp="iso") .