R画NMDS

文摘   2024-12-14 23:37   甘肃  
#加载包library(openxlsx)library(vegan)library(ggplot2)library(ggrepel)setwd("E://数据")#读取数据,一般所需是数据行名为样本名的数据表spe <- read.xlsx('Mantest.xlsx', sheet = "全部NMDS")spe <- t(spe)otu.distance <- vegdist(spe, method = 'bray')#NMDS排序分析#egan包中的metaMDS函数df_nmds <- metaMDS(otu.distance, k = 2)#应力函数值(<=0.2合理)df_nmds_stress <- df_nmds$stressdf_nmds_stress#提取作图数据df_points <- as.data.frame(df_nmds$points)#添加samp1es变量df_points$samples <- row.names(df_points)#修改列名names(df_points)[1:2] <- c('NMDS1', 'NMDS2')head(df_points)#读入分组文件group <- read.xlsx('Mantest.xlsx', sheet = "NMDS组")#修改列名colnames(group) <- c("samples","group")#将绘图数据和分组合并df <- merge(df_points,group,by="samples")df#使用ggplot2包绘图#隐藏纵轴,并对字体样式、坐标轴的粗细、颜色、刻度长度进行限定;mytheme<-theme_bw()+  theme(axis.title.x = element_text(size = 18), #x标题        axis.title.y = element_text(size = 18),#y标题        axis.text.y = element_text(size = 12), #x刻度        axis.text.x = element_text(size = 12), #y刻度        panel.grid = element_blank(), #隐藏网格线        panel.border = element_rect(linewidth=1) #框线粗细        # legend.position = 'none' #隐藏图例  )color=c('#73bbaf',"#AB1D22","#354E6B",'#d15b64','#592c93', "#F7903D")#颜色变量p <- ggplot(data=df, aes(x=NMDS1, y=NMDS2, fill=group, color=group)) +    mytheme + # 主题设置    geom_point(shape = 21, size=3) + # 绘制点图并设定大小    geom_hline(yintercept = 0, linetype = 2) + # 添加原点垂直辅助线    geom_vline(xintercept = 0, linetype = 2) + # 添加原点水平辅助线    # geom_text_repel(    #   aes(label = samples),     #   size = 4,     #   min.segment.length = 0.2, # 调整这个值来控制标签之间的最小距离    #   force = 4, # 增加这个值来使标签更加分散    #   seed = 123 # 设置一个种子以确保结果的可重复性(可选)    # ) +    stat_ellipse(geom = "polygon", level = 0.90, alpha = 0.2) +    scale_color_manual(values = color) + # 点的颜色设置    scale_fill_manual(values = color) + # 椭圆颜色    ggtitle(paste('Stress=', round(df_nmds_stress, 3))) + # 添加应力函数值    labs(fill = "Group", color = "Group") # 添加图例标签  
# 显示图形 print(p)
###旱田小图spe <- read.xlsx('Mantest.xlsx', sheet = "农田NMDS")spe <- t(spe)otu.distance <- vegdist(spe, method = 'bray')#NMDS排序分析#egan包中的metaMDS函数df_nmds <- metaMDS(otu.distance, k = 2)#应力函数值(<=0.2合理)df_nmds_stress <- df_nmds$stressdf_nmds_stress#提取作图数据df_points <- as.data.frame(df_nmds$points)#添加samp1es变量df_points$samples <- row.names(df_points)#修改列名names(df_points)[1:2] <- c('NMDS1', 'NMDS2')head(df_points)#读入分组文件group <- read.xlsx('Mantest.xlsx', sheet ="NMDS农田组")#修改列名colnames(group) <- c("samples","group")#将绘图数据和分组合并df <- merge(df_points,group,by="samples")df#使用ggplot2包绘图#隐藏纵轴,并对字体样式、坐标轴的粗细、颜色、刻度长度进行限定;mytheme<-theme_bw()+ theme(axis.title.x = element_text(size = 18), #x标题 axis.title.y = element_text(size = 18),#y标题 axis.text.y = element_text(size = 12), #x刻度 axis.text.x = element_text(size = 12), #y刻度 panel.grid = element_blank(), #隐藏网格线 panel.border = element_rect(linewidth=1) #框线粗细 # legend.position = 'none' #隐藏图例 )color=c("#AB1D22","#354E6B",'#d15b64','#592c93', "#F7903D")#颜色变量p1 <- ggplot(data=df, aes(x=NMDS1, y=NMDS2, fill=group, color=group)) + mytheme + # 主题设置 geom_point(shape = 21, size=3) + # 绘制点图并设定大小 geom_hline(yintercept = 0, linetype = 2) + # 添加原点垂直辅助线 geom_vline(xintercept = 0, linetype = 2) + # 添加原点水平辅助线 # geom_text_repel( # aes(label = samples), # size = 4, # min.segment.length = 0.2, # 调整这个值来控制标签之间的最小距离 # force = 4, # 增加这个值来使标签更加分散 # seed = 123 # 设置一个种子以确保结果的可重复性(可选) # ) + stat_ellipse(geom = "polygon", level = 0.90, alpha = 0.2) + scale_color_manual(values = color) + # 点的颜色设置 scale_fill_manual(values = color) + # 椭圆颜色 ggtitle(paste('Stress=', round(df_nmds_stress, 3))) + # 添加应力函数值 labs(fill = "Group", color = "Group") # 添加图例标签
# 显示图形 print(p1)



麦当的生态学笔记
用于文献信息解读和分享,统计分析相关方法的传递。
 最新文章