[R-sig-Geo] How to check a large neighbourhood matrix?

Roger Bivand Roger.Bivand at nhh.no
Fri Mar 28 09:32:45 CET 2008


On Fri, 28 Mar 2008, Oscar Breugelmans wrote:

> Roger and Danlin:
>
> Thanks for your help on this. This is the code I used to create and plot
> the nb:
>
> pc4.02=readShapePoly("..../pc4_2002/pc4_gebieden_2002",IDvar="PC4NR")
> nb.pc4.02=poly2nb(pc4.02,snap=0.01)
> coords=coordinates(pc4.02)
> plot(pc4.02)
> plot.nb(nb.pc4.02,coords,add=T)
> summary(nb.pc4.02)
>
> From Roger's comments I understand that there is no easy way to zoom in on
> this plot. I wanted to specifically zoom in on the plot.nb() because it
> shows the connections between the neighbours. I was able to check all the
> areas with only 1 neighbour, but was afraid that there would be areas that
> had only some of its neighbours assigned correctly. I will try to use xlim=
> en ylim= to zoom in on the plot. I think writeOGR() is not really an option
> because it will not show the lines connecting the neighbours that plot.nb()
> so nicely does.

?nb2lines

This makes a SpatialLinesDataFrame, showing the lines connecting the 
neighbours. However, GE also has the drawback of only being in projected 
coordinates. I suggest using locator(2) at the SW and NE corners of the 
area of interest, feeding these forward to plot.nb() as xlim= and ylim=. 
Tedious, but it will get you there.

Roger

>
> Oscar
>
>
>
>
> On Thu, 27 Mar 2008, Danlin Yu wrote:
>
>> Oscar:
>>
>> If you produced the nb list in R, you can always summarize the nb list
>> to see whether there are units that don't have neighbors.
>>
>> For instance:
>> #loading spdep
>> library(spdep)
>>
>> #you have read a shapefile of postal code areas:
>>
>> post<-read.shape("../yourshapefile.shp")
>>
>> #since it is a polygon shapefile, a Map2poly and poly2nb function shall
>> do the trick (Roger will have better ideas than my naive ones)
>>
>> post.nb<-poly2nb(Map2poly(post))
>
> I think that Oscar had got this far using the prefered sp object:
>
> SPDF <- readShapePoly()
>
> nb <- poly2nb(SPDF)
>
> but had needed to use the snap= argument to poly2nb() to overcome slivers
> in his polygons.
>
>>
>> #then you summarize the nb list:
>>
>> summary(post.nb)
>>
>
> This helps, because it says which have no neighbours. It will indicate how
> to "zoom" in on the plot for the locations of the polygons. To actually
> "zoom", use xlim= and ylim= in the plot method for nb objects.
>
> If you want real control, consider writeOGR(SPDF, ..., driver="KML") in
> the rgdal package, and equivalently writeOGR() of the
> SpatialLinesDataFrame output by nb2lines(), and zoom to your satisfaction
> in GE using KML files.
>
> Roger
>
>> The summary command will give you the characteristics of the neighbor
>> list object, including Number of regions, Number of nonzero links,
>> Percentage nonzero weights, Average number of links, regions that have
>> no links (which shall give you the idea whether or not some postal code
>> areas are connected) and the most and least connected regions
>>
>> Hope this helps.
>>
>> Regards,
>> Dr. Danlin Yu
>
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list