# [R] Do you know if you can map a large minimum spanning tree in R?

Fri Aug 20 09:16:16 CEST 2004

```Thanks Roger and Mike.
I spent most of today fiddling as Mike's suggestion got me the nodes on the map, but I had trouble with the coordinates, however I eventually got it to work with the following :

...
code to input site data, calculate distances, calculate minimum spanning tree in mdist
...

coords = array(0,c(NumSites,2))

for( i in 1:NumSites )
{
coords[i,1] = sites[i,3];
coords[i,2] = sites[i,2];
}  #sites contains site name, lat, long

library(spdep)
mdist.nb <- mat2listw(mdist)\$neighbours
plot(mdist.nb, coords)

library(maps)

Now I just have to pretty up the result for the boss!
Thanks again!
Fiona.

-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand at nhh.no]
Sent: Thursday, 19 August 2004 5:51 PM
To: r-help at stat.math.ethz.ch
Cc: fiona.m.wade at team.telstra.com
Subject: RE: [R] Do you know if you can map a large minimum spanning
tree in R?

>>
>> Thanks Mike.
>> My data has longitude and latitude coords and I used distAB {clim.pact}
> then mst {ape} to calculate my minimum spanning tree.  The nodes are
> telecoms sites from all over Australia.  My goal is to determine the
> minimum cost of linking them via cabling, and I'm starting by
>> calculating the distance "as the crow flies", but will probably
>> eventually need to calculate the rectilinear distances also.
>> I am a very newbie user of R, but have had experience with other
> stats/programming software such as SAS, however no longer have access to
> SAS so I've turned to R.  I also have tried using MapInfo with the data
> exported from R, but have found that not so intuitive to learn on the
> fly.
>> Back to R - I'm using W2K, and have managed to graph the tree using
> plot(mdist,graph="nsca") where mdist is the output matrix from my mst
> command, however this is not terribly map-like, so I'm looking for a
> better display that can be embedded in a document.
>> Any assistance gratefully received!
>
> One possibility is to use the fact that mst() function returns a 0/1
> matrix exactly like spatial weights matrices used in the spdep package:
>

Sorry, must be 2D:

> n <- 2

otherwise plot.nb() will not like X, the matrix of coordinates. In your
case that's obvious, but not in general.

>> X <- matrix(runif(n*50), 50, n)
>> d <- dist(X)
>> M <- mst(d)
>> library(spdep)
>> M.nb <- mat2listw(M)\$neighbours
>> plot(M.nb, X)
>
> The plot.nb() function then plots the graph by joining points defined as
> neighbours, use the add=TRUE argument to overplot on a base map. The nb
> object can also be manipulated a little, like subsetting, if that is any
> use. The idea is to convert the 0/1 matrix to a list with vectors of
> neighbours' IDs for each point, rather than store the whole matrix.
> Contact me off-list if you need more details. (nbdists() retrieves the
> distances on the graph too).
>
>
>> Fiona.
>>
>>> Project Manager
>>> MARA
>>> F&A
>>> Telstra Corporation Limited
>>> Tel: 03 9634 5674
>>> Fax: 03 9634 2874
>>> Email: fiona.m.wade at team.telstra.com
>>> The information contained in this e-mail message may be confidential.
>> If you are not the intended recipient, any use of, interference with,
> disclosure or copying of this material is unauthorised and prohibited.
> If you have received this message in error, please notify me by reply
> e-mail and then delete the message.
>>
>>
>> -----Original Message-----
>> From: Michael Sumner [mailto:mdsumner at utas.edu.au]
>> Sent: Thursday, 19 August 2004 10:18 AM
>> To: Briggs, Meredith M; r-help at stat.math.ethz.ch
>> Cc: Wade, Fiona M
>> Subject: Re: [R] Do you know if you can map a large minmum spanning tree
> in R?
>>
>>
>> At 09:47 AM 8/19/2004, Briggs, Meredith M wrote:
>>
>>
>>
>>>         Do you know
> if you can map in R?
>>>
> I have my minimum spanning tree, but as there are 1371 nodes
>> (all
>>> over Australia) I'd like to be able to "graph" them as they actually
> would be on the map.
>>>Do you know if this is possible?
>>
>> You can certainly "map" in R.  Depending on the coordinate system of
>> your
>> data . . .
>> but, e.g. - if it's lat/lon - perhaps the easiest way is to install the
> "maps" package and you can add the continental outlines to an existing
> plot:
>>
>> ## display nodes code here . . .
>> library(maps)
>>
>> There are plenty of other options, if you have your own map data (or
>> want
>> to use another source).  Feel free to provide more detail about your
> current plotting methods and coordinate system.
>>
>> Also, the package "mapdata" contains a high reso
>> lution continental
>> dataset
>> -"worldHires"
>>
>> Hope that helps, Mike.
>>
>>
>>
>>
>>
>> ###############################################
>>
>> Michael Sumner - PhD. candidate
>> Maths and Physics (ACE CRC & IASOS) and Zoology (AWRU)
>> University of Tasmania
>> Private Bag 77, Hobart, Tas 7001, Australia
>> Phone: 6226 1752
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide!
>> http://www.R-project.org/posting-guide.html
>
>
> --
> Roger Bivand
> NHH, Breiviksveien 40, N-5045 Bergen, Norway
>
>

--
Roger Bivand
NHH, Breiviksveien 40, N-5045 Bergen, Norway

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

```