Sarah Berke
2010-Apr-07 03:05 UTC
[R] Customizing ordination plots using symbols for factors
Hi, I could use a hand solving a fairly straightforward ordination plot problem: I am conducing NMDS on some community data for roughly 300 localities and 650 species. I have a community matrix, a species attribute matrix, and an environmental attributes matrix. After running metaMDS {vegan} on the community matrix, I can successfully use either of the other two matrices to draw hulls around points belonging to various classes of species attributes or environmental characteristics. I can also use either matrix to run envfit and plot vectors for different variables. But what I really want to do is just display the species ordination and color code the points by levels of a particular factor from the species attribute matrix. For example, I've got 650 species that have 5 different feeding mechanisms, so I want each feeding mechanism to be a different color (or different shape or whatever). I was sure that this would be easy to figure out. But I have been scouring help files and R tutorials for 2 days, and I am at my wits end! If my points and my factors were in a single dataframe then I would know how to do it, but I don't see how to make the matrix with the factors talk to the ordination result (which is typeof=list). I assume that with() must be involved, but how? I suspect that I've actually read the relevant help files, but did not recognize the answer as such. All of the vegan documentation I've found seems to assume that this is so basic that anyone could figure it out on their own. FWIW, here is an example of my code for plotting convex hulls around 3 different feeding groups, which works. comm.mds is the ordination of the community matrix comm.mat, FD.mat is the species attributes matrix. comm.mds<- metaMDS(comm.mat, distance = "euclidean", k = 2, zerodist "add", noshare = 0) plot(comm.mds, type="p", display="species") with(FD.mat, ordihull(comm.mds.t, feeding, show.groups = "1", col = "yellow", lty = 1, lwd=3)) with(FD.mat, ordihull(comm.mds.t, feeding, show.groups = "2", col = "black", lty = 1, lwd=3)) t with(FD.mat, ordihull(comm.mds.t, feeding, show.groups = "3", col = "green", lty = 1, lwd=3)) Any guidance would be greatly appreciated! Many thanks, Sarah
Jari Oksanen
2010-Apr-07 09:15 UTC
[R] Customizing ordination plots using symbols for factors
Sarah Berke <skberke <at> gmail.com> writes: r (or different shape or whatever).> > I was sure that this would be easy to figure out. But I have been > scouring help files and R tutorials for 2 days, and I am at my wits > end! If my points and my factors were in a single dataframe then I > would know how to do it, but I don't see how to make the matrix with > the factors talk to the ordination result (which is typeof=list). I > assume that with() must be involved, but how? I suspect that I've > actually read the relevant help files, but did not recognize the > answer as such. All of the vegan documentation I've found seems to > assume that this is so basic that anyone could figure it out on their > own.Sarah, You can extract specificy types of scores from the list type result using function scores() which returns a matrix of scores: scores(comm.mds, display="sp") You can use function points() to directly add a specific set of points to an ordination graph. The following example first draws an empty plot (with only frames and axes), and then adds species scores as points of different colours and symbols: plot(comm.mds, type = "n") points(comm.mds, dis="sp", pch = as.numeric(FD.mat$feeding), col as.numeric(FD.mat$feeding)) This works only with vegan functions, but you seemed to be using them. Best wishes, Jari Oksanen