[R] label plotting on nmds diagram

Dave Roberts droberts at montana.edu
Thu Nov 15 01:14:26 CET 2007


Using nmds from package labdsv will work, but you are confusing some 
elements with ordiplot from package vegan.  The plot function in labdsv 
does not produce a new object, it simply plots the requested ordination. 
  There is an accompanying command "plotid" that puts labels on the 
ordonation.  By default, it simply numbers them, but you can specify a 
vector of names of labels with the optional argument ids= (see 
?plotid.nmds), so

library(labdsv)
data(bryceveg)
data(brycesite)
dis.bc <- dsvdis(bryceveg,'bray') # Bray-Curtis dissimilarity
nmds.bc <- nmds(dis.bc)
plot(nmds.bc)
plotid(nmds.bc,ids=row.names(brycesite))

will put more complex row.names on items selected with a mouse.  It's 
tedious if you want to label ALL the points. but it will allow you to 
minimize overlapping labels.

You can also use it to print the values of categorical variables, rather 
than glyphs,

plotid(nmds.bc,ids=brycesite$depth)

Gavin is correct that NMDS doesn't have species coordinates, only sample 
coordinates.  package vegan addresses this by calculating the weighted 
centroid of all samples containing a species as the species location, 
using function 'wascores'.  This is a post-hoc calculation, and species 
don't influence the configuration of the ordination except as they 
influence the underlying dissimilarity/distance matrix.  wascores also 
works with nmds, as

spec.loc <- wascores(nmds.bc$points,bryceveg)
points(spec.loc,pch="+",col=2)

Alternatively, you could switch the entire operation to package vegan, 
using metaMDS and associated plotting routines.  metaMDS uses a number 
of arguments to specify whether to use step-acxross distances, and uses 
a number of random initial starts.  The default with nmds is to use a 
pco as the initial conditions, but random starts are also available in 
labdsv with bestnmds.


Good luck, Dave Roberts

Gavin Simpson wrote:
> On Wed, 2007-11-14 at 15:19 -0300, Antonio Olinto wrote:
>> Hi Gavin, thanks for your message.
>>
>> You are right. The function nmds is from library labdsv and not from vegan. My
>> mistake.
>>
>> Well, that's what I did:
>>
>> library(vegan)
>> library(labdsv)
>> nms.euc <- nmds(dis.euc,4)
>> initial  value 13.457832 
>> iter   5 value 8.589716
>> iter  10 value 7.989993
>> iter  15 value 7.734503
>> iter  20 value 7.539719
>> iter  25 value 7.438721
>> final  value 7.397254 
>> converged
>>
>> plot(nms.euc)
>> text(nms.euc$points[,1],nms.euc$points[,2],labels=row.names(nms.euc$points),pos=4,cex=0.7)
>> # got overwritten labels
>>
>> identify(plot(nms.euc))
>> numeric(0)
>>
>> It seems that ordiplot is not suitable to nmds but only to cca and rda.
> 
> Hi Antonio,
> 
> You haven't used ordiplot there or created an object of class "ordiplot"
> so the problem is that you expected identify to work with an arbitrary
> object. Someone would need to write an identify method that could deal
> with whatever plot.nmds returns for what you have above to work.
> 
> ordiplot is suitable for lots of ordination types (effectively anything
> that scores() can deal with) I was just not familiar with nmds() from
> labdsv. Having looked at this, nmds() is an even simpler wrapper round
> isoMDS() from MASS.
> 
>> But you understood exactly what I want. Your example showed for cca what I want
>> for nmds plot.
> 
> Only because it is easier to write the cca code without having to wait
> for metaMDS to finish the random starts ;-)
> 
> Here's what I think you need but using vegan and metaMDS for the random
> starts and other NMDS sugar...:
> 
> require(vegan)
> data(varespec)
> nms.euc <- metaMDS(varespec, distance = "euclidean", k = 4)
> plt <- plot(nms.euc, display = "sites", type = "p")
> identify(plt, what = "sites")
> 
> I also forgot to mention that NMDS doesn't have species scores as such,
> so your example has nothing to do with species as far as I can see - did
> you omit something else?
> 
> vegan does allow you to get "species" scores via weighted averages
> (see ?scores.metaMDS ), e.g.
> 
> plt2 <- plot(nms.euc, display = c("species","sites"), type = "p", shrink
> = TRUE)
> identify(plt2, what = "species", cex = 0.8, col = "red")
> identify(plt2, what = "sites", cex = 0.8, col = "black")
> 
> Is this what you are looking for?
> 
> If you stick with labdsv:::nmds then take a look at plotid.nmds() which
> is a wrapper around identify for nmds objects, which appears to do the
> hard work for you. You could look at this to see how you could have gone
> about crafting the identify call yourself - the function is very simple.
> 
> Alternatively, do look at orditorp() in vegan, as it is a nice function
> that allows you to display only labels for species/sites that can fit:
> 
> plot(nms.euc, display = c("species","sites"), type = "n", shrink = TRUE)
> points(nms.euc, display = "sites", cex = 0.7)
> orditorp(nms.euc, display = "species", shrink = TRUE, col = "red")
> 
> The orditorp call will generate some warnings - they are harmless and
> have been fixed in the latest development version of vegan on R-Forge.
> Jari is planning a release of vegan real soon now that will contain this
> fix.
> 
> Note that orditorp has a priority argument, which you can use to give
> labelling priority to species/sites, say based on their maximal
> abundance or number of occurrences or similar; see ?orditorp
> 
> HTH
> 
> G
> 
>> Best regards,
>>
>> Antonio
>>
>>
>> Citando Gavin Simpson <gavin.simpson at ucl.ac.uk>:
>>
>>> On Wed, 2007-11-14 at 11:22 -0300, Antonio Olinto wrote:
>>>> Hi,
>>>>
>>>> I'm using nmds command (library vegan) to analyze some fishing data.
>>> I doubt it - there is no nmds command. Do you mean metaMDS() ? This uses
>>> isoMDS() from package MASS with some extra features.
>>>
>>>> I'd like to plot not only points, but also the names of species and
>>> stations in
>>>> a specified position.
>>>>
>>>> I used the command
>>>> text(nmds$points[,1], nmds $points[,2],labels=row.names(nmds
>>>> $points),pos=3,cex=0.5)
>>>>
>>>> But the labels are sometimes overlapped.
>>>>
>>>> Is there any way to use identify, or a similar command, to plot the row
>>> names in
>>>> a given position? Identify would be perfect but it indicates the row number
>>> and
>>>> I'd like to have the row name given in nmds$points.
>>> Have a look at ?orditorp in vegan for an alternative approach, but yes
>>> you can use identify, but you have to capture the output of the plot
>>> call and use that in your identify call:
>>>
>>> require(vegan)
>>> data(varespec)
>>> mod <- cca(varespec)
>>> mod.plt <- plot(mod, display = "species", type = "p")
>>> identify(mod.plt)
>>>
>>> See ?identify.ordiplot for more info.
>>>
>>> HTH
>>>
>>>> Many thanks,
>>>>
>>>> Antonio Olinto
>>>> Sao Paulo Fisheries Institute
>>>> Brasil
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> -------------------------------------------------
>>>> BCMG Internet Webmail
>>>> www.bcmg.com.br
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>> -- 
>>> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>>>  Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
>>>  ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
>>>  Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
>>>  Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
>>>  UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
>>> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>>>
>>>
>>
>>
>>
>> -------------------------------------------------
>> BCMG Internet Webmail
>> www.bcmg.com.br
>>


-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
David W. Roberts                                     office 406-994-4548
Professor and Head                                      FAX 406-994-3190
Department of Ecology                         email droberts at montana.edu
Montana State University
Bozeman, MT 59717-3460



More information about the R-help mailing list