简体中文 繁體中文 English 日本語 Deutsch 한국 사람 بالعربية TÜRKÇE português คนไทย Français

站内搜索

搜索

活动公告

11-02 12:46
10-23 09:32
通知:本站资源由网友上传分享,如有违规等问题请到版务模块进行投诉,将及时处理!
10-23 09:31
10-23 09:28
通知:签到时间调整为每日4:00(东八区)
10-23 09:26

科研数据可视化新高度 使用R语言创建专业进化树并掌握多种输出格式的详细教程与实用技巧分享让你的研究数据脱颖而出提升学术影响力与认可度获得更多引用与合作机会推动研究发展与创新促进学术交流与进步提升价值与影响力增强竞争力与声誉推动发展与进步促进创新

3万

主题

318

科技点

3万

积分

大区版主

木柜子打湿

积分
31894

财Doro三倍冰淇淋无人之境【一阶】立华奏小樱(小丑装)⑨的冰沙以外的星空【二阶】

发表于 2025-10-3 18:50:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
引言

进化树(系统发育树)是生物学、生态学、进化生物学等领域中不可或缺的研究工具,它直观地展示了物种或基因之间的进化关系。随着高通量测序技术的发展,科研人员能够获得海量的遗传数据,构建更加精确和复杂的进化树。然而,如何将这些数据转化为清晰、美观且信息丰富的可视化图表,成为了科研人员面临的重要挑战。

R语言作为一款强大的统计分析和图形绘制工具,在进化树可视化方面展现出无与伦比的优势。它不仅提供了丰富的包和函数,还支持高度自定义的图形参数,使科研人员能够创建出专业级别的进化树可视化作品。本文将详细介绍如何使用R语言创建专业进化树,掌握多种输出格式,并通过实用技巧让你的研究数据脱颖而出,提升学术影响力与认可度。

准备工作

安装R和RStudio

在开始之前,确保你的计算机上已安装R和RStudio。R可以从CRAN(Comprehensive R Archive Network)官网(https://cran.r-project.org/)下载,而RStudio可以从https://www.rstudio.com/下载。RStudio提供了一个用户友好的集成开发环境,使R编程更加便捷。

安装必要的R包

在R中,有许多专门用于进化树分析的包。以下是本教程将用到的主要包:
  1. # 安装CRAN上的包
  2. install.packages("ape")
  3. install.packages("ggplot2")
  4. install.packages("ggtree")
  5. install.packages("phangorn")
  6. install.packages("phytools")
  7. install.packages("tidyverse")
  8. install.packages("cowplot")
  9. install.packages("gridExtra")
  10. # 安装Bioconductor上的包
  11. if (!requireNamespace("BiocManager", quietly = TRUE))
  12.     install.packages("BiocManager")
  13. BiocManager::install("ggtree")
  14. BiocManager::install("treeio")
复制代码

安装完成后,加载这些包:
  1. library(ape)        # 用于进化树分析和基本绘图
  2. library(ggtree)     # 基于ggplot2的进化树可视化
  3. library(treeio)     # 用于处理不同格式的进化树文件
  4. library(ggplot2)    # 基础绘图系统
  5. library(phangorn)   # 进化树重建和分析
  6. library(phytools)   # 进化树比较和分析方法
  7. library(tidyverse)  # 数据处理和可视化
  8. library(cowplot)    # 图形组合和布局
  9. library(gridExtra)  # 图形排列
复制代码

数据准备与导入

进化树数据格式

进化树数据通常以Newick、Nexus或Phylip格式存储。Newick格式是最常用的,它使用括号表示分支关系,逗号分隔分支,分号结束树。例如:
  1. ((A:0.1,B:0.2):0.3,C:0.4,D:0.5);
复制代码

导入进化树数据

R中的ape包提供了读取不同格式进化树文件的函数:
  1. # 读取Newick格式的树文件
  2. tree <- read.tree("path/to/your/treefile.newick")
  3. # 读取Nexus格式的树文件
  4. tree <- read.nexus("path/to/your/treefile.nexus")
  5. # 如果没有现成的树文件,可以使用ape生成随机树用于演示
  6. set.seed(123)
  7. tree <- rtree(10)  # 生成一个有10个分类单元的随机树
复制代码

导入关联数据

在实际研究中,我们通常需要将进化树与其他数据(如物种特征、地理分布等)结合分析。这些数据通常以表格形式存储,可以使用read.csv或read.table函数导入:
  1. # 导入CSV格式的数据表
  2. trait_data <- read.csv("path/to/your/trait_data.csv")
  3. # 查看数据结构
  4. head(trait_data)
复制代码

基础进化树绘制

使用ape包绘制基础进化树

ape包提供了基础的进化树绘制功能:
  1. # 绘制基本的进化树
  2. plot(tree, type = "phylogram")  # 系统发育图
  3. plot(tree, type = "cladogram")  # 分支图
  4. plot(tree, type = "fan")        # 扇形图
  5. plot(tree, type = "unrooted")   # 无根树
  6. plot(tree, type = "radial")     # 径向树
复制代码

使用ggtree包绘制进化树

ggtree包基于ggplot2系统,提供了更加灵活和美观的进化树绘制方法:
  1. # 基础进化树
  2. ggtree(tree) +
  3.   geom_tiplab() +  # 添加分类单元标签
  4.   geom_nodepoint() # 显示节点
  5. # 不同布局的进化树
  6. ggtree(tree, layout = "rectangular") +  # 矩形布局
  7.   geom_tiplab()
  8. ggtree(tree, layout = "slanted") +      # 倾斜布局
  9.   geom_tiplab()
  10. ggtree(tree, layout = "circular") +     # 圆形布局
  11.   geom_tiplab()
  12. ggtree(tree, layout = "fan") +          # 扇形布局
  13.   geom_tiplab()
  14. ggtree(tree, layout = "unrooted") +     # 无根树布局
  15.   geom_tiplab()
复制代码

进化树美化

调整分支颜色和样式
  1. # 为分支着色
  2. ggtree(tree) +
  3.   geom_tiplab() +
  4.   geom_nodepoint() +
  5.   theme_tree2() +
  6.   geom_tippoint(color = "steelblue", size = 3) +  # 分类单元点颜色
  7.   geom_nodepoint(color = "firebrick", size = 3)   # 节点颜色
  8. # 根据分组为分支着色
  9. # 假设我们有一个分组向量
  10. group <- rep(c("A", "B"), each = 5)
  11. names(group) <- tree$tip.label
  12. ggtree(tree) +
  13.   geom_tiplab() +
  14.   geom_tippoint(aes(color = group), size = 3) +
  15.   scale_color_manual(values = c("A" = "steelblue", "B" = "firebrick"))
复制代码

调整标签样式
  1. # 调整标签字体、大小和颜色
  2. ggtree(tree) +
  3.   geom_tiplab(color = "black", size = 4, fontface = "bold",
  4.               hjust = -0.1) +  # hjust控制标签水平位置
  5.   geom_nodepoint(color = "firebrick", size = 3) +
  6.   xlim(0, 6)  # 调整x轴范围,为标签留出空间
  7. # 添加标签背景
  8. ggtree(tree) +
  9.   geom_tiplab(color = "white", size = 4, fontface = "bold",
  10.               hjust = -0.1,
  11.               geom = "label",  # 使用label geom
  12.               label.size = 0,  # 去掉边框
  13.               fill = "steelblue") +
  14.   geom_nodepoint(color = "firebrick", size = 3) +
  15.   xlim(0, 6)
复制代码

添加支持值
  1. # 假设我们的树有支持值(通常在节点标签中)
  2. tree$node.label <- round(runif(n = tree$Nnode, min = 0.5, max = 1), 2)
  3. # 显示支持值
  4. ggtree(tree) +
  5.   geom_tiplab() +
  6.   geom_nodelab(aes(label = node.label), hjust = -0.2) +
  7.   geom_nodepoint() +
  8.   xlim(0, 6)
  9. # 根据支持值为节点着色
  10. ggtree(tree) +
  11.   geom_tiplab() +
  12.   geom_nodepoint(aes(color = as.numeric(node.label)), size = 3) +
  13.   scale_color_viridis_c(limits = c(0.5, 1), option = "plasma") +
  14.   xlim(0, 6)
复制代码

添加时间轴或进化距离轴
  1. # 添加时间轴/进化距离轴
  2. ggtree(tree) +
  3.   geom_tiplab() +
  4.   geom_nodepoint() +
  5.   theme_tree2() +
  6.   xlim(0, 6) +
  7.   ggtitle("Evolutionary Tree with Scale") +
  8.   xlab("Evolutionary Distance")
复制代码

高级可视化技巧

添加条形图或热图
  1. # 创建一些示例数据
  2. data <- data.frame(
  3.   species = tree$tip.label,
  4.   trait1 = rnorm(10),
  5.   trait2 = rnorm(10, mean = 2),
  6.   trait3 = rnorm(10, mean = 3)
  7. )
  8. # 将树与数据结合
  9. p_tree <- ggtree(tree) +
  10.   geom_tiplab(align = TRUE, linesize = 0.5) +
  11.   xlim(0, 6)
  12. # 添加条形图
  13. p_bar <- ggtree(tree) +
  14.   geom_tiplab(align = TRUE, linesize = 0.5) +
  15.   geom_facet(panel = "Trait", data = data,
  16.              geom = geom_col, mapping = aes(x = trait1, fill = species),
  17.              width = 0.6, color = 'white') +
  18.   theme_tree2() +
  19.   xlim(0, 6)
  20. # 添加热图
  21. # 需要将数据转换为长格式
  22. data_long <- data %>%
  23.   pivot_longer(cols = -species, names_to = "variable", values_to = "value")
  24. p_heat <- ggtree(tree) +
  25.   geom_tiplab(align = TRUE, linesize = 0.5) +
  26.   geom_facet(panel = "Trait", data = data_long,
  27.              geom = geom_tile, mapping = aes(x = variable, y = species, fill = value),
  28.              width = 0.8, height = 0.8) +
  29.   scale_fill_viridis_c() +
  30.   theme_tree2() +
  31.   xlim(0, 6)
  32. # 显示图形
  33. p_bar
  34. p_heat
复制代码

添加祖先性状重建
  1. # 创建一个连续性状的示例
  2. cont_trait <- setNames(rnorm(10), tree$tip.label)
  3. # 使用fastAnc进行祖先性状重建
  4. anc_trait <- phytools::fastAnc(tree, cont_trait)
  5. # 将树与性状数据结合
  6. p <- ggtree(tree) +
  7.   geom_tiplab() +
  8.   geom_nodepoint(aes(color = node), size = 3, data = nodepie(anc_trait)) +
  9.   scale_color_viridis_c() +
  10.   theme(legend.position = "right")
  11. # 显示图形
  12. p
复制代码

添加系统发育信号
  1. # 计算Blomberg's K
  2. K <- phylosig(tree, cont_trait, test = TRUE)
  3. print(K)
  4. # 在图上添加系统发育信号信息
  5. p <- ggtree(tree) +
  6.   geom_tiplab(aes(color = cont_trait)) +
  7.   scale_color_viridis_c() +
  8.   geom_text(aes(x = 0, y = 0, label = paste("Blomberg's K =", round(K$K, 2),
  9.                                            "\np =", round(K$P, 3))),
  10.             size = 4, hjust = 0, vjust = 0) +
  11.   theme(legend.position = "right")
  12. # 显示图形
  13. p
复制代码

添加子树高亮
  1. # 选择要高亮的子树
  2. subtree <- getSubtree(tree, node = 15)  # 假设我们想高亮节点15的子树
  3. # 高亮子树
  4. ggtree(tree) +
  5.   geom_tiplab() +
  6.   geom_hilight(node = 15, fill = "steelblue", alpha = 0.3) +
  7.   geom_point2(aes(subset = (node == 15)), size = 5, color = "red")
复制代码

添加比较树
  1. # 生成第二棵树用于比较
  2. set.seed(456)
  3. tree2 <- rtree(10)
  4. # 准备数据
  5. trees <- list(tree1 = tree, tree2 = tree2)
  6. # 绘制比较树
  7. ggtree(trees) +
  8.   geom_tiplab() +
  9.   geom_taxalink("t1", "t9", color = "steelblue") +
  10.   geom_taxalink("t3", "t7", color = "firebrick")
复制代码

多种输出格式

导出为PDF
  1. # 保存为PDF
  2. pdf("evolutionary_tree.pdf", width = 10, height = 8)
  3. print(p_tree)
  4. dev.off()
  5. # 使用ggsave(适用于ggplot对象)
  6. ggsave("evolutionary_tree_ggplot.pdf", p_tree, width = 10, height = 8, dpi = 300)
复制代码

导出为PNG/JPEG
  1. # 保存为PNG
  2. png("evolutionary_tree.png", width = 3000, height = 2400, res = 300)
  3. print(p_tree)
  4. dev.off()
  5. # 使用ggsave
  6. ggsave("evolutionary_tree_ggplot.png", p_tree, width = 10, height = 8, dpi = 300)
  7. # 保存为JPEG
  8. jpeg("evolutionary_tree.jpg", width = 3000, height = 2400, res = 300)
  9. print(p_tree)
  10. dev.off()
  11. # 使用ggsave
  12. ggsave("evolutionary_tree_ggplot.jpg", p_tree, width = 10, height = 8, dpi = 300, quality = 90)
复制代码

导出为TIFF
  1. # 保存为TIFF(适合出版)
  2. tiff("evolutionary_tree.tiff", width = 3000, height = 2400, res = 300)
  3. print(p_tree)
  4. dev.off()
  5. # 使用ggsave
  6. ggsave("evolutionary_tree_ggplot.tiff", p_tree, width = 10, height = 8, dpi = 300)
复制代码

导出为SVG
  1. # 保存为SVG(矢量图,可无限缩放)
  2. svg("evolutionary_tree.svg", width = 10, height = 8)
  3. print(p_tree)
  4. dev.off()
  5. # 使用ggsave
  6. ggsave("evolutionary_tree_ggplot.svg", p_tree, width = 10, height = 8)
复制代码

导出为PPT
  1. # 安装和加载export包
  2. install.packages("export")
  3. library(export)
  4. # 导出为PPT
  5. graph2ppt(file = "evolutionary_tree.pptx", width = 10, height = 8)
复制代码

导出为Word
  1. # 导出为Word
  2. graph2doc(file = "evolutionary_tree.docx", width = 10, height = 8)
复制代码

实用技巧与案例分享

技巧1:批量处理多棵树
  1. # 生成多棵树
  2. trees <- lapply(1:5, function(x) rtree(10))
  3. # 为每棵树添加标签
  4. labeled_trees <- lapply(trees, function(tree) {
  5.   ggtree(tree) +
  6.     geom_tiplab() +
  7.     ggtitle(paste("Tree", which(trees == tree)[1]))
  8. })
  9. # 组合多棵树
  10. combined_plot <- plot_grid(plotlist = labeled_trees, ncol = 2)
  11. # 保存组合图
  12. ggsave("combined_trees.pdf", combined_plot, width = 12, height = 15)
复制代码

技巧2:添加图片到进化树
  1. # 假设我们有一些物种图片,存储在本地
  2. # 这里我们使用grid包的rasterGrob函数来添加图片
  3. library(grid)
  4. library(png)
  5. # 创建示例图片(实际应用中替换为你的图片路径)
  6. img_paths <- paste0("species_", 1:10, ".png")
  7. # 读取图片并创建grob对象
  8. img_list <- lapply(img_paths, function(path) {
  9.   # 如果图片不存在,创建一个示例图片
  10.   if (!file.exists(path)) {
  11.     png(filename = path, width = 100, height = 100)
  12.     plot(1, type = "n", axes = FALSE, ann = FALSE)
  13.     text(1, 1, labels = gsub(".png", "", path), cex = 2)
  14.     dev.off()
  15.   }
  16.   rasterGrob(readPNG(path))
  17. })
  18. # 创建带有图片的进化树
  19. p <- ggtree(tree) +
  20.   geom_tiplab(align = TRUE, linesize = 0.5) +
  21.   geom_image(aes(image = image), data = data.frame(
  22.     label = tree$tip.label,
  23.     image = I(img_list)
  24.   ), size = 0.1, alpha = 0.8) +
  25.   xlim(0, 6)
  26. # 显示图形
  27. p
复制代码

技巧3:创建交互式进化树
  1. # 安装和加载必要的包
  2. install.packages("plotly")
  3. library(plotly)
  4. # 创建基本进化树
  5. p <- ggtree(tree) +
  6.   geom_tiplab() +
  7.   geom_nodepoint()
  8. # 转换为交互式图形
  9. interactive_p <- ggplotly(p)
  10. # 保存交互式图形
  11. htmlwidgets::saveWidget(interactive_p, "interactive_tree.html")
复制代码

技巧4:添加地理分布信息
  1. # 创建示例地理数据
  2. geo_data <- data.frame(
  3.   species = tree$tip.label,
  4.   longitude = runif(10, -180, 180),
  5.   latitude = runif(10, -90, 90)
  6. )
  7. # 创建进化树和地图的组合图
  8. library(maps)
  9. library(mapdata)
  10. # 创建地图
  11. world_map <- map_data("world")
  12. map_plot <- ggplot() +
  13.   geom_map(data = world_map, map = world_map,
  14.            aes(x = long, y = lat, map_id = region),
  15.            fill = "white", color = "black", size = 0.2) +
  16.   geom_point(data = geo_data, aes(x = longitude, y = latitude, color = species),
  17.              size = 3) +
  18.   coord_fixed(1.3) +
  19.   theme_minimal()
  20. # 组合进化树和地图
  21. combined_plot <- plot_grid(p_tree, map_plot, ncol = 2, labels = c("A", "B"))
  22. # 保存组合图
  23. ggsave("tree_with_map.pdf", combined_plot, width = 15, height = 8)
复制代码

技巧5:创建时间校准的进化树
  1. # 创建一些示例时间校准点
  2. calibration_points <- data.frame(
  3.   node = c(15, 18),  # 节点编号
  4.   age_min = c(10, 20),  # 最小年龄(百万年)
  5.   age_max = c(15, 25)   # 最大年龄(百万年)
  6. )
  7. # 创建时间校准的进化树
  8. p <- ggtree(tree) +
  9.   geom_tiplab() +
  10.   geom_nodepoint() +
  11.   geom_range(aes(xmin = age_min, xmax = age_max, y = y),
  12.              data = calibration_points, color = "blue", size = 1.5) +
  13.   scale_x_continuous(breaks = seq(0, 50, by = 10),
  14.                      labels = paste(seq(50, 0, by = -10), "Ma")) +
  15.   theme_tree2() +
  16.   ggtitle("Time-Calibrated Phylogenetic Tree") +
  17.   xlab("Time (Million Years Ago)")
  18. # 显示图形
  19. p
复制代码

总结与展望

本文详细介绍了如何使用R语言创建专业进化树并掌握多种输出格式。从基础进化树绘制到高级可视化技巧,我们探讨了如何通过R语言的强大功能,将科研数据转化为清晰、美观且信息丰富的进化树可视化作品。

通过本文的学习,你不仅掌握了使用ape和ggtree等包绘制基础进化树的方法,还学会了如何美化进化树、添加支持值、结合其他数据类型、创建交互式图形等高级技巧。同时,我们还介绍了如何将进化树导出为PDF、PNG、JPEG、TIFF、SVG等多种格式,以满足不同出版和展示需求。

这些技能将帮助你的研究数据脱颖而出,提升学术影响力与认可度,获得更多引用与合作机会,推动研究发展与创新,促进学术交流与进步,提升价值与影响力,增强竞争力与声誉,推动发展与进步,促进创新。

展望未来,随着R语言生态系统的不断发展,我们可以期待更多强大而便捷的进化树可视化工具的出现。同时,结合机器学习、虚拟现实等新兴技术,进化树可视化将变得更加智能、交互和沉浸式,为科研人员提供更深入的数据洞察和更直观的结果展示。

无论你是进化生物学的新手还是经验丰富的研究者,希望本文提供的教程和技巧能够帮助你在科研数据可视化的道路上取得新的突破,让你的研究成果更加引人注目,为科学进步贡献力量。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

频道订阅

频道订阅

加入社群

加入社群

联系我们|TG频道|RSS

Powered by Pixtech

© 2025 Pixtech Team.