笔记—R语言NMDS

##########NMDS
install.packages("devtools")
install.packages("usethis")
install.packages("sessioninfo")
library(sessioninfo)
library(usethis)
library(devtools)
devtools::install_github('YuLab-SMU/ggtree')
if (!requireNamespace("BiocManager", quietly = TRUE))
  install.packages("BiocManager")
devtools::install_github("GuangchuangYu/gglayer")
devtools::install_github('GuangchuangYu/yyplot')
"vqv/ggbiplot"
BiocManager::install("ggtree")
library(gglayer)
library(ggtree)
library(yyplot)
library(plyr)
library(scales)
library(grid)
library(ggbiplot)
library(permute)
library(lattice)
library(vegan)
library(ggplot2)

sample <- read.table(file.choose(),sep = "\t",row.names = 1)#第一行为列名
group <- read.table(file.choose(),sep = "\t",header = T,row.names = 1)#其中header=T表示将文件中第一行设为列名字。row.names= 1表示第一列设为行名
group1 <- group[match(rownames(sample),rownames(group)),] #匹配行名,很重要group = group[rownames(sample),]

#nmds分析
nmds1 <- metaMDS(sample, distance = 'bray', k = 3)
summary(nmds1)
#提取数据
nmds1.stress <- nmds1$stress
nmds1.point <- data.frame(nmds1$point)
nmds1.species <- data.frame(nmds1$species)
sample_site <- nmds1.point[1:2]
sample_site$names <- rownames(sample_site)
colnames(sample_site)[1:2] <- c('NMDS1', 'NMDS2')
#合并分组数据
sample_site <- cbind(sample_site,group1)
#给分组排序
sample_site$Depth <- factor(sample_site$Depth,levels =c('0m','25m','50m','117m','150m', '200m',
                            ordered = FALSE ))
sample_site$Particlesize <- factor(sample_site$Particlesize, levels = c("0.2-0.6μm","0.6-1.2μm","1.2-2μm","2-20μm",">20μm",
                                   ordered = FALSE))
View(sample_site)
#NMDS图绘制
windowsFonts(TNM = windowsFont("Times New Roman"))#设置字体
nmds_plot <- ggplot() +
  geom_point(data = sample_site, aes(NMDS1, NMDS2,color = Particlesize, shape = Depth), size = 5, alpha = 0.8) + #可在这里修改点的透明度、大小
  scale_shape_manual(values = c(15,17,18,19,20,79)) + #可在这里修改点的形状
  scale_color_manual(values = c("#99CCFF","#FF9999","#FFCC33","#99CC99",
                                "#996699" )) + #可在这里修改点的颜色
  scale_fill_manual(values = c("black" ),aesthetics = c("fill")) +#填充颜色
 #geom_ord_ellipse(aes(sample_site$NMDS1,sample_site$NMDS2,group= sample_site$Particlesize),ellipse_pro = 0.8,linetype=2,size=0.7,color='firebrick')+##添加0.8置信椭圆
  #geom_ord_ellipse(aes(sample_site$NMDS1,sample_site$NMDS2,group= sample_site$Particlesize,color= sample_site$Particlesize),ellipse_pro = 0.9,linetype=3,size=1)+ ##添加0.9置信椭圆
  theme(panel.grid = element_blank(), panel.background = element_rect(color = 'black', fill = 'transparent')) + #去掉背景  
  theme(legend.key = element_rect(fill = 'transparent'), legend.text =element_text(family="TNM"), legend.title = element_blank()) + #去掉图例标题及标签背景
  labs(x = 'Dimension1', y = 'Dimension2', title = paste('Stress =', round(nmds1$stress, 2))) +
  theme(plot.title = element_text(hjust = 0.8,family="TNM",face = "bold"))+  #标题
  theme(axis.text.x =element_text(family="TNM",face = "bold"),axis.text.y =element_text(family="TNM",face = "bold"),
        legend.text = element_text(family="TNM"),panel.grid.minor = element_blank())+
  theme(axis.title.y = element_text(family="TNM",face = "bold"),axis.title.x = element_text(family="TNM",face = "bold"))
#theme(panel.background = element_blank(),axis.line = element_line(color = "black"))#去上右边框
nmds_plot
library(ggpubr)
show_point_shapes() 


p<-nmds_plot+
  stat_ellipse(aes(fill=group),geom="polygon",level=0.8,alpha=0.1,show.legend=F)
p
?geom_point
??geom_ord_ellipse
?stat_ellipse
site <- c("#99CCFF","#FF9999","#FFCC33","#99CC99",
          "#FFCC99","#996699")
?scale_shape_manual
?scale_fill_manual
install.packages("ggthemes")
install.packages("emojifont")
library(emojifont)
require(ggthemes)
require(emojifont)
 

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值