【问题标题】:ggplot2 version of shepard plot, i.e. vegan::stressplot()?ggplot2 版本的 shepard 图,即 vegan::stressplot()?
【发布时间】:2021-11-11 22:47:58
【问题描述】:

似乎有相当多的信息可用于使用ggplot2 绘制 NMDS 输出(即 NMDS1 与 NMDS1),但是我找不到使用ggplot2 绘制vegan::stressplot()(谢泼德图)的方法。

有没有办法产生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 package (v2.0.1) 于 2021-09-17 创建

【问题讨论】:

  • 您的具体编程问题是什么?要求软件推荐是题外话
  • 使用 ggplot2 生成压力图(谢泼德图)。我现在重新表述这个问题。谢谢。

标签: r ggplot2 vegan


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    相关资源
    最近更新 更多