Juliane Struve | 20 Jun 2012 18:40
Picon
Favicon

plot nearest neighbor links

Dear list,
 
I would like to plot the links that dnearneigh() identifies.
 
Space <- matrix(data = 1, nrow = 10, ncol = 10)
Space[1,] <- NA
Space[nrow(Space),] <- NA
Space[,1] <- NA
Space[,ncol(Space)] <- NA
WaterRow <- row(Space)
WaterCol <- col(Space)
WaterXY  <- cbind(X=WaterRow[Space==1], Y=WaterCol[Space==1])
library(spdep)
WaterXY  <- na.omit(WaterXY)
WaterNB  <- dnearneigh(WaterXY, 0, 1) 
res <- listw2sn(nb2listw(WaterNB))[,1:2]
as.matrix(res)
 
I have found an older post on a related topic that shows an example of what I would like to do:
 
example(read.gal)
# to get an nb object
us48.q
us48.q[1:2]
res <- listw2sn(nb2listw(us48.q))[,1:2]
res[1:9,]
str(res)

I have managed to retrieve the links using as.matrix(res), but I can't get the plot of nodes and links to
appear. 
(Continue reading)

juantomas.sayago | 20 Jun 2012 18:55
Picon

Re: plot nearest neighbor links

If I understand your question right you want to create a map of the knn links so you can see them and who is
neighbor of who. Right? 
The way I would do it is. 
I would create the knn nb and then plot it that way. 
If you need the codes for that, I can send them in a couple of hours.  
Juan. 
Sent on the Sprint® Now Network from my BlackBerry®

-----Original Message-----
From: Juliane Struve <juliane_struve <at> yahoo.co.uk>
Sender: r-sig-geo-bounces <at> r-project.orgDate: Wed, 20 Jun 2012 17:40:31 
To: r-sig-geo <at> stat.math.ethz.ch<r-sig-geo <at> stat.math.ethz.ch>
Reply-To: Juliane Struve <juliane_struve <at> yahoo.co.uk>
Subject: [R-sig-Geo] plot nearest neighbor links

Dear list,
 
I would like to plot the links that dnearneigh() identifies.
 
Space <- matrix(data = 1, nrow = 10, ncol = 10)
Space[1,] <- NA
Space[nrow(Space),] <- NA
Space[,1] <- NA
Space[,ncol(Space)] <- NA
WaterRow <- row(Space)
WaterCol <- col(Space)
WaterXY  <- cbind(X=WaterRow[Space==1], Y=WaterCol[Space==1])
library(spdep)
WaterXY  <- na.omit(WaterXY)
WaterNB  <- dnearneigh(WaterXY, 0, 1) 
(Continue reading)

Juan Tomas Sayago | 20 Jun 2012 20:23
Picon

Re: plot nearest neighbor links

check the example on spdep's help.

example(columbus)
coords <- coordinates(columbus)
col.knn <- knearneigh(coords, k=4)
plot(columbus, border="grey")
plot(knn2nb(col.knn), coords, add=TRUE)
title(main="K nearest neighbours, k = 4")

On Wed, Jun 20, 2012 at 12:55 PM, <juantomas.sayago <at> gmail.com> wrote:

> If I understand your question right you want to create a map of the knn
> links so you can see them and who is neighbor of who. Right?
> The way I would do it is.
> I would create the knn nb and then plot it that way.
> If you need the codes for that, I can send them in a couple of hours.
> Juan.
> Sent on the Sprint® Now Network from my BlackBerry®
>
> -----Original Message-----
> From: Juliane Struve <juliane_struve <at> yahoo.co.uk>
> Sender: r-sig-geo-bounces <at> r-project.orgDate: Wed, 20 Jun 2012 17:40:31
> To: r-sig-geo <at> stat.math.ethz.ch<r-sig-geo <at> stat.math.ethz.ch>
> Reply-To: Juliane Struve <juliane_struve <at> yahoo.co.uk>
> Subject: [R-sig-Geo] plot nearest neighbor links
>
> Dear list,
>
> I would like to plot the links that dnearneigh() identifies.
>
(Continue reading)

Juan Tomas Sayago | 20 Jun 2012 20:36
Picon

Re: plot nearest neighbor links

The example for the function that you are looking for is here from spdep's
manual

example(columbus)
coords <- coordinates(columbus)
rn <- sapply(slot(columbus, "polygons"), function(x) slot(x, "ID"))
k1 <- knn2nb(knearneigh(coords))
all.linked <- max(unlist(nbdists(k1, coords)))
col.nb.0.all <- dnearneigh(coords, 0, all.linked, row.names=rn)
summary(col.nb.0.all, coords)
plot(columbus, border="grey")
plot(col.nb.0.all, coords, add=TRUE)
title(main=paste("Distance based neighbours 0-",  format(all.linked),
 " distance units", sep=""))

On Wed, Jun 20, 2012 at 2:23 PM, Juan Tomas Sayago <
juantomas.sayago <at> gmail.com> wrote:

> check the example on spdep's help.
>
> example(columbus)
> coords <- coordinates(columbus)
> col.knn <- knearneigh(coords, k=4)
> plot(columbus, border="grey")
> plot(knn2nb(col.knn), coords, add=TRUE)
> title(main="K nearest neighbours, k = 4")
>
>
>
> On Wed, Jun 20, 2012 at 12:55 PM, <juantomas.sayago <at> gmail.com> wrote:
(Continue reading)


Gmane