在模仿中精进数据可视化_模仿linkET的风格使用R语言手搓一个相关性和连线图
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
❝前天绘制一张微生物领域常见的图
Mantel Test
图。
使用缊哥的神包linkET
。
期刊
原图
❝今天我使用
ggplot2
按照linkET
的风格,绘制一张相关性图。在此,对缊哥的linkET
致以敬意。
❝测试数据都是编造的,没有任何实际意义。
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(ggnewscale)
library(psych)
library(ggfun)
source("R/cor_test.R")
加载数据
####----load Data----####
# Environment Factor
Environment <- readr::read_delim(file = "./Input/Environment.csv", delim = ",") %>%
tibble::column_to_rownames(var = "Sample")
# Experiment Data
Experiment <- readr::read_delim(file = "./Input/Experiment.csv", delim = ",") %>%
tibble::column_to_rownames(var = "Sample")
# Correlation
stat_out <- cor_test(Environment, Experiment)
绘图
####----Plot----####
p <- stat_out[[1]] %>%
ggplot() +
geom_tile(aes(x = ID, y = Type),
fill = "white", color = "#000000", linewidth = 0.5) +
geom_point(aes(x = ID, y = Type, fill = Value, size = abs(Value)), shape = 21, color = "black") +
geom_text(data = stat_out[[2]],
aes(x = ID, y = Type, label = p_value)) +
scale_fill_gradient(low = "#edf8b1", high = "#1d91c0", name = "Env Cor",
guide = guide_colorbar(order = 1)) +
scale_x_discrete(position = "top") +
scale_size(range = c(6,14),
guide = guide_legend(order = 2),
name = "Env Cor Size") +
xlab('') + ylab('') +
coord_cartesian(clip = "off") +
geom_segment(data = stat_out[[3]] ,
mapping = aes(x = 12, y = 1, xend = start1, yend = end1,
linetype = p_value,
color = Value,
linewidth = abs(Value))) +
scale_color_gradient(low = "#e0f3db", high = "#4eb3d3", name = "CUE Cor",
guide = guide_colorbar(order = 3)) +
scale_linewidth(range = c(1, 2.5), name = "CUR Cor",
guide = guide_legend(order = 4)) +
scale_linetype(name = "CUE PValue",
guide = guide_legend(order = 5)) +
geom_point(data = stat_out[[3]],
mapping = aes(x = start2, y = end2), color = "#c51b8a", size = 5) +
geom_text(data = stat_out[[3]][1,],
mapping = aes(x = start2, y = end2-0.5, label = Experiment),color = "#c51b8a") +
geom_point(data = stat_out[[3]],
mapping = aes(x = start1, y = end1), fill = "#9e9ac8", size = 5, shape = 21) +
theme_minimal() +
theme(
panel.grid = element_blank(),
aspect.ratio = 1,
axis.text = element_text(color = "black", size = 12),
axis.text.x = element_text(angle=45, hjust=0),
plot.margin = margin(t = 1, r = 1, b = 1, l = 1, unit = "cm"),
legend.background = element_roundrect(color = "#969696")
)
p
ggsave(filename = "Output/p.pdf",
plot = p,
height = 10,
width = 10.5)
版本信息
R version 4.3.0 (2023-04-21)
Platform: x86_64-apple-darwin20 (64-bit)
Running under: macOS 15.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.5 psych_2.3.9 ggnewscale_0.5.0 lubridate_1.9.3 forcats_1.0.0
[6] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2 readr_2.1.5 tidyr_1.3.1
[11] tibble_3.2.1 ggplot2_3.5.1 tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] bit_4.0.5 gtable_0.3.5 crayon_1.5.2 compiler_4.3.0 tidyselect_1.2.1
[6] parallel_4.3.0 textshaping_0.3.7 systemfonts_1.1.0 scales_1.3.0 lattice_0.22-5
[11] R6_2.5.1 labeling_0.4.3 generics_0.1.3 munsell_0.5.1 pillar_1.9.0
[16] tzdb_0.4.0 rlang_1.1.4 utf8_1.2.4 stringi_1.8.3 bit64_4.0.5
[21] timechange_0.2.0 cli_3.6.3 withr_3.0.1 magrittr_2.0.3 grid_4.3.0
[26] vroom_1.6.4 rstudioapi_0.15.0 hms_1.1.3 nlme_3.1-163 lifecycle_1.0.4
[31] vctrs_0.6.5 mnormt_2.1.1 glue_1.8.0 farver_2.1.2 ragg_1.2.6
[36] fansi_1.0.6 colorspace_2.1-1 tools_4.3.0 pkgconfig_2.0.3
历史绘图合集
公众号推文一览
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。