This tutorial shows how you can create Shepard's Plot using GGPLOT2.
Here is an example to plot a very similar plot using ggplot2
.The method was to get the structure of the stressplot(example_NMDS)
and extract the data stored in that object.
Shepard's Plot Using GGPLOT2 Example
The below example uses the tidyverse
package that includes ggplot
and other packages such as tidyr
that contains the pivot_longer
function.
library(vegan) library(tidyverse) # Analyze the structure of the stressplot # Notice there's an x, y and yf list str(stressplot(example_NMDS)) # Create a tibble that contains the data from stressplot df <- tibble(x = stressplot(example_NMDS)$x, y = stressplot(example_NMDS)$y, yf = stressplot(example_NMDS)$yf) %>% # Change data to long format pivot_longer(cols = c(y, yf), names_to = "var") # Create plot df %>% ggplot(aes(x = x, y = value)) + # Add points just for y values geom_point(data = df %>% filter(var == "y")) + # Add line just for yf values geom_step(data = df %>% filter(var == "yf"), col = "red", direction = "vh") + # Change axis labels labs(x = "Observed Dissimilarity", y = "Ordination Distance") + # Add bw theme theme_bw()
Output:
Credit: Jonathan V. Solórzano