install.packages("ggplot2")
install.packages("ggpubr")
install.packages("vegan")
计算Shannon-香农指数和Simpson-辛普森指数的命令在vegan包中,计算各组显著性的命令在ggpubr包中;画图使用ggplot命令,在行使每个命令之前一定要加载相应的包,如下:
library(ggplot2)
library(ggpubr)
library(vegan)
拿到一个otu表格,要先计算香农指数和辛普森指数,操作如下:
otu=read.table('D:/r-working/feature-table.taxonomy.txt',row.names = 1,skip=1,header=T,comment.char ='',sep='\t')
#读取out表格
#'D:/feature table.taxonomy.txt'为文件路径,注意斜线方向
#row.names = 1指定第一列为行名
#skip=1跳过第一行不读
#header=T指定第一个有效行为列名
#sep='\t'表示指定制表符为分隔符
#comment.char=''表示设置注释符号为空字符‘’,这样#后面的内容就不会被省略
otu=otu[,-ncol(otu)]
#去除表格的最后一列,无用信息
otu=t(otu)
#表格转置,必须将样品名作为行名
shannon=diversity(otu,"shannon")
#计算香农指数,先加载vegan包
shannon
#查看香农指数
simpson=diversity(otu,"simpson")
#计算辛普森指数,先加载vegan包
simpson
#查看辛普森指数
alpha=data.frame(shannon,simpson,check.names=T)
#合并两个指数
write.table(alpha,"D:/r-working/alpha-summary.xls",sep='\t',quote=F)
#存储数据,注意路径使用反斜杠
将各样本进行分组,并进行画图,操作如下:
map<-read.table('D:/r-working/mapping_file.txt',row.names = 1,header=T,comment.char ='',sep='\t',check.names=F)
#读取分组表格
group<-map["Group1"]
#提取需要的分组,'Group1'是表中的分组列名,包括A,B,C三组
alpha<-alpha[match(rownames(group),rownames(alpha)),]
#重排alpha的行的顺序,使其与group的样本id(行名)一致
data<-data.frame(group,alpha,check.rows=T)
#合并两个表格.'<-'与'='同属赋值的含义.
p=ggplot(data=data,aes(x=Group1,y=shannon))+geom_boxplot(fill=rainbow(7)[2])
#data = data指定数据表格
#x=Group1指定作为x轴的数据列名
#y=shannon指定作为y轴的数据列名
#geom_boxplot()表示画箱线图
#fill=rainbow(7)[2]指定填充色
此处用到ggplot2包画箱线图,将画图函数赋值给p后,可以用‘+’不断进行图层叠加,给图片p增加新的特性
p
#查看p
mycompare=list(c('A','B'),c('A','C'),c('B','C'))
#指定多重比较的分组对
mycompare
p<-p+stat_compare_means(comparisons=mycompare,label = "p.signif",method = 'wilcox')
#添加显著性标记的第一种方法,在此之前先加载ggpubr包
p<-p+ylim(2,5.5)
#调整图像的外观
题主你好,
这是R默认的自动补全功能。
可以通过☑️ Match brackets/quotes 进行设置。
以下是RStudio的设置方式,R软件类似,希望可以帮助到你~
https://github.com/fbreitwieser/sankeyD3
实例分析:
### 安装与加载包
install.packages("devtools")
devtools::install_github("fbreitwieser/sankeyD3")
library(sankeyD3)
第一个为链接数据框 links(起点、靶点、权重、链接的特征1、链接的特征1.....);
然后根据links构建第二个为节点数据框nodes(起点与靶点、点的特征1、点的特征........)
nodes <- data.frame(name=c(as.character(links$source), as.character(links$target)) %>% unique())
然后基于nodes数据框构建links中节点的唯一标识符ID,而非根据节点的name
links$IDsource <- match(links$source, nodes$name)-1
links$IDtarget <- match(links$target, nodes$name)-1
sankeyNetwork( Links = links, Nodes = nodes, Source = "IDsource", Target = "IDtarget",
Value = "weight", NodeID = "name",nodeWidth =10,units = 'TWh',
height=300,width=300,colourScale=JS("d3.scaleOrdinal(d3.schemeCategory10)"),
numberFormat=".0f",fontSize = 8)
nodes$color<-sample(c("red","orange","blue","green"),nrow(nodes),replace=T) #在这里进行随机自定义颜色,当然也可以按照自己的需求进行设置
sankeyNetwork(Links = links, Nodes = nodes,Source = "IDsource", Target = "IDtarget",
Value = "weight", NodeID = "name",nodeWidth =10,units = 'TWh',
height=300,width=300,numberFormat=".0f",fontSize = 8, NodeColor = "color" )
也可以根据节点自定义的分类对节点进行颜色的绘制
nodes$group<-rep("水果",nrow(nodes))
nodes$group[nodes$name %in% c("上海","深圳","北京","南京")]<-"城市"
nodes$group[nodes$name %in% c("律师","老师","白领","公务员","记者","化妆师")]<-"职业"
sankeyNetwork(Links = links, Nodes = nodes,Source = "IDsource", Target = "IDtarget",
Value = "weight", NodeID = "name",nodeWidth =10,units = 'TWh',
numberFormat=".0f",fontSize = 8,height=300,width=300,
NodeGroup="group",colourScale=JS("d3.scaleOrdinal(d3.schemeCategory10)") )
对于缎带的颜色设置同理也可以对其进行分组颜色设置(这里按照其统计量进行分组设置,当然也可以按照其他进行分组)
links$group<-rep("A",nrow(links))
links$group[links$weight<500 &links$weight>=100]<-"B"
links$group[links$weight<100]<-"C"
sankeyNetwork(Links = links, Nodes = nodes,Source = "IDsource", Target = "IDtarget",
Value = "weight", NodeID = "name",nodeWidth =10,units = 'TWh',
numberFormat=".0f",fontSize = 8,height=300,width=300,
NodeGroup="group", LinkGroup = "group",
colourScale=JS("d3.scaleOrdinal(d3.schemeCategory10)"))
有时候想要缎带根据其宽度进行一定透明度的变化,可以使用 linkType="path1"参数进行设置
install.packages("webshot")
library(webshot)
if(!is_phantomjs_installed()){
install_phantomjs()
}
library(webshot)
p<-sankeyNetwork(Links = links, Nodes = nodes,Source = "IDsource", Target = "IDtarget",
Value = "weight", NodeID = "name",nodeWidth =10,units = 'TWh',
numberFormat=".0f",fontSize = 8,height=300,width=300,
NodeGroup="group",LinkGroup = "group",
colourScale=JS("d3.scaleOrdinal(d3.schemeCategory10)"))
### 将结果存储PDF
saveNetwork(p,"sankey.html")
webshot("sankey.html" , "sankey.pdf")