20 Jun 2012 20:23

## 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")
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.
>
> 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:
>
> # 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.
>
> Could someone help out ?
>
> Thank you very much in advance,
>
> Juliane
>        [[alternative HTML version deleted]]
>
>
>

--

--
Juan Tomás Sayago Gómez
West Virginia University - RRI
886 Chestnut Ridge Road, Room 520
P.O. Box 6825
Morgantown, WV 26506-6825

[[alternative HTML version deleted]]

```
```_______________________________________________
R-sig-Geo mailing list
R-sig-Geo <at> r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
```

Gmane