2018-6-13 23:43

[TOC]

## 写在前面

《水稻微生物组时间序列分析》的文章，大家对其中图绘制过程比较感兴趣。一上午收到了超30条留言，累计收到41个小伙伴的留言求精讲。

## 图2. 微生物组随时间变化的规律

A-D. 对两个水稻品种分别在两地进行的连续微生物组调查结果相关分析，发现8-10周后群落结构趋于稳定。

E. 所有时间点距离水稻最后取样点的Bray-Curtis距离。发现土壤呈现小幅波动变化，而根系呈现出先快后慢，逐渐趋近的变化规律。

F. 不同水稻品种在两个地点间的距离变化，发现土壤差异稳定，而根系微生物组差异随时间增长而趋于一致。

E-F基于vegan包计算的所有样品两两间Bray-Curtis距离。分别挑选距离终点的距离，和两地间的距离与时间序列上的关系，并采用ggplot2可视化散点图，并添加拟合曲线方便观察变化规律。

## 图2A-D. 相关性corrplot

A-D为四个相当类型图，只是分别两个地点的两个品种进行分析，进一步説在不同地点和不同品种的变量下仍然存在稳定的变化规律。这里仅以图2A在北京种植的日本晴水稻品种为例进行代码说明。

# Set working enviroment in Rstudio, select Session - Set working directory - To source file location, default is runing directory
rm(list=ls()) # clean enviroment object

library("corrplot")
library("pheatmap")
library(ggcorrplot)

# Public file 1. "design.txt"  Design of experiment

# Public file 2. "otu_table.txt"  raw reads count of each OTU in each sample
otu_table = read.delim("../data/otu_table.txt", row.names= 1,  header=T, sep="\t")

# setting subset design
if (TRUE){
sub_design = subset(design,groupID %in% c("A50Cp0","A50Cp1","A50Cp2","A50Cp3","A50Cp7","A50Cp10","A50Cp14","A50Cp21","A50Cp28","A50Cp35","A50Cp42","A50Cp49","A50Cp63","A50Cp70","A50Cp77","A50Cp84","A50Cp91","A50Cp98","A50Cp112","A50Cp119") ) # select group1
}else{
sub_design = design
}
sub_design$group=sub_design$groupID

# Set group order
if ("TRUE" == "TRUE") {
sub_design$group = factor(sub_design$group, levels=c("A50Cp0","A50Cp1","A50Cp2","A50Cp3","A50Cp7","A50Cp10","A50Cp14","A50Cp21","A50Cp28","A50Cp35","A50Cp42","A50Cp49","A50Cp63","A50Cp70","A50Cp77","A50Cp84","A50Cp91","A50Cp98","A50Cp112","A50Cp119"))
}else{sub_design$group = as.factor(sub_design$group)}

print(paste("Number of group: ",length(unique(sub_design$group)),sep="")) # show group numbers 筛选后的实验设计样本与OTU表交叉筛选 # sub and reorder subdesign and otu_table idx = rownames(sub_design) %in% colnames(otu_table) sub_design = sub_design[idx,] count = otu_table[, rownames(sub_design)] OTU表标准化为百分比，在R中只需一小行代码 norm = t(t(count)/colSums(count,na=T)) * 100 # normalization to total 100 按组合并：因为样本太多，一小部分过百个样本，展示太乱，按组求均值，组间比较更容易发现规律 # Pearson correlation among groups sampFile = as.data.frame(sub_design$group,row.names = row.names(sub_design))
colnames(sampFile)[1] = "group"
mat = norm
mat_t = t(mat)

mat_t2 = merge(sampFile, mat_t, by="row.names")
mat_t2 = mat_t2[,-1]

mat_mean = aggregate(mat_t2[,-1], by=mat_t2[1], FUN=mean) # mean
mat_mean_final = do.call(rbind, mat_mean)[-1,]
geno = mat_mean\$group
colnames(mat_mean_final) = geno

sim=cor(mat_mean_final,method="pearson")
sim=round(sim,3)

pdf(file="ggcorrplot_pearson_A50Cp.pdf", height = 2.5, width = 2.5)
ggcorrplot(sim, type = "upper", colors = c("green", "yellow", "red")) # , method = "circle"
dev.off()

# 人为设置颜色度
col1 <- colorRampPalette(c("green", "green", "red"))

pdf(file="corplot_pie_pearson_A50Cp.pdf", height = 2.5, width = 2.5)
corrplot(sim, method="pie", type="lower", col=col1(100)) # , diag=F , na.label = "1"
dev.off()

col1 <- colorRampPalette(c("green", "red"))
corrplot(sim, method="pie", type="lower", col=col1(100)) # , diag=F , na.label = "1"

# 生成时间热图，分别为土和植物的
time1 = c(0,1,2,3,7,10,14,21,28,35,42,49,63,70,77,84,91,98,112,119)
time2 = c(0,41,48,54,62,77,84,90,97,119,0,0,0,0,0,0,0,0,0,0)
time=data.frame(time1,time2)
pheatmap(time, cluster_rows = F,  cluster_cols = F)
pheatmap(time, cluster_rows = F,  cluster_cols = F, filename = "corplot_pie_legend_time.pdf" ,width=2, height=4)

Citition:
Zhang, J., Zhang, N., Liu, Y.X., Zhang, X., Hu, B., Qin, Y., Xu, H., Wang, H., Guo, X., Qian, J., et al. (2018). Root microbiota shift in rice correlates
with resident time in the field and developmental stage. Sci China Life Sci 61, https://doi.org/10.1007/s11427-018-9284-4

## 写在后面

https://mp.weixin.qq.com/s/5jQspEvH5_4Xmart22gjMA