在模仿中精进数据可视化_使用R语言绘制NatureMicrobiology中的高级扇形图
❝写在前面的话:不知不觉之中在模仿中精进数据可视化已经有100个推文了,感觉各位朋友,对我画图推文还是蛮喜欢的。
由衷感谢大家对我的认可。
可以这么说,在模仿中精进数据可视化的系列推文中,我们打的就是精锐,在阅读高水平图片,模仿高水平论文可视化的过程中,实打实的提高了自己的审美能力,磨练自己的可视化技能。
100篇,即是一个记录,也是一个新的开始。
毕竟咱们的这个公众号也仅仅刚刚起步,时间与我都在向前走,未来我们的推文将会更加精彩!
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
❝笔者现在非常喜欢
Nature Microbiology
的文章。
未来将会长期驻扎在Nature Microbiology
期刊里面,对优秀的图片进行复现。
希望大家能跟着我在模仿中精进数据可视化!。
同时也欢迎大家投稿!
论文来源
论文图片
图片复现:
❝总而言之,还算不错。复现的较为全面。
我们绘图的结果中也更加的细节。
画图的原因:因为我自己的Paper会用到。
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(ggfun)
library(readxl)
library(writexl)
library(PieGlyph)
library(ggnewscale)
source("R/Stat_taxonomy.R")
# 这里我自定义了一个函数,
#主要是用于批量处理ASV表和注释信息,
#并且提取我们想要的数据,进行下游可视化
加载数据
####----load Data----####
otu_plot <- stat_taxonomy(file1 = "Input/16S_otutab.xlsx",
file2 = "Input/16S_taxonomy.xlsx",
file3 = "Input/ITS_otutab.xlsx",
file4 = "Input/ITS_taxonomy.xlsx",
topn = 3,
number = 10)
df1 <- otu_plot %>%
dplyr::rowwise() %>%
dplyr::mutate(sum = sum(c_across(where(is.numeric)))) %>%
dplyr::group_by(Phylum) %>%
dplyr::summarise(sum_all = sum(number)) %>%
dplyr::mutate(cumsum = cumsum(sum_all),
position = cumsum - 0.5 * sum_all,
Phylum = factor(Phylum, levels = rev(Phylum), ordered = T),
id = 1:nrow(.),
angle = 90 - 360 * (id-0.5) /nrow(.),
hjust = ifelse(angle < -90, 0.5, 0.5),
angle = ifelse(angle >-90, angle + 180, angle),
x = 1
)
df2 <- otu_plot %>%
dplyr::rowwise() %>%
dplyr::mutate(sum = sum(c_across(where(is.numeric)))) %>%
dplyr::group_by(Class) %>%
dplyr::summarise(sum_all = sum(number)) %>%
dplyr::mutate(cumsum = cumsum(sum_all),
position = cumsum - 0.5 * sum_all,
Class = factor(Class, levels = rev(Class), ordered = T),
id = 1:nrow(.),
angle = 90 - 360 * (id-0.5) /nrow(.),
hjust = ifelse(angle < -90, 0.5, 0.5),
angle = ifelse(angle >-90, angle + 180, angle),
x = 2
)
df3 <- otu_plot %>%
dplyr::select(3:7) %>%
dplyr::mutate(Class = as.character(Class)) %>%
dplyr::left_join(df2 %>% dplyr::mutate(Class = as.character(Class)),
by = c("Class" = "Class")) %>%
dplyr::mutate(Class = factor(Class, levels = rev(df2$Class), ordered = T)) %>%
dplyr::select(-c(sum_all, cumsum)) %>%
dplyr::select(1,6,2:5) %>%
dplyr::mutate(x = 2.9)
可视化
####----Plot----####
plot <- ggplot(data = df1) +
geom_col(aes(x = x, y = sum_all, fill = Phylum), width = 1, color = "#ffffff") +
scale_fill_manual(values = rev(c("#8dd3c7", "#bebada", "#80b1d3", "#b3cde3", "#fccde5",
"#feb24c", "#ef6548", "#f4a582")),
guide = guide_legend(label.theme = element_text(face = "italic"),
order = 1)) +
geom_text(aes(x = x, y = position, label = Phylum, hjust = hjust),
angle = df1$angle,
color = "#000000",
fontface = "italic") +
ggnewscale::new_scale_fill() +
geom_col(data = df2, aes(x = x, y = sum_all, fill = Class), width = 1,color = "#ffffff") +
scale_fill_manual(values = rev(rep(c("#8dd3c7", "#bebada", "#80b1d3", "#b3cde3", "#fccde5",
"#feb24c", "#ef6548", "#f4a582"),
each = 3)),
guide = guide_legend(label.theme = element_text(face = "italic"),
order = 2)) +
geom_text(data = df2, aes(x = x, y = position, label = Class, hjust = hjust),
angle = df2$angle,
color = "#000000",
fontface = "italic") +
ggnewscale::new_scale_fill() +
geom_pie_glyph(data = df3,
mapping = aes(x = x, y = position),
slices = c("Control", "Treatment1", "Treatment2","Treatment3"),
colour = "#000000", radius = 0.8) +
scale_fill_manual(values = c("#fa9fb5", "#7bccc4", "#9e9ac8", "#74c476"),
guide = guide_legend(order = 3)) +
coord_polar(theta = "y") +
theme_nothing() +
theme(plot.margin = margin(t = 0.5, r = 0.5, b = 0.5, l = 0.5, unit = "cm"))
ggsave(filename = "./Output/plot.pdf",
plot = plot,
height = 14,
width = 15)
版本信息
####----sessionInfo----####
sessionInfo()
R version 4.3.0 (2023-04-21)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS 14.6.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] ggnewscale_0.5.0 PieGlyph_1.0.0 writexl_1.4.2 readxl_1.4.3 ggfun_0.1.5 scatterpie_0.2.4
[7] lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2 readr_2.1.5
[13] tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.1 tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] ggiraph_0.8.7 tidyselect_1.2.1 viridisLite_0.4.2 farver_2.1.2 fastmap_1.2.0 lazyeval_0.2.2
[7] tweenr_2.0.3 promises_1.2.1 digest_0.6.36 timechange_0.2.0 mime_0.12 lifecycle_1.0.4
[13] ellipsis_0.3.2 processx_3.8.2 tidytree_0.4.5 magrittr_2.0.3 compiler_4.3.0 rlang_1.1.4
[19] tools_4.3.0 utf8_1.2.4 htmlwidgets_1.6.3 prettyunits_1.2.0 labeling_0.4.3 curl_5.1.0
[25] bit_4.0.5 pkgbuild_1.4.2 plyr_1.8.9 pkgload_1.3.3 miniUI_0.1.1.1 withr_3.0.1
[31] desc_1.4.2 grid_4.3.0 polyclip_1.10-7 fansi_1.0.6 urlchecker_1.0.1 profvis_0.3.8
[37] xtable_1.8-4 colorspace_2.1-1 scales_1.3.0 MASS_7.3-60 cli_3.6.3 crayon_1.5.2
[43] ragg_1.2.6 remotes_2.4.2.1 treeio_1.26.0 generics_0.1.3 rstudioapi_0.15.0 tzdb_0.4.0
[49] sessioninfo_1.2.2 ape_5.8 cachem_1.1.0 ggforce_0.4.2 parallel_4.3.0 cellranger_1.1.0
[55] yulab.utils_0.1.7 vctrs_0.6.5 devtools_2.4.5 jsonlite_1.8.7 callr_3.7.3 hms_1.1.3
[61] bit64_4.0.5 systemfonts_1.1.0 glue_1.7.0 ps_1.7.5 stringi_1.8.3 gtable_0.3.5
[67] later_1.3.1 munsell_0.5.1 pillar_1.9.0 htmltools_0.5.7 R6_2.5.1 textshaping_0.3.7
[73] rprojroot_2.0.4 vroom_1.6.4 shiny_1.8.0 lattice_0.22-5 memoise_2.0.1 httpuv_1.6.12
[79] uuid_1.1-1 Rcpp_1.0.13 nlme_3.1-163 usethis_2.2.2 fs_1.6.4 pkgconfig_2.0.3
历史绘图合集
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。