r - 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)
解决方案
这是一种使用 . 绘制非常相似的绘图的解决方法ggplot2
。诀窍是获取 的结构stressplot(example_NMDS)
并提取存储在该对象中的数据。我使用了包含该功能的tidyverse
包ggplot
和其他包,例如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()
推荐阅读
- php - Collection pluck by relation id and get count
- javascript - range.setStart 后新字符出现在前一个节点中
- javascript - Spring message in javascript file
- python - TypeError: unorderable types: Tree() < int() (这在 Python 2 中运行良好)
- python - 如何使用 Stack Exchange API 获取答案的正文?
- php - 脚本 selenium python 总是被检测为 flashseats 上的 bot :(
- c# - WPF:将 TextBlock 与顶部的不同字体大小对齐
- c# - 如何使用 MongoDB C# 客户端驱动程序将复杂的嵌套 JSON 数据结构保存到 MongoDB?
- r - 避免重新计算时数据表变灰
- caching - Service Worker 未创建 networkFirst Cache