在模仿中精进数据可视化_继续使用R语言进行网络组合图
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
❝今天还是依旧感谢我的叶师兄,这家伙审美没得说,给我推荐的论文也都很炫。叶师兄推荐的这篇论文,简直是美图大赏。跟随着优秀的师兄师姐师弟师妹们,也能不断提高数据可视化能力。
期待着叶师兄的大作!
论文来源
论文图片
图片复现:
❝这是一张很美的组合图,
1.里面内圈是一个网络图
2.连续3个完全分别是分类变量、连续变量和连续变量
3.最外面是柱形图
整体来讲,比较有难度。
不过依然只使用R
代码完成了所有图片的绘制。
有小伙伴问我,这个图的实际应用场景,这是我自己的原话:
❝总之,发挥主观能动性,想象空间拉满,最后可视化出来!
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(readxl)
library(tidygraph)
library(ggraph)
library(igraph)
library(cowplot)
library(ggnewscale)
source("R/layout_function.R")
source("R/combine_plot.R")
加载数据
####----load Data----####
# node file
nodes <- readxl::read_xlsx(path = "Input/nodes.xlsx")
# edge file
edges <- readxl::read_xlsx(path = "Input/edges.xlsx")
# ppi file
ppi <- readxl::read_xlsx(path = "Input/ppi.xlsx")
####----Plot----####
graph <- as_tbl_graph(ppi) %>%
tidygraph::mutate(Popularity = centrality_degree(mode = 'out')) %>%
tidygraph::left_join(nodes, by = c("name" = "name"))
可视化
❝先画里面的网络图
####----Plot----####
graph <- as_tbl_graph(ppi) %>%
tidygraph::mutate(Popularity = centrality_degree(mode = 'out')) %>%
tidygraph::left_join(nodes, by = c("name" = "name"))
ly4 <- layout_function(graph)
p_ppi <- ggraph(ly4) +
geom_edge_diagonal(aes(color = type)) +
geom_node_point(aes(size = Popularity, color = type), alpha=1) +
geom_node_text(data = ly4 %>%
tidygraph::filter(name %in% c("Cis", "Ctrl")) ,
aes(label = name),
size = 5,
color = "#ffffff") +
scale_size(range = c(3, 15)) +
coord_fixed() +
theme_void() +
theme(legend.position = "none")
❝然后是依次画圈
p2 <- ggplot(data = data_ex) +
geom_tile(aes(x = id, y = 1, fill = data1), height = 0.2) +
scale_fill_manual(values = c('#8dd3c7','#ffffb3','#bebada','#fb8072','#80b1d3'), na.value = "transparent") +
new_scale_fill() +
geom_tile(aes(x = id, y = 1.25, fill = data2), height = 0.2) +
scale_fill_gradient(low = "#fcc5c0", high = "#dd3497", na.value = "transparent") +
new_scale_fill() +
geom_tile(aes(x = id, y = 1.5, fill = data3), height = 0.2) +
scale_fill_gradient(low = "#dadaeb", high = "#807dba", na.value = "transparent") +
coord_polar(
start = 1.17*pi
) +
scale_y_continuous(
limits = c(-1, 2.5)
) +
theme_void()
p2
❝然后是最外面的柱形图
data_ex_add_angle <- data_ex %>%
dplyr::mutate(angle = 90 - 360 * (id - 0.5)/ nrow(.) + (1.17* pi)) %>%
dplyr::mutate(hjust = ifelse(angle < -90, 0, 1)) %>%
dplyr::mutate(angle = ifelse(angle < -90, angle + 150, angle - 25))
p3 <- ggplot(data = data_ex_add_angle) +
geom_bar(aes(x = id, y = data4), stat = "identity", fill = "#d9d9d9") +
geom_text(data = data_ex_add_angle %>% dplyr::filter(!name %in% c("Cis","Ctrl")),
aes(x = id, y = data4+1, label = name, hjust = hjust),
angle = data_ex_add_angle %>% dplyr::filter(!name %in% c("Cis","Ctrl")) %>% pull(angle)) +
coord_polar(
start = 1.17*pi
) +
scale_y_continuous(
limits = c(-50, 30)
) +
theme_void()
p3
❝最后是拼图
####----combine----####
p_combine <- combine_plot(p2, p_ppi, p3)
p_combine
ggsave(filename = "Output/p_combine.pdf",
height = 12,
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] ggnewscale_0.5.0 cowplot_1.1.3 igraph_2.0.3 ggraph_2.1.0 tidygraph_1.2.3 lubridate_1.9.3 forcats_1.0.0
[8] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2 readr_2.1.5 tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.1
[15] tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] viridis_0.6.4 utf8_1.2.4 generics_0.1.3 stringi_1.8.3 hms_1.1.3 digest_0.6.36
[7] magrittr_2.0.3 grid_4.3.0 timechange_0.2.0 cellranger_1.1.0 writexl_1.4.2 ggrepel_0.9.6
[13] gridExtra_2.3 fansi_1.0.6 viridisLite_0.4.2 scales_1.3.0 tweenr_2.0.3 textshaping_0.3.7
[19] cli_3.6.3 rlang_1.1.4 graphlayouts_1.0.2 crayon_1.5.2 polyclip_1.10-7 munsell_0.5.1
[25] withr_3.0.1 tools_4.3.0 tzdb_0.4.0 colorspace_2.1-1 vctrs_0.6.5 R6_2.5.1
[31] lifecycle_1.0.4 MASS_7.3-60 ragg_1.2.6 pkgconfig_2.0.3 pillar_1.9.0 gtable_0.3.5
[37] glue_1.7.0 Rcpp_1.0.13 systemfonts_1.1.0 ggforce_0.4.2 tidyselect_1.2.1 rstudioapi_0.15.0
[43] farver_2.1.2 labeling_0.4.3 compiler_4.3.0 readxl_1.4.3
历史绘图合集
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。