版本与运行环境信息

  1. Date:2021-4-14
  2. R version 4.0.3 (2020-10-10)
  3. Platform: x86_64-w64-mingw32/x64 (64-bit)
  4. Running under: Windows 10 x64 (build 18363)

载入相关包

  1. library("ggtree")
  2. library("tidyverse")
  3. library("treeio")
  4. library("ggtreeExtra")
  5. library("ggsci")
  6. #ggtreeExtraA安装
  7. #install.packages("remotes")
  8. #remotes::install_github("YuLab-SMU/ggtreeExtra")

设置工作目录

  1. setwd("D:\\Desktop\\s_note\\data\\ggtreeextract")
  2. #node.label = "support"`**: 表示节点可以增加其他信息
  3. #**`split()`**: 按照分组因子,把向量,矩阵和数据框进行适当的分组。它的返回值是一个列表,代表分组变量每个水平的观测。

绘制进化树

构建进化树

  1. #读入数据
  2. tree <- read.tree("tree.nwk")
  3. #使用ggtree构建系统发育树
  4. p1 <- ggtree(tree,layout = "fan",open.angle =10,size = 0.5)
  5. ###常用选项
  6. #layout:树形状,常见的又fan、circual(圆形)、rectangular(正常的竖)
  7. #open.angle:开口角度
  8. #size: 线条粗细
  9. #branch.length:none则忽略枝条长度
  10. #root.position:指定根节点,默认0为根节点

ggtreeExtra-系统发育树 - 图1

读入第一圈数据

  1. dat1 <- read.csv("tree_tippoint_bar.csv",header = T)
  2. #查看第一圈数据情况
  3. > dim(dat1)
  4. [1] 230 5
  5. > head(dat1)
  6. ID Location Length Group Abundance
  7. 1 DE0655_HCMC_2001 HK 0.1786629 Yes 12.331055
  8. 2 MS0111_HCMC_1996 HK 0.2105236 Yes 9.652052
  9. 3 MS0063_HCMC_1995 HK 1.4337983 Yes 11.584822
  10. 4 DE0490_HCMC_2000 HK 0.3823731 Yes 7.893231
  11. 5 DE0885_HCMC_2001 HK 0.8478901 Yes 12.117232
  12. 6 DE0891_HCMC_2001 HK 1.5038646 Yes 10.819734
  13. #加入第一圈数据,构建新的发育树,加入星星图,使用geom_star函数
  14. ##geom_fruit选项
  15. #geom: 指定加入图的类型,如,geom_bar,geom_col,geom_boxplot,geom_violin,geom_tile、geom_star....
  16. #mapping :构建数据映射,注意x和y!!
  17. #starstroke: 外框的宽度
  18. #size:星星大小
  19. #alpha: 透明度
  20. #starshape :指定形状,本例为1:星星;15:点
  21. #show.legend = T:是否显示图例
  22. #offset:图距树的距离
  23. ##guide对图例进行调整
  24. #keywidth/keyheight :图例的宽和长(整体包括字)
  25. #direction:图例的排序,horizontal(水平)、vertical(垂直)
  26. #title.positio:图例标题位置,left right top .....
  27. #title: 图例标题
  28. #order:图例处于第几个
  29. p2 <- p1 +
  30. geom_fruit(data = dat1,
  31. geom = geom_star,
  32. mapping = aes(y = ID,fill = Location, starshape = Group),
  33. position = "identity",
  34. starstroke = 0.2,
  35. size = 2) +
  36. scale_starshape_manual(values = c(1,15),
  37. guide = guide_legend(keywidth = 0.5,keyheight = 0.5,order = 3,direction = "horizontal",title.position = "top")) +
  38. scale_fill_npg(guide = guide_legend(keywidth = 0.5,keyheight = 0.5,order = 2,override.aes=list(starshape=15)))

ggtreeExtra-系统发育树 - 图2

读入第二圈数据

  1. #读入第二圈数据
  2. dat2 <- read.csv("first_ring_discrete.csv",header = T)
  3. > dim(dat2)
  4. [1] 1024 3
  5. > head(dat2)
  6. ID Pos Type
  7. 1 DE0846_HCMC_2001 8 type2
  8. 2 MS0034_HCMC_1995 8 type2
  9. 3 EG1017_HCMC_2009 6 type2
  10. 4 KH18_HCMC_2009 5 type2
  11. 5 10365_HCMC_2010 7 type2
  12. 6 EG1021_HCMC_2009 1 type1
  13. #####构建第二圈,热图
  14. p3 <-p2 + new_scale_fill() +
  15. geom_fruit(data = dat2,
  16. geom = geom_tile,
  17. mapping = aes(y = ID ,x = Pos,fill = Type),pwidth = 0.2,offset = 0.08) +
  18. scale_fill_d3(guide = guide_legend(keywidth = 0.5,keyheight = 0.5,order = 1,direction = "horizontal",title.position = "top"))
  19. p3

ggtreeExtra-系统发育树 - 图3

读入第三圈数据

  1. dat3 <- read.csv("second_ring_continuous.csv",header = T)
  2. > dim(dat3)
  3. [1] 200 3
  4. > head(dat3)
  5. ID Type2 Alpha
  6. 1 MS0004_HCMC_1995 p3 0.2256195
  7. 2 DE1150_HCMC_2002 p2 0.2222086
  8. 3 MS0048_HCMC_1995 p2 0.1881510
  9. 4 HUE57_HCMC_2010 p3 0.1939088
  10. 5 DE1486_HCMC_2002 p2 0.2018493
  11. 6 DE1165_HCMC_2002 p3 0.1812997
  12. #构建第三圈数据
  13. p4 <- p3 +
  14. new_scale_fill()+
  15. geom_fruit(data = dat3,
  16. geom = geom_tile,
  17. mapping = aes(y = ID,x = Type2,fill = Type2),pwidth = 0.1,offset = 0.05,axis.params = list(axis = "x",color = "black",text.angle = 45,hjust = 0.5,vjust = 1))+
  18. scale_fill_brewer(direction = -1,palette = 5,guide = guide_legend(keywidth = 0.5,keyheight = 0.5,order = 5,direction = "vertical",title.position = "top"))
  19. p4
  20. #使用axis.params设定坐标轴
  21. #axis::"x","y","xy"
  22. #title: 坐标轴标题
  23. #title.angle/size/height/color/angel: 字体角度/大小/距离/颜色/角度
  24. #hjust/vjust: 调节字的位置
  25. #nbreak:坐标轴分为几份
  26. #line.size/color/alpha: 坐标轴的大小/颜色/透明度

ggtreeExtra-系统发育树 - 图4

读入第四圈数据

  1. dat5 <- read.csv("dat5.csv",header = T)
  2. > dim(dat5)
  3. [1] 690 5
  4. > head(dat5)
  5. ID Location Length Group Abundance
  6. 1 DE0655_HCMC_2001 HK 0.1786629 Yes 12.331055
  7. 2 MS0111_HCMC_1996 HK 0.2105236 Yes 9.652052
  8. 3 MS0063_HCMC_1995 HK 1.4337983 Yes 11.584822
  9. ......
  10. #构建第四圈数据,加入一个箱线图
  11. p6 <- p5 + new_scale_fill() + geom_fruit(data = dat5,
  12. geom = geom_boxplot,
  13. mapping = aes(y= ID, x= Abundance,fill = Location),
  14. size = 0,
  15. grid.params = list()) + scale_fill_npg()

ggtreeExtra-系统发育树 - 图5