在模仿中精进数据可视化_R语言组合微生物的物种统计图以及物种分类图
❝唠叨几句:最近陷入了很大的精神内耗,小时候受到的打压式的教育和讨好型人格,让我时常在羡慕别人为什么会经常收到褒奖,而我如此努力却总是有不足的地方需要改进。
有时候我会真的怀疑,是不是我真的不好,真的不行,真的很差劲。
这种内耗实在是太累了!
终归是要勇敢的做自己,毕竟我真的不比别人差,同时也总会有人发现你的优点。
OK!负能量结束!肝活!
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
❝今天还是依旧感谢我的叶师兄,这家伙审美没得说,给我推荐的论文也都很炫。叶师兄推荐的这篇论文,简直是美图大赏。跟随着优秀的师兄师姐师弟师妹们,也能不断提高数据可视化能力。
期待着叶师兄的大作!
论文来源
论文图片
图片复现:
❝这是一张很美的组合图,我认为的唯一的难点就是
Link
, 我因此随手写了一个demo。虽然没有那么顺滑的曲线,但是曲线的效果肯定是达到了,如何让曲线变得顺滑,这种精雕细琢的工作,那么就看个人需要在里面花费多少功夫了。
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(readxl)
library(ggalt)
library(ggraph)
library(tidygraph)
library(patchwork)
library(ggfun)
library(ggh4x)
source("R/stat_taxonomy_4_graph.R")
source("R/get_line.R")
# 自定义了两个函数
# 第一个对数据进行统计
# 第二个获取Link的数据
加载数据
####----load Data----####
otu_stat <- stat_taxonomy_4_graph(file1 = "Input/16S_otutab.xlsx",
file2 = "Input/16S_taxonomy.xlsx",
topn = 5)
可视化
####----Phylum水平的可视化----####
p1 <- otu_stat[["stat_Phylum"]] %>%
dplyr::filter(Group == "Phylum") %>%
ggplot() +
geom_bar(aes(x = Type, y = total, fill = Type), stat = "identity", width = 0.6) +
geom_text(aes(x = Type, y = total + 50, label = total)) +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
labs(x = "", y = "Total") +
theme_classic() +
theme(
axis.text = element_text(color = "#000000", size = 12),
axis.text.x = element_text(angle = 90, hjust = 0.5, vjust = 0.5),
plot.margin = margin(t = 1, r = 1, b = 0, l = 1, unit = "cm"),
axis.title.y = element_text(color = "#000000", size = 15),
axis.ticks.length.x.bottom = unit(6, "pt"),
axis.ticks = element_line(linewidth = 1)
)
p1
####----Class, Order, Family水平的可视化----####
p2 <- otu_stat[["stat_COF"]] %>%
dplyr::group_by(Gruop2, Group) %>%
dplyr::summarise(sum = sum(Number)) %>%
dplyr::ungroup() %>%
ggplot() +
geom_text(aes(x = Gruop2, y = Group, label = sum), size = 5) +
guides(x.sec = guide_axis_manual(breaks = 1:9,labels = sort(unique(otu_stat[["stat_COF"]]$Gruop2)))) +
labs(x = "") +
theme_bw() +
theme(
axis.text.x = element_blank(),
axis.text.y = element_text(color = "#000000", size = 12),
axis.ticks.x.bottom = element_blank(),
axis.ticks.length.x.top = unit(6, "pt"),
panel.grid = element_blank(),
panel.border = element_rect(color = "#000000", linewidth = 0.8),
plot.margin = margin(t = 0, r = 1, b = 0, l = 1, unit = "cm"),
axis.title.y = element_text(color = "#000000", size = 15),
axis.ticks = element_line(linewidth = 1)
)
p2
####----物种分类图----####
graph_data <- tbl_graph(otu_stat[["node"]], otu_stat[["edge"]])
p3 <- ggraph(graph_data, "tree") +
geom_hline(yintercept = 4) +
geom_edge_diagonal(color = "#bdbdbd", edge_width = 1) +
geom_node_point(aes(size = size), shape = 21, color = "#a6bddb", fill = "#ffffff", stroke = 3) +
geom_node_text(aes(x = x, label = node),
angle = 90, vjust = 1.75, fontface = "italic") +
theme_void() +
theme(
plot.margin = margin(t = 0, r = 1, b = 1, l = 1, unit = "cm")
)
p3
p3$data
####----Link----####
line_df <- get_line(n = 20)
p4 <- line_df %>%
ggplot() +
# geom_point(aes(x = x, y = y, group = Phy, color = Phy)) +
# geom_line(aes(x = x, y = y, group = Phy, color = Phy), linewidth = 1.5) +
geom_xspline(aes(x = x, y = y, group = Phy, color = Phy), spline_shape=1, size=0.5) +
scale_x_continuous(limits = c(-20, 25)) +
labs(x = "", y = "") +
theme_bw() +
theme(
panel.border = element_blank(),
panel.grid = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
plot.margin = margin(t = 0, r = 0, b = 0, l = 0, unit = "cm")
)
p4
####----combine----####
p_combine <- p1 / p2 / p4 / p3 + plot_layout(heights = c(3, 2, 1, 15))
ggsave(filename = "./Output/tmp.pdf",
plot = p_combine,
height = 20,
width = 12)
版本信息
####----sessionInfo----####
sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS 15.0.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] ggh4x_0.2.8.9000 ggfun_0.1.5 patchwork_1.2.0.9000
[4] tidygraph_1.2.3 ggraph_2.1.0 ggalt_0.4.0
[7] readxl_1.4.3 lubridate_1.9.3 forcats_1.0.0
[10] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2
[13] readr_2.1.5 tidyr_1.3.1 tibble_3.2.1
[16] ggplot2_3.5.1 tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] gtable_0.3.5 ggrepel_0.9.6 tzdb_0.4.0 vctrs_0.6.5
[5] tools_4.3.0 generics_0.1.3 fansi_1.0.6 pkgconfig_2.0.3
[9] KernSmooth_2.23-22 RColorBrewer_1.1-3 lifecycle_1.0.4 compiler_4.3.0
[13] farver_2.1.2 textshaping_0.3.7 munsell_0.5.1 proj4_1.0-13
[17] ggforce_0.4.2 graphlayouts_1.0.2 ash_1.0-15 maps_3.4.1.1
[21] Rttf2pt1_1.3.12 pillar_1.9.0 crayon_1.5.2 extrafontdb_1.0
[25] MASS_7.3-60 viridis_0.6.4 tidyselect_1.2.1 digest_0.6.36
[29] stringi_1.8.3 labeling_0.4.3 extrafont_0.19 polyclip_1.10-7
[33] grid_4.3.0 colorspace_2.1-1 cli_3.6.3 magrittr_2.0.3
[37] utf8_1.2.4 withr_3.0.1 scales_1.3.0 timechange_0.2.0
[41] igraph_2.0.3 gridExtra_2.3 cellranger_1.1.0 ragg_1.2.6
[45] hms_1.1.3 viridisLite_0.4.2 rlang_1.1.4 Rcpp_1.0.13
[49] glue_1.7.0 tweenr_2.0.3 rstudioapi_0.15.0 R6_2.5.1
[53] systemfonts_1.1.0
历史绘图合集
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。