ggplot2veganrda

How to display centroids for categorical variables instead of arrows using function ggord?


I really can’t figure out how to display just the centroids for my categorical variables using the function ggord. If anybody could help me, that would be great. Here is an example of what I’m trying to achieve using the dune data set:

    library(vegan)
    library (ggord)
    library(ggplot2)

    ord <- rda(dune~Moisture+ Management+A1,dune.env)
    
#first plot
    plot(ord)

# second plot
    ggord(ord)
#I tried to add the centroids, but somehow the whole plot seems to be differently scaled?

centroids<-ord$CCA$centroids
ggord(ord)+geom_point(aes(centroids[,1],centroids[,2]),pch=4,cex=5,col="black",data=as.data.frame(centroids)) 

In the first plot only the centroids (instead of arrows) for moisture and management are displayed. In the ggord plot every variable is displayed with an arrow.

And why do these plots look so different? The scales of the axes is totally different?


Solution

  • Something like this could work - you can use the var_sub argument to retain specific predictors (e.g., continuous), then just plot others on top of the ggord object.

    library(vegan)
    library(ggord)
    library(ggplot2)
    
    data(dune)
    data(dune.env)
    ord <- rda(dune~Moisture+ Management+A1,dune.env)
    
    # get centroids for factors
    centroids <- data.frame(ord$CCA$centroids)
    centroids$labs <- row.names(centroids)
    
    # retain only continuous predictors, then add factor centroids
    ggord(ord, var_sub = 'A1') +
      geom_text(data = centroids, aes(x = RDA1, y = RDA2, label = labs))
    

    enter image description here