Skip to content

Instantly share code, notes, and snippets.

@fkeck
Created April 25, 2020 17:38
Show Gist options
  • Select an option

  • Save fkeck/f649cff21ab9cc1fb302042afc338a4c to your computer and use it in GitHub Desktop.

Select an option

Save fkeck/f649cff21ab9cc1fb302042afc338a4c to your computer and use it in GitHub Desktop.
tidy vegan procrustes errors plot for ggplot2
library(vegan)
library(tidyverse)
data(varespec)
vare.dist <- vegdist(wisconsin(varespec))
mds.null <- monoMDS(vare.dist, y = cmdscale(vare.dist))
mds.alt <- monoMDS(vare.dist)
x <- protest(mds.alt, mds.null)
rot <- x$rotation
tails <- tibble(X_IN = x$Yrot[, 1], Y_IN = x$Yrot[, 2])
heads <- tibble(X_OUT = x$X[, 1], Y_OUT = x$X[, 2])
tab <- bind_cols(tails, heads)
ggplot(tab) +
geom_hline(aes(yintercept = 0), color = "grey70", size = 0.5) +
geom_vline(aes(xintercept = 0), color = "grey70", size = 0.5) +
geom_abline(aes(slope = rot[1,2]/rot[1,1], intercept = 0), linetype = "dashed", color = "grey70", size = 0.5) +
geom_abline(aes(slope = rot[2,2]/rot[2,1], intercept = 0), linetype = "dashed", color = "grey70", size = 0.5) +
geom_point(aes(x = X_IN, y = Y_IN)) +
geom_segment(aes(x = X_IN, y = Y_IN, xend = X_OUT, yend = Y_OUT), arrow = arrow(angle = 20, length = unit(0.02, "npc"), type = "closed")) +
xlab("Dimension 1") +
ylab("Dimension 2") +
labs(title = " Procrustes errors") +
theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment