Search code examples
rplotradixvegan

How do I customize ordination plot in Vegan


I have a CCA in Vegan and am trying to plot it, but only getting a very basic plot, I am wondering how I can customize the plot- change colors of the species words and arrows, put in points, changes size of words on plot... Here is the code I used to make the CCA and plot it (in Vegan):

spe.cca <- cca(spdata~.,env)
plot(spe.cca, choices=c(1,2), display=c('sp','bp'), scaling=2)

spdata is species abundance information and env is a matrix of environmental data. I just want to display environmental variables and species on the plot (not samples/sites).


Solution

  • I think ggfortify can help do the trick. You can use the CCA1 and CCA2 scores of the species abundance and environmental variables to draw the text and arrows needed. Like any ggplot you can set the color, size, etc for each element in the plot. The only detail is that you may need to scale the environmental variables position and arrows, in order to get a very similar plot to the one generated by plot(spe.cca). Here is the code using varechem and varespec datasets

    library(vegan)
    library(ggfortify)
    
    data(varespec)
    data(varechem)
    
    #CCA
    cca_model<-cca(varespec ~ .,data=varechem)
    plot(cca_model,choices=c(1,2), display=c('sp','bp'), scaling=2)
    
    #Get CCA scores
    df_species  <- data.frame(summary(cca_model)$species[,1:2])# get the species CC1 and CC2 scores
    df_environ  <- scores(cca_model, display = 'bp') #get the environment vars CC1 and CC2 scores
    
    cca1_varex<-round(summary(cca_model)$cont$importance[2,1]*100,2) #Get percentage of variance explained by first axis
    cca2_varex<-round(summary(cca_model)$cont$importance[2,2]*100,2) #Get percentage of variance explained by second axis
    
    #Set a scaling variable to multiply the CCA values, in order to get a very similar plot to the the one generated by plot(cca_model). You can adjust it according to your data
    scaling_factor <- 2
    
    ggplot(df_species, 
           aes(x=CCA1, y=CCA2)) + 
      #Draw lines on x = 0 and y = 0
      geom_hline(yintercept=0, 
                 linetype="dashed") +
      geom_vline(xintercept=0, 
                 linetype="dashed") +
      coord_fixed()+
      #Add species text
      geom_text(data=df_species, 
                aes(x=CCA1,#Score in CCA1 to add species text
                    y=CCA2,#Score in CCA2 to add species text
                    label=rownames(df_species),
                    hjust=0.5*(1-sign(CCA1)),#Set the text horizontal alignment according to its position in the CCA plot
                    vjust=0.5*(1-sign(CCA2))),#Set the text vertical alignment according to its position in the CCA plot
                color = "forestgreen")+
      #Add environmental vars arrows
      geom_segment(data=df_environ, 
                   aes(x=0, #Starting coordinate in CCA1 = 0 
                       xend=CCA1*scaling_factor,#Ending coordinate in CCA1  
                       y=0, #Start in CCA2 = 0
                       yend=CCA2*scaling_factor), #Ending coordinate in CCA2 
                   color="firebrick1", #set color
                   arrow=arrow(length=unit(0.01,"npc"))#Set the size of the lines that form the tip of the arrow
                   )+
      #Add environmental vars text
      geom_text(data=df_environ, 
                aes(x=CCA1*scaling_factor, 
                    y=CCA2*scaling_factor,
                    label=rownames(df_environ),
                    hjust=0.5*(1-sign(CCA1)),#Add the text of each environmental var at the end of the arrow
                    vjust=0.5*(1-sign(CCA2))),#Add the text of each environmental var at the end of the arrow 
                color="firebrick1")+
      #Set bw theme
      theme_bw()+
      #Set x and y axis titles
      labs(x=paste0("CCA1 (",cca1_varex," %)"),
           y=paste0("CCA2 (",cca2_varex," %)"))