|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
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中,有许多专门用于进化树分析的包。以下是本教程将用到的主要包:
- # 安装CRAN上的包
- install.packages("ape")
- install.packages("ggplot2")
- install.packages("ggtree")
- install.packages("phangorn")
- install.packages("phytools")
- install.packages("tidyverse")
- install.packages("cowplot")
- install.packages("gridExtra")
- # 安装Bioconductor上的包
- if (!requireNamespace("BiocManager", quietly = TRUE))
- install.packages("BiocManager")
- BiocManager::install("ggtree")
- BiocManager::install("treeio")
复制代码
安装完成后,加载这些包:
- library(ape) # 用于进化树分析和基本绘图
- library(ggtree) # 基于ggplot2的进化树可视化
- library(treeio) # 用于处理不同格式的进化树文件
- library(ggplot2) # 基础绘图系统
- library(phangorn) # 进化树重建和分析
- library(phytools) # 进化树比较和分析方法
- library(tidyverse) # 数据处理和可视化
- library(cowplot) # 图形组合和布局
- library(gridExtra) # 图形排列
复制代码
数据准备与导入
进化树数据格式
进化树数据通常以Newick、Nexus或Phylip格式存储。Newick格式是最常用的,它使用括号表示分支关系,逗号分隔分支,分号结束树。例如:
- ((A:0.1,B:0.2):0.3,C:0.4,D:0.5);
复制代码
导入进化树数据
R中的ape包提供了读取不同格式进化树文件的函数:
- # 读取Newick格式的树文件
- tree <- read.tree("path/to/your/treefile.newick")
- # 读取Nexus格式的树文件
- tree <- read.nexus("path/to/your/treefile.nexus")
- # 如果没有现成的树文件,可以使用ape生成随机树用于演示
- set.seed(123)
- tree <- rtree(10) # 生成一个有10个分类单元的随机树
复制代码
导入关联数据
在实际研究中,我们通常需要将进化树与其他数据(如物种特征、地理分布等)结合分析。这些数据通常以表格形式存储,可以使用read.csv或read.table函数导入:
- # 导入CSV格式的数据表
- trait_data <- read.csv("path/to/your/trait_data.csv")
- # 查看数据结构
- head(trait_data)
复制代码
基础进化树绘制
使用ape包绘制基础进化树
ape包提供了基础的进化树绘制功能:
- # 绘制基本的进化树
- plot(tree, type = "phylogram") # 系统发育图
- plot(tree, type = "cladogram") # 分支图
- plot(tree, type = "fan") # 扇形图
- plot(tree, type = "unrooted") # 无根树
- plot(tree, type = "radial") # 径向树
复制代码
使用ggtree包绘制进化树
ggtree包基于ggplot2系统,提供了更加灵活和美观的进化树绘制方法:
- # 基础进化树
- ggtree(tree) +
- geom_tiplab() + # 添加分类单元标签
- geom_nodepoint() # 显示节点
- # 不同布局的进化树
- ggtree(tree, layout = "rectangular") + # 矩形布局
- geom_tiplab()
- ggtree(tree, layout = "slanted") + # 倾斜布局
- geom_tiplab()
- ggtree(tree, layout = "circular") + # 圆形布局
- geom_tiplab()
- ggtree(tree, layout = "fan") + # 扇形布局
- geom_tiplab()
- ggtree(tree, layout = "unrooted") + # 无根树布局
- geom_tiplab()
复制代码
进化树美化
调整分支颜色和样式
- # 为分支着色
- ggtree(tree) +
- geom_tiplab() +
- geom_nodepoint() +
- theme_tree2() +
- geom_tippoint(color = "steelblue", size = 3) + # 分类单元点颜色
- geom_nodepoint(color = "firebrick", size = 3) # 节点颜色
- # 根据分组为分支着色
- # 假设我们有一个分组向量
- group <- rep(c("A", "B"), each = 5)
- names(group) <- tree$tip.label
- ggtree(tree) +
- geom_tiplab() +
- geom_tippoint(aes(color = group), size = 3) +
- scale_color_manual(values = c("A" = "steelblue", "B" = "firebrick"))
复制代码
调整标签样式
- # 调整标签字体、大小和颜色
- ggtree(tree) +
- geom_tiplab(color = "black", size = 4, fontface = "bold",
- hjust = -0.1) + # hjust控制标签水平位置
- geom_nodepoint(color = "firebrick", size = 3) +
- xlim(0, 6) # 调整x轴范围,为标签留出空间
- # 添加标签背景
- ggtree(tree) +
- geom_tiplab(color = "white", size = 4, fontface = "bold",
- hjust = -0.1,
- geom = "label", # 使用label geom
- label.size = 0, # 去掉边框
- fill = "steelblue") +
- geom_nodepoint(color = "firebrick", size = 3) +
- xlim(0, 6)
复制代码
添加支持值
- # 假设我们的树有支持值(通常在节点标签中)
- tree$node.label <- round(runif(n = tree$Nnode, min = 0.5, max = 1), 2)
- # 显示支持值
- ggtree(tree) +
- geom_tiplab() +
- geom_nodelab(aes(label = node.label), hjust = -0.2) +
- geom_nodepoint() +
- xlim(0, 6)
- # 根据支持值为节点着色
- ggtree(tree) +
- geom_tiplab() +
- geom_nodepoint(aes(color = as.numeric(node.label)), size = 3) +
- scale_color_viridis_c(limits = c(0.5, 1), option = "plasma") +
- xlim(0, 6)
复制代码
添加时间轴或进化距离轴
- # 添加时间轴/进化距离轴
- ggtree(tree) +
- geom_tiplab() +
- geom_nodepoint() +
- theme_tree2() +
- xlim(0, 6) +
- ggtitle("Evolutionary Tree with Scale") +
- xlab("Evolutionary Distance")
复制代码
高级可视化技巧
添加条形图或热图
- # 创建一些示例数据
- data <- data.frame(
- species = tree$tip.label,
- trait1 = rnorm(10),
- trait2 = rnorm(10, mean = 2),
- trait3 = rnorm(10, mean = 3)
- )
- # 将树与数据结合
- p_tree <- ggtree(tree) +
- geom_tiplab(align = TRUE, linesize = 0.5) +
- xlim(0, 6)
- # 添加条形图
- p_bar <- ggtree(tree) +
- geom_tiplab(align = TRUE, linesize = 0.5) +
- geom_facet(panel = "Trait", data = data,
- geom = geom_col, mapping = aes(x = trait1, fill = species),
- width = 0.6, color = 'white') +
- theme_tree2() +
- xlim(0, 6)
- # 添加热图
- # 需要将数据转换为长格式
- data_long <- data %>%
- pivot_longer(cols = -species, names_to = "variable", values_to = "value")
- p_heat <- ggtree(tree) +
- geom_tiplab(align = TRUE, linesize = 0.5) +
- geom_facet(panel = "Trait", data = data_long,
- geom = geom_tile, mapping = aes(x = variable, y = species, fill = value),
- width = 0.8, height = 0.8) +
- scale_fill_viridis_c() +
- theme_tree2() +
- xlim(0, 6)
- # 显示图形
- p_bar
- p_heat
复制代码
添加祖先性状重建
- # 创建一个连续性状的示例
- cont_trait <- setNames(rnorm(10), tree$tip.label)
- # 使用fastAnc进行祖先性状重建
- anc_trait <- phytools::fastAnc(tree, cont_trait)
- # 将树与性状数据结合
- p <- ggtree(tree) +
- geom_tiplab() +
- geom_nodepoint(aes(color = node), size = 3, data = nodepie(anc_trait)) +
- scale_color_viridis_c() +
- theme(legend.position = "right")
- # 显示图形
- p
复制代码
添加系统发育信号
- # 计算Blomberg's K
- K <- phylosig(tree, cont_trait, test = TRUE)
- print(K)
- # 在图上添加系统发育信号信息
- p <- ggtree(tree) +
- geom_tiplab(aes(color = cont_trait)) +
- scale_color_viridis_c() +
- geom_text(aes(x = 0, y = 0, label = paste("Blomberg's K =", round(K$K, 2),
- "\np =", round(K$P, 3))),
- size = 4, hjust = 0, vjust = 0) +
- theme(legend.position = "right")
- # 显示图形
- p
复制代码
添加子树高亮
- # 选择要高亮的子树
- subtree <- getSubtree(tree, node = 15) # 假设我们想高亮节点15的子树
- # 高亮子树
- ggtree(tree) +
- geom_tiplab() +
- geom_hilight(node = 15, fill = "steelblue", alpha = 0.3) +
- geom_point2(aes(subset = (node == 15)), size = 5, color = "red")
复制代码
添加比较树
- # 生成第二棵树用于比较
- set.seed(456)
- tree2 <- rtree(10)
- # 准备数据
- trees <- list(tree1 = tree, tree2 = tree2)
- # 绘制比较树
- ggtree(trees) +
- geom_tiplab() +
- geom_taxalink("t1", "t9", color = "steelblue") +
- geom_taxalink("t3", "t7", color = "firebrick")
复制代码
多种输出格式
导出为PDF
- # 保存为PDF
- pdf("evolutionary_tree.pdf", width = 10, height = 8)
- print(p_tree)
- dev.off()
- # 使用ggsave(适用于ggplot对象)
- ggsave("evolutionary_tree_ggplot.pdf", p_tree, width = 10, height = 8, dpi = 300)
复制代码
导出为PNG/JPEG
- # 保存为PNG
- png("evolutionary_tree.png", width = 3000, height = 2400, res = 300)
- print(p_tree)
- dev.off()
- # 使用ggsave
- ggsave("evolutionary_tree_ggplot.png", p_tree, width = 10, height = 8, dpi = 300)
- # 保存为JPEG
- jpeg("evolutionary_tree.jpg", width = 3000, height = 2400, res = 300)
- print(p_tree)
- dev.off()
- # 使用ggsave
- ggsave("evolutionary_tree_ggplot.jpg", p_tree, width = 10, height = 8, dpi = 300, quality = 90)
复制代码
导出为TIFF
- # 保存为TIFF(适合出版)
- tiff("evolutionary_tree.tiff", width = 3000, height = 2400, res = 300)
- print(p_tree)
- dev.off()
- # 使用ggsave
- ggsave("evolutionary_tree_ggplot.tiff", p_tree, width = 10, height = 8, dpi = 300)
复制代码
导出为SVG
- # 保存为SVG(矢量图,可无限缩放)
- svg("evolutionary_tree.svg", width = 10, height = 8)
- print(p_tree)
- dev.off()
- # 使用ggsave
- ggsave("evolutionary_tree_ggplot.svg", p_tree, width = 10, height = 8)
复制代码
导出为PPT
- # 安装和加载export包
- install.packages("export")
- library(export)
- # 导出为PPT
- graph2ppt(file = "evolutionary_tree.pptx", width = 10, height = 8)
复制代码
导出为Word
- # 导出为Word
- graph2doc(file = "evolutionary_tree.docx", width = 10, height = 8)
复制代码
实用技巧与案例分享
技巧1:批量处理多棵树
- # 生成多棵树
- trees <- lapply(1:5, function(x) rtree(10))
- # 为每棵树添加标签
- labeled_trees <- lapply(trees, function(tree) {
- ggtree(tree) +
- geom_tiplab() +
- ggtitle(paste("Tree", which(trees == tree)[1]))
- })
- # 组合多棵树
- combined_plot <- plot_grid(plotlist = labeled_trees, ncol = 2)
- # 保存组合图
- ggsave("combined_trees.pdf", combined_plot, width = 12, height = 15)
复制代码
技巧2:添加图片到进化树
- # 假设我们有一些物种图片,存储在本地
- # 这里我们使用grid包的rasterGrob函数来添加图片
- library(grid)
- library(png)
- # 创建示例图片(实际应用中替换为你的图片路径)
- img_paths <- paste0("species_", 1:10, ".png")
- # 读取图片并创建grob对象
- img_list <- lapply(img_paths, function(path) {
- # 如果图片不存在,创建一个示例图片
- if (!file.exists(path)) {
- png(filename = path, width = 100, height = 100)
- plot(1, type = "n", axes = FALSE, ann = FALSE)
- text(1, 1, labels = gsub(".png", "", path), cex = 2)
- dev.off()
- }
- rasterGrob(readPNG(path))
- })
- # 创建带有图片的进化树
- p <- ggtree(tree) +
- geom_tiplab(align = TRUE, linesize = 0.5) +
- geom_image(aes(image = image), data = data.frame(
- label = tree$tip.label,
- image = I(img_list)
- ), size = 0.1, alpha = 0.8) +
- xlim(0, 6)
- # 显示图形
- p
复制代码
技巧3:创建交互式进化树
- # 安装和加载必要的包
- install.packages("plotly")
- library(plotly)
- # 创建基本进化树
- p <- ggtree(tree) +
- geom_tiplab() +
- geom_nodepoint()
- # 转换为交互式图形
- interactive_p <- ggplotly(p)
- # 保存交互式图形
- htmlwidgets::saveWidget(interactive_p, "interactive_tree.html")
复制代码
技巧4:添加地理分布信息
- # 创建示例地理数据
- geo_data <- data.frame(
- species = tree$tip.label,
- longitude = runif(10, -180, 180),
- latitude = runif(10, -90, 90)
- )
- # 创建进化树和地图的组合图
- library(maps)
- library(mapdata)
- # 创建地图
- world_map <- map_data("world")
- map_plot <- ggplot() +
- geom_map(data = world_map, map = world_map,
- aes(x = long, y = lat, map_id = region),
- fill = "white", color = "black", size = 0.2) +
- geom_point(data = geo_data, aes(x = longitude, y = latitude, color = species),
- size = 3) +
- coord_fixed(1.3) +
- theme_minimal()
- # 组合进化树和地图
- combined_plot <- plot_grid(p_tree, map_plot, ncol = 2, labels = c("A", "B"))
- # 保存组合图
- ggsave("tree_with_map.pdf", combined_plot, width = 15, height = 8)
复制代码
技巧5:创建时间校准的进化树
- # 创建一些示例时间校准点
- calibration_points <- data.frame(
- node = c(15, 18), # 节点编号
- age_min = c(10, 20), # 最小年龄(百万年)
- age_max = c(15, 25) # 最大年龄(百万年)
- )
- # 创建时间校准的进化树
- p <- ggtree(tree) +
- geom_tiplab() +
- geom_nodepoint() +
- geom_range(aes(xmin = age_min, xmax = age_max, y = y),
- data = calibration_points, color = "blue", size = 1.5) +
- scale_x_continuous(breaks = seq(0, 50, by = 10),
- labels = paste(seq(50, 0, by = -10), "Ma")) +
- theme_tree2() +
- ggtitle("Time-Calibrated Phylogenetic Tree") +
- xlab("Time (Million Years Ago)")
- # 显示图形
- p
复制代码
总结与展望
本文详细介绍了如何使用R语言创建专业进化树并掌握多种输出格式。从基础进化树绘制到高级可视化技巧,我们探讨了如何通过R语言的强大功能,将科研数据转化为清晰、美观且信息丰富的进化树可视化作品。
通过本文的学习,你不仅掌握了使用ape和ggtree等包绘制基础进化树的方法,还学会了如何美化进化树、添加支持值、结合其他数据类型、创建交互式图形等高级技巧。同时,我们还介绍了如何将进化树导出为PDF、PNG、JPEG、TIFF、SVG等多种格式,以满足不同出版和展示需求。
这些技能将帮助你的研究数据脱颖而出,提升学术影响力与认可度,获得更多引用与合作机会,推动研究发展与创新,促进学术交流与进步,提升价值与影响力,增强竞争力与声誉,推动发展与进步,促进创新。
展望未来,随着R语言生态系统的不断发展,我们可以期待更多强大而便捷的进化树可视化工具的出现。同时,结合机器学习、虚拟现实等新兴技术,进化树可视化将变得更加智能、交互和沉浸式,为科研人员提供更深入的数据洞察和更直观的结果展示。
无论你是进化生物学的新手还是经验丰富的研究者,希望本文提供的教程和技巧能够帮助你在科研数据可视化的道路上取得新的突破,让你的研究成果更加引人注目,为科学进步贡献力量。
版权声明
1、转载或引用本网站内容(科研数据可视化新高度 使用R语言创建专业进化树并掌握多种输出格式的详细教程与实用技巧分享让你的研究数据脱颖而出提升学术影响力与认可度获得更多引用与合作机会推动研究发展与创新促进学术交流与进步提升价值与影响力增强竞争力与声誉推动发展与进步促进创新)须注明原网址及作者(威震华夏关云长),并标明本网站网址(https://www.pixtech.cc/)。
2、对于不当转载或引用本网站内容而引起的民事纷争、行政处理或其他损失,本网站不承担责任。
3、对不遵守本声明或其他违法、恶意使用本网站内容者,本网站保留追究其法律责任的权利。
本文地址: https://www.pixtech.cc/thread-40914-1-1.html
|
|