Plotting envfit vectors (vegan package) in ggplot2

后端 未结 4 2069
南旧
南旧 2020-12-09 05:45

I am working on finalizing a NMDS plot that I created in vegan and ggplot2 but cannot figure out how to add envfit species-loading vectors to the plot. When I try to it says

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 06:27

    Short addition: To get a full representation of the plot.envfit functionality within ggplot2 aka "arrow lengths make full use of plot area" a factor needs to be applied. I don't know if it was intentionally left out in the answers above, as it was even specifically mentioned by Gavin? Just extract the required scaling factor using arrow_factor <- ordiArrowMul(vf) and then you can either apply it to both NMDS columns in spp.scrs or you can do this manually like

    arrow_factor <- ordiArrowMul(vf)
    spp.scrs <- as.data.frame(scores(vf, display = "vectors")) * arrow_factor
    spp.scrs <- cbind(spp.scrs, Species = rownames(spp.scrs), Pvalues = vf$vectors$pvals, R_squared = vf$vectors$r)
    
    # select significance similarly to `plot(vf, p.max = 0.01)`
    spp.scrs <- subset(spp.scrs, Pvalues < 0.01)
    
    # you can also add the arrow factor in here (don't do both!)
    ggplot(scrs) +
      geom_point(mapping = aes(x = NMDS1, y = NMDS2, colour = Group)) +
      coord_fixed() + ## need aspect ratio of 1!
      geom_segment(data = spp.scrs,
                   aes(x = 0, xend = NMDS1 * arrow_factor, y = 0, yend = NMDS2 * arrow_factor),
                   arrow = arrow(length = unit(0.25, "cm")), colour = "grey") +
      geom_text(data = spp.scrs, aes(x = NMDS1 * arrow_factor, y = NMDS2 * arrow_factor, label = Species),
                size = 3)
    

提交回复
热议问题