[R] Vegan plotting- color help
Gavin Simpson
gavin.simpson at ucl.ac.uk
Fri Apr 11 17:10:58 CEST 2008
On Fri, 2008-04-11 at 10:43 -0400, stephen sefick wrote:
> that worked just fine. I missed the fact that the scores could be
> extracted- I am learning how to think like a programmer it is just
> taking time. Thank you very much for your help
>
> this is the code that I settled on:
>
> sol <- metaMDS(x, distance="jaccard")
>
> shrink <- FALSE ## see argument shrink in ?metaMDS
>
> ## get species and site scores
> spp.sc <- scores(sol, display = "species", shrink = shrink)
> site.sc <- scores(sol, display = "sites")
>
> ## work out the ranges
> ylim <- range(spp.sc[,2], site.sc[,2])
> xlim <- range(spp.sc[,1], site.sc[,1])
>
> ## set-up the plotting region
> plot(site.sc, xlim = xlim, ylim = ylim, type = "n", asp = 1,
> xlab = "NMDS1", ylab = "NMDS2")
You can do all the points() calls below in one go by specifying the
vector of colours:
cols <- rep(c("red", "blue", "yellow", "green", "violet", "orange",
"darkred", "darkblue", "black", "bisque", "cyan", "azure", "chartreuse",
"khaki", "brown","aquamarine", "chocolate", "cornflowerblue",
"darkgoldenrod", "darkgreen", "darkolivegreen", "burlywood"),
times = c(22,23,18,22,21,21,21,24,23,23,....... etc))
then
points(site.sc, col = cols)
would have done that for you in a oner.
Also, note that 1:22 doesn't need to be wrapped in c( ). And, you'd save
yourself some writing if you ask scores to return only axis one and two
scores. Then you wouldn't need the ,c(1:2) bit either.
Easier in this instance would have been to use a grouping variable:
## dummy group ID
group <- sample(rep(1:3, each = 4))
## create dummy grouping factor
group <- factor(group, labels = paste("group", 1:3))
group
# vector of colours
cols <- c("red", "blue", "green")
## dummay data to plot
datx <- runif(12)
daty <- runif(12)
## plot it
plot(datx, daty, col = cols[group], pch = 16)
Notice how we index cols by group ( cols[group] ) in the above call to
plot. group is stored as a sequence of 1's, 2's and 3's (in this case as
we have 3 levels/groups). To see what R is seeing, try this:
> cols[group]
[1] "green" "green" "red" "blue" "red" "red" "blue" "green" "red"
[10] "blue" "green" "blue"
So you see that cols is expanded to the correct length according to what
the level of 'group' is.
HTH
G
> ## add the row
> points(site.sc[c(1:22),c(1:2)], col="red")
> points(site.sc[c(23:45),c(1:2)], col="blue")
> points(site.sc[c(46:63),c(1:2)], col="yellow")
> points(site.sc[c(64:85),c(1:2)], col="green")
> points(site.sc[c(86:106),c(1:2)], col="violet")
> points(site.sc[c(107:127),c(1:2)], col="orange")
> points(site.sc[c(128:148),c(1:2)], col="purple")
> points(site.sc[c(148:171),c(1:2)], col="darkred")
> points(site.sc[c(172:194),c(1:2)], col="darkblue")
> points(site.sc[c(195:217),c(1:2)], col="black")
> points(site.sc[c(218:233),c(1:2)], col="bisque")
> points(site.sc[c(234:256),c(1:2)], col="cyan")
> points(site.sc[c(257:262),c(1:2)], col="azure")
> points(site.sc[c(263:267),c(1:2)], col="chartreuse")
> points(site.sc[c(268:271),c(1:2)], col="khaki")
> points(site.sc[c(272:276),c(1:2)], col="brown")
> points(site.sc[c(277:279),c(1:2)], col="aquamarine")
> points(site.sc[c(279:286),c(1:2)], col="chocolate")
> points(site.sc[c(287:294),c(1:2)], col="cornflowerblue")
> points(site.sc[295,c(1:2)], col="coral")
> points(site.sc[c(296:299),c(1:2)], col="darkgoldenrod")
> points(site.sc[c(300:301),c(1:2)], col="darkgreen")
> points(site.sc[302,c(1:2)], col="darkolivegreen")
> points(site.sc[c(303:307),c(1:2)], col="burlywood")
> text(spp.sc, labels = colnames(x), cex = 0.8)
>
> On Fri, Apr 11, 2008 at 4:01 AM, Gavin Simpson <gavin.simpson at ucl.ac.uk> wrote:
> > On Thu, 2008-04-10 at 23:34 -0400, stephen sefick wrote:
> > > I have looked all over the internet for being able to color sites
> > > differently in a plot of an MDS (metaMDS)- I would like to color the
> > > different sites in the ordination plot (plot or ordiplot). I have set
> > > the matrix up so that my site code is 1 .... 296 (first column). is
> > > there a way to make 1-23 blue, 24-40 red etc.
> > > thanks
> > >
> > > Stephen
> > >
> >
> > This requires a bit of manipulation. Using the example from ?metaMDS
> >
> > data(dune)
> > require(MASS)
> > sol <- metaMDS(dune)
> >
> > shrink <- FALSE ## see argument shrink in ?metaMDS
> >
> > ## these are the plotting colours, must be a vector of the same
> > ## length (nrow) as the input data, here 20
> > cols <- rep(c("red","blue","green","black"), each = 5)
> >
> > ## get species and site scores
> > spp.sc <- scores(sol, display = "species", shrink = shrink)
> > site.sc <- scores(sol, display = "sites")
> >
> > ## work out the ranges
> > ylim <- range(spp.sc[,2], site.sc[,2])
> > xlim <- range(spp.sc[,1], site.sc[,1])
> >
> > ## set-up the plotting region
> > plot(site.sc, xlim = xlim, ylim = ylim, type = "n", asp = 1,
> > xlab = "NMDS1", ylab = "NMDS2")
> >
> > ## add the row
> > text(site.sc, col = cols, labels = rownames(dune), cex = 0.8)
> > text(spp.sc, labels = colnames(dune), cex = 0.8)
> >
> > is one way to do this,
> >
> > HTH
> >
> > G
> >
> > --
> > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> > 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
> > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> >
> >
>
>
>
> --
> Let's not spend our time and resources thinking about things that are
> so little or so large that all they really do for us is puff us up and
> make us feel like gods. We are mammals, and have not exhausted the
> annoying little problems of being mammals.
>
> -K. Mullis
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
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