基于R做宏基因组进化树+丰度柱状图TreeBar带聚类树的堆叠柱形图

news/2024/5/20 8:02:53 标签: r语言, 聚类, 开发语言

写在前面

同之前一样,重分析需要所以自己找了各路代码借鉴学习,详情请参考 R语言绘制带聚类树的堆叠柱形图 , 实操效果如下:

image-20230917151814185

步骤

表格预处理

  • 选取不同样本属水平的物种丰度图(绝对和相对水平都可以,相对画出来是齐的,绝对的bar是不齐的)。Top10丰度外的类群合并为Others。这里注意,用本篇代码时,表格选择csv/txt等格式文件一定要是制表符分隔的,不然会报错重复名或者没有多出的行

image-20230917152142907

  • 分组信息

image-20230917152559876

代码演示

# 装包
install.packages("vegan")

#层次聚类
#读取 OTU 丰度表,csv/txt等文件一定要是制表符分隔的,不然会报错重复名或者没有多出的行
dat <- read.delim('Unigenes.absolute.g.txt', row.names = 1, sep = '\t', head = TRUE, check.names = FALSE)

#计算样本间距离,以群落分析中常用的 Bray-curtis 距离为例
dis_bray <- vegan::vegdist(t(dat), method = 'bray')

#层次聚类,以 UPGMA 为例
tree <- hclust(dis_bray, method = 'average')
tree

plot(tree)

##聚类树绘制
#样本分组颜色、名称等
group <- read.delim('group.txt', row.names = 1, sep = '\t', head = TRUE, check.names = FALSE, stringsAsFactors = FALSE)
grp <- group[2]
group_col <- c('red', 'blue')
names(group_col) <- c('1', '2')
group_name <- c('RA', 'CON')

#样本分组标签
layout(t(c(1, 2, 2, 2, 3)))
par(mar = c(5, 2, 5, 0))

plot(0, type = 'n', xaxt = 'n', yaxt = 'n', frame.plot = FALSE, xlab = '', ylab = '',
     xlim = c(-max(tree$height), 0), ylim = c(0, length(tree$order)))
legend('topleft', legend = group_name, pch = 15, col = group_col, bty = 'n', cex = 1)

#聚类树绘制,按分组给分支上色
treeline <- function(pos1, pos2, height, col1, col2) {
  meanpos = (pos1[1] + pos2[1]) / 2
  segments(y0 = pos1[1] - 0.4, x0 = -pos1[2], y1 = pos1[1] - 0.4, x1 = -height,  col = col1,lwd = 2)
  segments(y0 = pos1[1] - 0.4, x0 = -height,  y1 = meanpos - 0.4, x1 = -height,  col = col1,lwd = 2)
  segments(y0 = meanpos - 0.4, x0 = -height,  y1 = pos2[1] - 0.4, x1 = -height,  col = col2,lwd = 2)
  segments(y0 = pos2[1] - 0.4, x0 = -height,  y1 = pos2[1] - 0.4, x1 = -pos2[2], col = col2,lwd = 2)
}

meanpos = matrix(rep(0, 2 * length(tree$order)), ncol = 2)
meancol = rep(0, length(tree$order))
for (step in 1:nrow(tree$merge)) {
  if(tree$merge[step, 1] < 0){
    pos1 <- c(which(tree$order == -tree$merge[step, 1]), 0)
    col1 <- group_col[as.character(grp[tree$labels[-tree$merge[step, 1]],1])]
  } else {
    pos1 <- meanpos[tree$merge[step, 1], ]
    col1 <- meancol[tree$merge[step, 1]]
  }
  if (tree$merge[step, 2] < 0) {
    pos2 <- c(which(tree$order == -tree$merge[step, 2]), 0)
    col2 <- group_col[as.character(grp[tree$labels[-tree$merge[step, 2]],1])]
  } else {
    pos2 <- meanpos[tree$merge[step, 2], ]
    col2 <- meancol[tree$merge[step, 2]]
  }
  height <- tree$height[step]
  treeline(pos1, pos2, height, col1, col2)
  meanpos[step, ] <- c((pos1[1] + pos2[1]) / 2, height)
  if (col1 == col2) meancol[step] <- col1 else meancol[step] <- 'grey'
}

##堆叠柱形图
#样本顺序调整为和聚类树中的顺序一致
dat <- dat[ ,tree$order]

#物种颜色设置
genus_color <- c('#8DD3C7', '#FFFFB3', '#00CDCD', '#FB8072', '#80B1D3', '#FDB462', '#B3DE69', '#FCCDE5', '#BC80BD', '#CCEBC5', 'gray')
names(genus_color) <- rownames(dat)

#堆叠柱形图
#数字向量,格式为c(bottom, left, top, right),给出各个方向绘图区边缘空白的大小
par(mar = c(5, 3, 5, 0))

relative <- read.delim('new_Unigenes.relative.g.txt', row.names = 1, sep = '\t', head = TRUE, check.names = FALSE)

bar <- barplot(as.matrix(relative), col = genus_color, space = 0.4, width = 0.7, cex.axis = 1, horiz = TRUE, cex.lab = 1.2,
               xlab = 'Relative Abundance', yaxt = 'n', las = 1, ylim = c(0, ncol(dat)), family = 'mono')

mtext('Top 10 genus', side = 3, line = 1, cex = 1)
text(x = -0.05, y = bar, labels = colnames(dat), col = group_col[group[tree$order, 2]], xpd = TRUE)

#柱形图图例
par(mar = c(5, 1, 5, 0))
plot(0, type = 'n', xaxt = 'n', yaxt = 'n', bty = 'n', xlab = '', ylab = '')
legend('left', pch = 15, col = genus_color, legend = names(genus_color), bty = 'n', cex = 1)

http://www.niftyadmin.cn/n/5045876.html

相关文章

【100天精通Python】Day69:Python可视化_实战:导航定位中预测轨迹和实际轨迹的3D动画,示例+代码

目录 1. 预测的3D轨迹和实际轨迹的动画图&#xff0c;同时动态更新 2 真值轨迹设置为静态的&#xff0c;预测轨迹不断更新 3 网格的三维坐标系有旋转运动&#xff0c;以此全方位展示预测轨迹和真值轨迹之间的空间关系 1. 预测的3D轨迹和实际轨迹的动画图&#xff0c;同时动态更…

JavaScript混淆工具大比拼:JScrambler和JShaman哪个更胜一筹?

两款顶级JavaScript混淆工具测评&#xff1a;JScrambler和JShaman 出于JavaScript代码安全需求&#xff0c;JavaScript混淆已经被广泛使用。在这个领域中&#xff0c;有免费的小工具&#xff0c;也有专业、商业级的产品。 商业产品在功能强度、保护效果、稳定性等各方面都是全…

多策略改进蜣螂优化--螺旋搜索+最优值引导+反向学习策略

声明&#xff1a;对于作者的原创代码&#xff0c;禁止转售倒卖&#xff0c;违者必究&#xff01; 关于蜣螂算法的原理网上有很多&#xff0c;本文就不再详细介绍&#xff0c;本期算法是作者在参考了网上一些文献后自行改进的&#xff0c;接下来直接上改进策略&#xff1a; ①螺…

华为云云耀云服务器L实例评测:您值得信赖的云端伙伴

&#x1f337;&#x1f341; 博主猫头虎 带您 Go to New World.✨&#x1f341; &#x1f984; 博客首页——猫头虎的博客&#x1f390; &#x1f433;《面试题大全专栏》 文章图文并茂&#x1f995;生动形象&#x1f996;简单易学&#xff01;欢迎大家来踩踩~&#x1f33a; &a…

洛谷100题DAY6

26.P1628 合并序列 法一&#xff1a; #include<bits/stdc.h> using namespace std; const int N 1e5 10; int n, cnt; string c, s[N], a[N]; int main() {cin >> n;for(int i 1; i < n; i ){cin >> s[i];}cin >> c;int len c.size();for(int…

Ae 效果:CC Simple Wire Removal

Keying/CC Simple Wire Removal Keying/CC Simple Wire Removal CC Simple Wire Removal &#xff08;CC 简单线条移除&#xff09;通过在两点之间创建一条指定宽度&#xff08;厚度&#xff09;的连线&#xff0c;然后将连线区域内的像素按指定方式进行填充&#xff0c;从而实…

深入学习JUC,深入了解Java线程中的锁,及锁的实现原理,底层的知识又增加了!!!

文章目录 如何停止一个线程i的线程安全问题共享变量线程安全的解决问题synchronized基础概念java对象头Monitor优化轻量级锁锁膨胀自旋优化偏向锁偏量级锁的撤销偏量级锁的批量重定向偏量级锁的批量撤销锁消除 如何停止一个线程 stop方法, 非常不安全, 不应该使用 此方法会立即…

使用stream的skip方法进行分页处理

前言 在日常开发过程中&#xff0c;将查询的数据进行分页处理是非常常见的需求&#xff0c;而有时候 PageHelper的 startPage 方法对查询数据进行分页后&#xff0c;我们需要对这数据集进行再处理&#xff0c;而导致分页数据丢失一部分&#xff0c;只能查询第一页的数据。所以…