[R] ordisymbol - changing symbols used in plotting factor levels

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Jan 27 10:42:01 CET 2010


On Tue, 2010-01-26 at 19:59 -0400, Colin Curry wrote:
> Hello,
> 
> I'm trying plot points in an NMDS according to a factor with two levels:
> 
> fig<-ordiplot(canod.sol,  
> type="none",cex.axis=0.9,cex.lab=0.1,pty="m",tck=-0.01)
> ordisymbol(fig, y = hab, factor = "habitat", rainbow = T,col = env,  
> legend = F)
> 
> This gets me part of the way - It produces a plot with blue triangles  
> for the first factor level and red circles for the second level. What  
> I want to do is change the symbols so they all plot as circles, then  
> change the colours used to plot each factor level (e.g. I want blue  
> circles and red circles). Any advice on how to do this?

You can't, not with the way that function is currently implemented. It
would have helped if you'd told us that this was in BiodiversityR ---
originally I thought it was in vegan.

You have to hack the code yourself. The example below, I think, will now
do what you want:

`Myordisymbol` <-
function(ordiplot,y,factor,col=1,rainbow=T,legend=T,...) {
    ordiscores <- scores(ordiplot, display="sites")
    groups <- table(y[,factor])
    m <- length(groups)
    if (m > 25) {
        warning("Symbol size was kept constant as there were more than 25 categories (> number of symbols that are currently used in R)")
        rainbow <- T
    }
    levels <- names(groups)
    if (rainbow == T) {palette(rainbow(m))}
    for (i in 1:m) {
        subs <- y[,factor]==levels[i]
        for (q in 1:length(subs)) {
            if(is.na(subs[q])) {subs[q]<-F}
        }
        scores <- ordiscores[subs,,drop=F]
        if (rainbow == T && m < 26) {
            ## Keep plotting char same for all groups
            ##points(scores[,1],scores[,2],pch=i,col=i,...)
            points(scores[,1],scores[,2],pch=1,col=i,...)
        }
        if (rainbow == T && m > 25) {
            points(scores[,1],scores[,2],pch=19,col=i,...)
        }
        if (rainbow == F) {
            ## Keep plotting char same for all groups
            ##points(scores[,1],scores[,2],pch=i,col=col,...)
            points(scores[,1],scores[,2],pch=1,col=col,...)
        }
    }
    if (legend==T) {
        if (rainbow==T && m<26) {legend(locator(1),legend=levels,pch=c(1:m),col=c(1:m))}
        if (rainbow==T && m>25) {legend(locator(1),legend=levels,pch=rep(19,m),col=c(1:m))}
        if (rainbow==F) {legend(locator(1),legend=levels,pch=c(1:m))}
    }
    palette("default")
}

But that isn't using your (nor was it ever if the call you show above is
correct) setting of 'col' to 'env'. As you have rainbow = TRUE and m
*is* < 26 (m == 2 in your case), the colour used to plot is colour i =
{1,...,m} using the rainbow palette.

Anyway, HTH

G

> 
> Thanks,
> Colin Curry
> --
> Colin Curry
> Ph.D. Candidate
> Canadian Rivers Institute
> Department of Biology
> University of New Brunswick, Fredericton
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list