我在 NMDS 图的数据周围添加省略号时遇到困难。我希望有两个单独的多边形椭圆颜色填充与站点点协调的颜色。 当我向其中添加 stat_ellipse 时,它的颜色相同,并且似乎围绕所有点卷曲,而不是单独卷曲。我不太熟悉 NMDS 图,因此感谢您的帮助。
nmds.data_scores %>%
ggplot( aes(x=NMDS1, y=NMDS2)) +
geom_point(aes(NMDS1, NMDS2, colour = factor(site), shape = factor(Group)),
size = 3, alpha=0.7) +
geom_text(data = nmds.species_scores, aes(x = NMDS1, y = NMDS2, label = species), alpha = 0) +
geom_text(aes(label = site, colour = factor(site)), vjust = 1.7, show.legend = FALSE) +
labs(
title = "Ordination Plot(Years)",
colour = "Year Communities",
shape = "Temp. Grouping") +
scale_colour_manual(values = years_colors, guide = "none") + # Remove color legend
scale_shape_manual(values = years_only$point_shape, name = "Temp. Grouping") + # Add shape legend
coord_equal() +
theme_classic()+
theme(
panel.background = element_rect(fill = NA, colour = "black", size = 1, linetype = "solid"),
legend.position = c(0.10, 0.10),
legend.justification = c(0, 0),
legend.text = element_text(size = 12),
legend.title = element_text(size = 10, face = "bold"),
legend.key = element_blank(),
legend.box.background = element_rect(color = "black"),
) +
theme(
axis.text = element_text(size = 10),
axis.title = element_text(size=12,color="grey14",face="bold")) +
theme(plot.title = element_text(color = "#45ADA8", size=15,face="bold",hjust=0.5)) +
geom_hline(yintercept = 0, linetype = "dashed", color = "grey") +
geom_vline(xintercept = 0, linetype = "dashed", color = "grey")+
annotate("text", x = -1, y = 0.45,
label = paste0("stress: ", format(nmds$stress, digits = 4)), hjust = 0)
看起来你可以只使用
fill
的 stat_ellipse
参数来实现这一点,尽管没有示例数据很难判断。
这是使用示例数据集的代码,我认为它可以满足您的需求:
library(ggplot2)
library(vegan)
data(mtcars)
nmds <- metaMDS(mtcars)
nmds.data_scores <- as.data.frame(nmds$points)
nmds.data_scores$cyl <- as.factor(mtcars$cyl)
ggplot(nmds.data_scores, aes(x = MDS1, y = MDS2)) +
geom_point(aes(color = cyl)) +
stat_ellipse(aes(fill = cyl, color = cyl), geom = "polygon", alpha = 0.2) +
theme_classic()
让我知道这是否有帮助,或者不是您想要的。