在模仿中精进数据可视化_使用ggtree绘制发表级基因家族进化树
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘制图片如下:
❝依旧是进化树,依旧是
ggtree
。只能说是y叔yyds
!,图中包含了非常多的内容,大家可以仔细看看图。
❝公众号的粉丝们,应该是人人都会
ggtree
了吧!
代码如下
加载R包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(ggtree)
library(treeio)
library(tidytree)
library(ggtreeExtra)
library(ggfun)
library(ggnewscale)
library(patchwork)
library(readxl)
library(writexl)
加载数据
####----load Data----####
tree <- treeio::read.newick(file = "WRKY_TAIR.pep.muscle.treefile",
node.label = "support")
# get ID
get_taxa_name(ggtree(tree, branch.length="none") ) -> tree.sort.id
tree_df <- as_tibble(tree)
# get gene family Information
WRKY_info <- read_xlsx(path = "WRKY_information.xlsx", col_names = T)
WRKY_info2 <- WRKY_info %>%
dplyr::select(1,4,5,6,8) %>%
tidyr::pivot_longer(cols = -c(ID, Group),
names_to = "Type",
values_to = "Value")
开始画图
ggtree(tree, layout = "fan", open.angle = 90, branch.length = "none") %<+% tree_df +
scale_color_manual(values = "#ef3b2c",na.translate=FALSE, name = "Signif") +
geom_cladelab(node=80, label="Group1", align=TRUE, angle = 130,
offset = 4.5,offset.text=-0.6, textcolor='#8c6bb1', barcolor='#8c6bb1',
barsize=1.5,extend = 0.7) +
geom_cladelab(node=80, label="", align=TRUE, angle = 270,
offset = 1.8,offset.text=1, textcolor='#bfd3e6', barcolor='#bfd3e6',
barsize=30,extend = 0.7) +
geom_cladelab(node=104, label="Group2", align=TRUE, angle = 240,
offset = 4.5,offset.text=-0.6, textcolor='#1d91c0', barcolor='#1d91c0',
barsize=1.5,extend = 0.7) +
geom_cladelab(node=104, label="", align=TRUE, angle = 270,
offset = 1.8,offset.text=1, textcolor='#41b6c4', barcolor='#41b6c4',
barsize=30,extend = 0.7) +
geom_cladelab(node=120, label="Group3", align=TRUE, angle = 180,
offset = 4.5,offset.text=-0.6, textcolor='#ef6548', barcolor='#ef6548',
barsize=1.5,extend = 0.7) +
geom_cladelab(node=120, label="", align=TRUE, angle = 270,
offset = 1.8,offset.text=1, textcolor='#fdd49e', barcolor='#fdd49e',
barsize=30,extend = 0.7) +
geom_cladelab(node=78, label="Group4", align=TRUE, angle = 120,
offset = 4.5,offset.text=-0.6, textcolor='#df65b0', barcolor='#df65b0',
barsize=1.5,extend = 0.7) +
geom_cladelab(node=78, label="", align=TRUE, angle = 270,
offset = 1.8,offset.text=1, textcolor='#d4b9da', barcolor='#d4b9da',
barsize=30,extend = 0.7) +
geom_nodepoint(aes(fill=cut(support, c(0, 50, 75, 100))),
shape=21, size=2) +
scale_fill_manual(values=c("black", "grey", "white"),
guide='legend', name='Bootstrap Percentage(BP)',
breaks=c('(75,100]', '(50,75]', '(0,50]'),
labels=expression(BP>75, 50 < BP * "<=75", BP <= 50)) +
geom_treescale(x = 0.7, fontsize = 5, linesize = 2) +
geom_tiplab(align = T, offset = 0.5) +
new_scale_fill() +
geom_fruit(
data = WRKY_info2,
geom = geom_tile,
mapping = aes(y = ID, x = Type, fill = Group, alpha = Value),
color = "#000000",
offset = 0.35,
size = 0.1
) +
geom_fruit(
data = WRKY_info,
geom = geom_point,
mapping = aes(y = ID, x = subcellular, color = subcellular),
offset = 0.08,
size = 5
) +
scale_color_manual(values = "#addd8e") +
geom_fruit(
data = WRKY_info,
geom = geom_bar,
mapping = aes(y = ID, x = Length, fill = Group),
pwidth=0.9,
# width = 0.75,
orientation="y",
stat="identity",
color = "#000000",
offset = 0.1,
linewidth = 0.3,
axis.params = list(
axis = "y",
line.color = "#000000",
line.size = 0.5)
) +
scale_fill_manual(values = c("#bfd3e6", "#41b6c4", "#fdd49e", "#d4b9da")) +
geom_tree() +
theme_tree(legend.position = c(0.65, 0.35)) +
theme(
legend.background = element_roundrect(color = "#969696")
)
####----save plot----####
ggsave(filename = "plot.pdf",
height = 20,
width = 20)
版本信息
####----sessionInfo----####
sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS 14.5
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.11.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Asia/Shanghai
tzcode source: internal
attached base packages:
[1] stats4 stats graphics grDevices utils datasets methods base
other attached packages:
[1] readxl_1.4.3 writexl_1.4.2 ggprism_1.0.4 ggbeeswarm_0.7.2 Peptides_2.4.5
[6] Biostrings_2.70.1 GenomeInfoDb_1.38.1 XVector_0.42.0 IRanges_2.36.0 S4Vectors_0.40.2
[11] BiocGenerics_0.48.1 ggtreeExtra_1.12.0 ggstar_1.0.4 patchwork_1.2.0.9000 ggnewscale_0.4.10.9000
[16] aplot_0.2.2 ggfun_0.1.3 tidytree_0.4.5 treeio_1.26.0 ggtree_3.10.0
[21] lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2
[26] readr_2.1.5 tidyr_1.3.1 tibble_3.2.1 ggplot2_3.4.4 tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] beeswarm_0.4.0 gtable_0.3.4 lattice_0.22-5 tzdb_0.4.0
[5] bitops_1.0-7 vctrs_0.6.5 tools_4.3.0 generics_0.1.3
[9] yulab.utils_0.1.0 parallel_4.3.0 fansi_1.0.6 pkgconfig_2.0.3
[13] ggplotify_0.1.2 GenomeInfoDbData_1.2.11 lifecycle_1.0.4 compiler_4.3.0
[17] farver_2.1.1 textshaping_0.3.7 munsell_0.5.0 vipor_0.4.5
[21] RCurl_1.98-1.13 lazyeval_0.2.2 crayon_1.5.2 pillar_1.9.0
[25] cachem_1.0.8 nlme_3.1-163 tidyselect_1.2.1 digest_0.6.35
[29] stringi_1.8.3 labeling_0.4.3 fastmap_1.1.1 grid_4.3.0
[33] colorspace_2.1-0 cli_3.6.2 magrittr_2.0.3 utf8_1.2.4
[37] ape_5.8 withr_3.0.0 scales_1.3.0 timechange_0.2.0
[41] gridExtra_2.3 cellranger_1.1.0 ragg_1.2.6 hms_1.1.3
[45] memoise_2.0.1 gridGraphics_0.5-1 rlang_1.1.3 Rcpp_1.0.11
[49] glue_1.7.0 rstudioapi_0.15.0 jsonlite_1.8.7 R6_2.5.1
[53] systemfonts_1.0.5 fs_1.6.3 zlibbioc_1.48.0
历史绘图合集
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。