在模仿中精进数据可视化_R语言绘制多组差异表达的结果
❝我一直在做事,没答案也要做事。
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
❝今天又是好朋友提供的素材。果然是好文章。
原图
❝很明显是两组的差异微生物的数据,然后绘制在一个坐标系里面了。其实就是一个散点图。
图片复现
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(readxl)
library(ggrepel)
加载数据
####----load Data----####
set.seed(20241125)
df <- read_xlsx(path = "Input/test_df.xlsx") %>%
dplyr::rowwise() %>%
dplyr::mutate(Value = mean(c(baseMean_group1.x, baseMean_group1.y))) %>%
dplyr::ungroup() %>%
dplyr::slice_sample(n = 500) %>%
dplyr::mutate(
Category = case_when(
Value > 2.5 & Value < 500 ~ "Group1",
Value >= 500 & Value < 10000 ~ "Group2",
Value >= 10000 & Value < 20000 ~ "Group3",
Value >= 20000 & Value < 40000 ~ "Group4"
)
) %>%
na.omit()
绘图
####----Plot----####
p <- ggplot(data = df,
aes(x = log2FoldChange_group1.x,
y = log2FoldChange_group1.y)) +
geom_vline(xintercept = 0, linetype = 2) +
geom_hline(yintercept = 0, linetype = 2) +
geom_point(aes(x = log2FoldChange_group1.x,
y = log2FoldChange_group1.y,
size = Value,
fill = Category),
alpha = 0.75,
shape = 21) +
geom_point(data = df %>% dplyr::filter(Category == "Group3"),
aes(x = log2FoldChange_group1.x,
y = log2FoldChange_group1.y,
size = Value),
fill = "#fb9a99",
shape = 21) +
geom_text_repel(data = df %>% dplyr::filter(Category == "Group3"),
aes(x = log2FoldChange_group1.x,
y = log2FoldChange_group1.y,
label = SYMBOL),
nudge_x = 0.5,
nudge_y = -0.25,
segment.curvature = -1e-20) +
geom_point(data = df %>% dplyr::filter(Category == "Group4"),
aes(x = log2FoldChange_group1.x,
y = log2FoldChange_group1.y,
size = Value),
fill = "#e78ac3",
shape = 21) +
geom_text_repel(data = df %>% dplyr::filter(Category == "Group4"),
aes(x = log2FoldChange_group1.x,
y = log2FoldChange_group1.y,
label = SYMBOL),
nudge_x = -0.5,
nudge_y = 0.25)+
scale_size(range = c(3, 15),
breaks = seq(min(df$Value), max(df$Value), length.out = 6))+
scale_fill_manual(values = c("Group1" = "#bdbdbd",
"Group2" = "#fdc086",
"Group3" = "#fb9a99",
"Group4" = "#e78ac3")) +
guides(
size = guide_legend(override.aes = list(fill = "#000000"), reverse = T),
fill = guide_legend(override.aes = list(alpha = 1, size = 10))
) +
xlim(c(-5, 5)) +
ylim(c(-2.5, 2.5)) +
labs(x = "Group1 log2(FoldChange)",
y = "Group2 log2(FoldChange)") +
theme_bw() +
theme(
panel.border = element_rect(linewidth = 1),
axis.text = element_text(color = "#000000", size = 15),
axis.title = element_text(color = "#000000", size = 17)
)
ggsave(filename = "Output/p.pdf",
plot = p,
height = 8,
width = 10)
版本信息
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] ggrepel_0.9.6 readxl_1.4.3 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4
[7] purrr_1.0.2 readr_2.1.5 tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.1 tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] gtable_0.3.5 compiler_4.3.0 tidyselect_1.2.1 Rcpp_1.0.13 textshaping_0.3.7
[6] systemfonts_1.1.0 scales_1.3.0 R6_2.5.1 labeling_0.4.3 generics_0.1.3
[11] munsell_0.5.1 pillar_1.9.0 tzdb_0.4.0 rlang_1.1.4 utf8_1.2.4
[16] stringi_1.8.3 timechange_0.2.0 cli_3.6.3 withr_3.0.1 magrittr_2.0.3
[21] grid_4.3.0 rstudioapi_0.15.0 hms_1.1.3 lifecycle_1.0.4 vctrs_0.6.5
[26] glue_1.8.0 farver_2.1.2 cellranger_1.1.0 ragg_1.2.6 fansi_1.0.6
[31] colorspace_2.1-1 tools_4.3.0 pkgconfig_2.0.3
历史绘图合集
公众号推文一览
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。