1

我想使用spplot+ sp.lines(格)而不是plot+ segments。你知道实现这一点的简单方法吗,例如R:计算两个点层之间的最短距离

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

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

4

2 回答 2

1

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])
       })
于 2015-04-03T19:39:04.287 回答
0

理解面板函数比依赖sp.layoutin更强大spplot——直接使用latticeorgrid函数也是如此。解决方案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.linesetc 功能;在即将发布的 sp 1.1-0 中,也可以省略函数名的引号。

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

于 2015-04-04T17:36:33.140 回答