在模仿中精进数据可视化_使用ggtree进行进化树的美化
❝❞
在模仿中精进数据可视化
该系列推文中,我们将从「各大顶级学术期刊」的Figure
入手,
「解读」文章的绘图思路,
「模仿」文章的作图风格,
「构建」适宜的绘图数据,
并且将代码「应用」到自己的实际论文中。
❝本期分享的期刊是:
❞ISME
上面的Figure 5a
,具体文章信息如下图:
参考文献
原图
原图
图片解读
首先,映入眼帘的是进化树,对不同 node
分别映射了颜色。其次,是一个柱形图。 最后是两张热图,并且热图的方块大小有一些细节的等长设置。 最后把所有的图拼接在一起。
图片复现
图片复现
❝本期推文的图片是基于
❞tidyverse
处理数据,使用Y叔
的大作ggtree
以及aplot
绘制图片,虽然数据和配色略有不同,但是图片中的绝大部分的细节都实现了较高的还原。
代码
❝由于我并没有获取到作者文章的数据,因此我重新构建了绘图所需的所有数据。
❞
加载绘图所需的R
包:
####----Load R Package----####
library(tidyverse)
library(ggtree)
library(tidytree)
library(treeio)
library(aplot)
library(ggfun)
加载绘图数据:
####----Load Data----####
# tree file
tree <- ggtree::read.tree(file = "example.tree")
# bar file
bar_df <- read_csv(file = "bar_df.csv")
# heatmap file 1
heatmap_df1 <- read_csv(file = "heatmap_df1.csv")
# heatmap file 2
heatmap_df2 <- read_csv(file = "heatmap_df2.csv")
可视化和图片导出
####----Plot and Save----####
# tree plot
p1 <- ggtree(tree) +
geom_tiplab(offset = 0.1, align = T) +
xlim(NA,40) +
geom_cladelab(node = 90, barcolor = "#a8ddb5", geom = "text", label = "",barsize = 28, alpha = 0.3,extend = 0.5, offset = 11.5) +
geom_cladelab(node = 81, barcolor = "#377eb8", geom = "text", label = "",barsize = 28, alpha = 0.3,extend = 0.5, offset = 8.5) +
geom_cladelab(node = 66, barcolor = "#4daf4a", geom = "text", label = "",barsize = 28, alpha = 0.3,extend = 0.5, offset = 11.5) +
geom_cladelab(node = 112, barcolor = "#984ea3", geom = "text", label = "",barsize = 28, alpha = 0.3,extend = 0.5, offset = 5) +
geom_cladelab(node = 101, barcolor = "#ff7f00",geom = "text", label = "",barsize = 28, alpha = 0.3, extend = 0.5, offset = 9.5) +
geom_tiplab(offset = 0.1, align = T) +
geom_tree2()
p1
p2 <- ggtree(tree) +
geom_tiplab(offset = 0.1, align = T) +
xlim(NA,100) +
geom_hilight(node = 90, fill = "#a8ddb5", alpha = 0.3, extendto = 32, type = "roundrect") +
geom_hilight(node = 81, fill = "#377eb8", alpha = 0.3, extendto = 32, type = "roundrect") +
geom_hilight(node = 66, fill = "#4daf4a", alpha = 0.3, extendto = 32, type = "roundrect") +
geom_hilight(node = 112, fill = "#984ea3", alpha = 0.3, extendto = 32, type = "roundrect") +
geom_hilight(node = 101, fill = "#ff7f00", alpha = 0.3, extendto = 32, type = "roundrect") +
geom_tiplab(offset = 0.1, align = T) +
geom_tree2()
# barplot
bar_plot <- bar_df %>%
tidyr::pivot_longer(cols = c("HL","HH","VL","VH"),
names_to = "Type",
values_to = "Value") %>%
dplyr::mutate(Type = factor(Type, levels = c("HL","HH","VL","VH"), ordered = T)) %>%
dplyr::mutate(ASV = factor(ASV, levels = rev(gene_name), ordered = T)) %>%
ggplot() +
geom_bar(aes(x = ASV, y = Value), stat = "identity", fill = "#4daf4a") +
facet_wrap(~Type, scales = "free", nrow = 1) +
coord_flip() +
theme_nothing() +
theme(strip.text = element_text(size = 15))
bar_plot
# heatmap1
heatmap_plot1 <- heatmap_df1 %>%
tidyr::pivot_longer(cols = contains("P"),
names_to = "Type",
values_to = "Value") %>%
dplyr::mutate(ASV = factor(ASV, levels = rev(gene_name), ordered = T)) %>%
dplyr::mutate(Value = case_when(
Value > 0 ~ "Resistance_Gene",
Value <= 0 ~ "unResistance_Gene")) %>%
dplyr::mutate(Type = factor(Type, levels = colnames(heatmap_df1)[-1], ordered = T)) %>%
ggplot() +
geom_tile(aes(x = Type, y = ASV, fill = Value), color = "#b2182b", height = 0.75, width = 0.75) +
scale_fill_manual(values = c("Resistance_Gene" = "#f46d43",
"unResistance_Gene" = "#ffffff")) +
scale_x_discrete(position = "top") +
labs(x = "", y = "") +
# coord_equal() +
theme_noyaxis() +
theme(axis.text.x.top = element_text(angle = 45, size = 10),
axis.line.x.top = element_blank(),
axis.ticks.x.top = element_blank(),
panel.background = element_rect(fill = "#ffffff"))
heatmap_plot1
# heatmap2
heatmap_plot2 <- heatmap_df2 %>%
tidyr::pivot_longer(cols = contains("H"),
names_to = "Type",
values_to = "Value") %>%
dplyr::mutate(ASV = factor(ASV, levels = rev(gene_name), ordered = T)) %>%
dplyr::mutate(Value = case_when(
Value > 0 ~ "Cycle_Gene",
Value <= 0 ~ "unCycle_Gene")) %>%
dplyr::mutate(Type = factor(Type, levels = colnames(heatmap_df2)[-1], ordered = T)) %>%
ggplot() +
geom_tile(aes(x = Type, y = ASV, fill = Value), color = "#2166ac", height = 0.75, width = 0.75) +
scale_fill_manual(values = c("Cycle_Gene" = "#4393c3",
"unCycle_Gene" = "#ffffff")) +
scale_x_discrete(position = "top") +
labs(x = "", y = "") +
# coord_equal() +
theme_noyaxis() +
theme(axis.text.x.top = element_text(angle = 45, size = 10),
axis.line.x.top = element_blank(),
axis.ticks.x.top = element_blank(),
panel.background = element_rect(fill = "#ffffff"))
heatmap_plot2
# combine plot
# bar_plot %>% insert_left(., p1)
heatmap_plot2 %>%
insert_left(., heatmap_plot1, width = 0.3) %>%
insert_left(., bar_plot, width = 0.35) %>%
insert_left(., p1, width = 0.45)
# save plot
ggsave(filename = "plot.pdf",
height = 14,
width = 18,
limitsize = FALSE)
最后的图片:
版本信息
####----Version Information----####
sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS 14.1.1
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] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggfun_0.1.3 aplot_0.2.2 treeio_1.26.0 tidytree_0.4.5 ggtree_3.10.0 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4
[10] purrr_1.0.2 readr_2.1.4 tidyr_1.3.0 tibble_3.2.1 ggplot2_3.4.4 tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] yulab.utils_0.1.0 utf8_1.2.4 generics_0.1.3 ggplotify_0.1.2 stringi_1.8.2 lattice_0.22-5 hms_1.1.3 digest_0.6.33
[9] magrittr_2.0.3 timechange_0.2.0 grid_4.3.0 fastmap_1.1.1 jsonlite_1.8.7 ape_5.7-1 fansi_1.0.5 scales_1.2.1
[17] lazyeval_0.2.2 cli_3.6.1 rlang_1.1.2 munsell_0.5.0 withr_2.5.2 cachem_1.0.8 tools_4.3.0 parallel_4.3.0
[25] tzdb_0.4.0 memoise_2.0.1 colorspace_2.1-0 vctrs_0.6.4 R6_2.5.1 gridGraphics_0.5-1 lifecycle_1.0.4 fs_1.6.3
[33] pkgconfig_2.0.3 pillar_1.9.0 gtable_0.3.4 glue_1.6.2 Rcpp_1.0.11 tidyselect_1.2.0 rstudioapi_0.15.0 nlme_3.1-163
[41] patchwork_1.1.3 compiler_4.3.0
❝❞
ggtree
和aplot
真的是yyds
~
历史绘图合集
联系
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到或者错过,请添加下面的微信来联系作者,一起交流数据分析和可视化。