首页 > 解决方案 > ggplot2 版本的 shepard 情节,即 vegan::stressplot()?

问题描述

似乎有相当多的信息用于绘制 NMDS 输出(即 NMDS1 与 NMDS1),ggplot2但是我找不到vegan::stressplot()使用ggplot2.

有没有办法产生一个输出ggplot2版本metaMDS

可重现的代码

library(vegan)

set.seed(2)

community_matrix = matrix(
  sample(1:100,300,replace=T),nrow=10,
  dimnames=list(paste("community",1:10,sep=""),paste("sp",1:30,sep="")))

example_NMDS=metaMDS(community_matrix, k=2) 

stressplot(example_NMDS)

reprex 包于 2021-09-17 创建(v2.0.1)

标签: rggplot2vegan

解决方案


这是一种使用 . 绘制非常相似的绘图的解决方法ggplot2。诀窍是获取 的结构stressplot(example_NMDS)并提取存储在该对象中的数据。我使用了包含该功能的tidyverseggplot和其他包,例如tidyr包含该pivot_longer功能的包。

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()

应力图


推荐阅读