1

+の代わりにspplot+ sp.lines(格子)を使用したいと思います。これを実現する簡単な方法を知っていますか。たとえば、R: 2 つのポイント レイヤー間の最短距離の計算plotsegments

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

しかし、 ieに変換する簡単な方法はありますsegmentsSpatialLineslist("sp.lines", Lines(...))

4

2 に答える 2

1

panel.segments()-パッケージから試してくださいlattice

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

Understanding panel functions is more powerful than relying on sp.layout in spplot -- and so is using lattice or grid functions directly. The solution with sp.layout could look like this:

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

note that it is not restricted to the sp.lines etc functions; in upcoming sp 1.1-0, quotes around function names can also be omitted.

spplot tries to plot attributes of features in color by default, which is not meaningful here, so what you basically want is an xyplot with controlled aspect ratio (asp="iso").

于 2015-04-04T17:36:33.140 に答える