Search code examples
rvegan

Color RDA vectors by groups in R


I'm plotting a series of RDAs in R, each of which has 10+ environmental vectors. The environmental variables each fall in to one of 5 categories. I'd like the vector colors to reflect these categories. I did it in a somewhat ghetto fashion by producing the raw black and white plot, then tracing it in Powerpoint (super time consuming), but there are ALOT of inherent issues with this but it looks good at least. Here's that plot, note the vector colors. enter image description here

I'd like to have my output be an R native plot. The plot I use to make the base plot is currently:

#download the data#
env<- read.csv("environmenta data.csv")
bio<- read.csv("bio data.csv")

#break out the groups of environmental vectors#
#these are purley for example#
group1<-as.matrix(subset(env,select=c(a,b,c))
group2<-as.matrix(subset(env,select=c(d,e,f,h))
group3<-as.matrix(subset(env,select=c(g))
group4<-as.matrix(subset(env,select=c(j,k,l,o))
group5<-as.matrix(subset(env,select=c(m,n,))

#run the RDA#
rda1<-rda(bio,env)

#Plot it#
plot(rda1,type="n",bty="n",main="",
     xlab="XX% variance explained",
     ylab="XX% variance explained",
     col.main="black",col.lab="black", col.axis="white",
     xaxt="n",yaxt="n")
#points(rda1,display="species",col="gray",pch=20)
#option to display species points
#text(rda2,display="species",col="gray") 
#option for species labels

points(rda1,display="cn",col="black",lwd=2)#<<< guessing this is the key statement for my issue, but not sure
text(rda1,display="cn",col="black",cex=0.5)

I'm assuming the answer falls in this points--> col="..." command, but I'm not sure how to tell it to subset by groups. Any and all help would be appreciated.


Solution

  • library(vegan)
    
    #DATA
    data(varespec)
    data(varechem)
    env = varechem
    bio = varespec
    rm(list = c("varechem", "varespec"))
    
    #Assign colors based on groups for the columns of env
    groups = rep(c("red", "blue"), length.out = NCOL(env))
    
    rda1 = rda(X = bio, Y = env)
    plot(rda1, type = "n")
    points(rda1)
    text(rda1, display = "bp", col = groups)