在模仿中精进数据可视化_使用R语言绘制微生物功能基因的表达模式
❝最近太忙了,好久没画图了!!!!!!
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
论文来源:
论文图片:
图片复现:
❝我对我自己的评价:勉勉强强吧。
今天先画到这里,其实对于无缝拼图,我之前在公众号里面就讲过,我今天先画到这里,无缝拼图,我挖一个坑在里面。
因为更深入的无缝拼图,需要ggplot2
的深入理解和细节的雕刻。
对于今天的可视化,只能借助AI手动调整了(这不是我本意)。不过有啥说啥,今天的可视化依旧是:细节满满!
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(readxl)
library(grid)
library(aplot)
library(patchwork)
library(cowplot)
library(ggplotify)
library(ggh4x)
加载数据
####----load Data----####
data <- read_xlsx(path = "test_data.xlsx", col_names = T) %>%
dplyr::mutate(id = factor(id, levels = id, ordered = T)) %>%
tidyr::pivot_longer(
cols = c(Growing_FW_mean, `Non-growing_FW_mean`, Growing_BW_mean, `Non-growing_BW_mean`),
names_to = "Condition",
values_to = "Mean"
) %>%
tidyr::pivot_longer(
cols = c(Growing_FW_se, `Non-growing_FW_se`, Growing_BW_se, `Non-growing_BW_se`),
names_to = "Condition_se",
values_to = "se"
) %>%
dplyr::filter(
(Condition == "Growing_FW_mean" & Condition_se == "Growing_FW_se") |
(Condition == "Growing_BW_mean" & Condition_se == "Growing_BW_se")|
(Condition == "Non-growing_FW_mean" & Condition_se == "Non-growing_FW_se")|
(Condition == "Non-growing_BW_mean" & Condition_se == "Non-growing_BW_se")
) %>%
dplyr::select(-Condition_se) %>%
dplyr::mutate(Condition = str_remove(Condition, pattern = "_mean")) %>%
tidyr::separate(col = Condition, sep = "_", into = c("Condition", "Treatment"))
可视化
####----Plot----####
#####-----p1-----#####
p1 <- ggplot(data = data %>% dplyr::filter(Condition == "Growing")) +
annotate(geom = "rect", ymin = 0, ymax = 3.6, xmin = 0, xmax = Inf, fill = "#d9d9d9", alpha = 0.75) +
annotate(geom = "rect", ymin = 4.4, ymax = 27.6, xmin = 0, xmax = Inf, fill = "#d9d9d9", alpha = 0.75) +
annotate(geom = "rect", ymin = 28.4, ymax = 40.6, xmin = 0, xmax = Inf, fill = "#d9d9d9", alpha = 0.75) +
annotate(geom = "rect", ymin = 41.4, ymax = Inf, xmin = 0, xmax = Inf, fill = "#d9d9d9", alpha = 0.75) +
annotate(geom = "rect", ymin = 3.6, ymax = 4.4, xmin = 0, xmax = Inf, fill = "#ffffffff") +
annotate(geom = "rect", ymin = 27.6, ymax = 28.4, xmin = 0, xmax = Inf, fill = "#ffffffff") +
annotate(geom = "rect", ymin = 40.6, ymax = 41.4, xmin = 0, xmax = Inf, fill = "#ffffffff") +
geom_bar(aes(x = Mean, y = id, fill = Treatment), stat = "identity", position = "dodge") +
geom_errorbar(aes(x = Mean, y = id, xmin = Mean - se, xmax = Mean + se, group = Treatment), width = 0.25, position = position_dodge(0.9)) +
geom_text(data = data %>%
dplyr::filter(Condition == "Growing") %>%
dplyr::arrange(Gene, desc(Mean)) %>%
dplyr::distinct(Gene, .keep_all = T),
aes(x = Mean + 0.01, y = id, label = signif)) +
scale_x_continuous(expand = expansion(mult = c(0, 0.1)),
breaks = c(0, 0.05, 0.10),
labels = c("0.00", "0.05", "0.10")) +
scale_fill_manual(values = c("BW" = "#4eb3d3",
"FW" = "#fed976")) +
labs(x = "", y = "") +
theme_bw() +
theme(axis.text.y = element_blank(),
axis.text.x = element_text(color = "#000000", size = 15),
# axis.ticks.length = unit(1.5, "native"),
axis.ticks.y = element_blank(),
panel.border = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(linetype = 2, color = "#000000"),
plot.margin = margin(0, 0, 0, 0, "pt"),
# plot.margin = margin(r = 0, l = 1, t = 0, b = 0, "pt"),
axis.line.y.left = element_line(color = "#000000", linewidth = 0.5),
axis.line.x.bottom = element_line(color = "#000000", linewidth = 0.5),
plot.background = element_blank(),
legend.position = "bottom"
# plot.background = element_rect(fill = "#d9d9d9", color = "#d9d9d9")
)
p1
#####-----p2-----#####
p2 <- ggplot(data = data %>% dplyr::filter(Condition == "Non-growing")) +
annotate(geom = "rect", ymin = 0, ymax = 3.6, xmin = 0, xmax = -Inf, fill = "#d9d9d9", alpha = 0.75) +
annotate(geom = "rect", ymin = 4.4, ymax = 27.6, xmin = 0, xmax = -Inf, fill = "#d9d9d9", alpha = 0.75) +
annotate(geom = "rect", ymin = 28.4, ymax = 40.6, xmin = 0, xmax = -Inf, fill = "#d9d9d9", alpha = 0.75) +
annotate(geom = "rect", ymin = 41.4, ymax = Inf, xmin = 0, xmax = -Inf, fill = "#d9d9d9", alpha = 0.75) +
annotate(geom = "rect", ymin = 3.6, ymax = 4.4, xmin = 0, xmax = -Inf, fill = "#ffffffff") +
annotate(geom = "rect", ymin = 27.6, ymax = 28.4, xmin = 0, xmax = -Inf, fill = "#ffffffff") +
annotate(geom = "rect", ymin = 40.6, ymax = 41.4, xmin = 0, xmax = -Inf, fill = "#ffffffff") +
geom_bar(aes(x = -1*Mean, y = id, fill = Treatment), stat = "identity", position = "dodge") +
geom_errorbar(aes(x = -1*Mean, y = id, xmin = -1*(Mean - se), xmax = -1*(Mean + se), group = Treatment), width = 0.25, position = position_dodge(0.9)) +
geom_text(data = data %>%
dplyr::filter(Condition == "Non-growing") %>%
dplyr::arrange(Gene, desc(Mean)) %>%
dplyr::distinct(Gene, .keep_all = T),
aes(x = -1*(Mean + 0.03), y = id, label = signif)) +
scale_x_continuous(expand = expansion(mult = c(0.1, 0)),
breaks = c(0, -0.05, -0.1),
labels = c("0.00", "0.05", "0.10")) +
scale_fill_manual(values = c("BW" = "#4eb3d3",
"FW" = "#fed976")) +
scale_y_discrete(position = "right") +
labs(x = "", y = "") +
theme_bw() +
theme(axis.text.y = element_blank(),
axis.text.x = element_text(color = "#000000", size = 15),
axis.ticks.y = element_blank(),
panel.border = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(linetype = 2, color = "#000000"),
# plot.margin = margin(t = 0, r = 1, b = 0, l = 0, "pt"),
plot.margin = margin(0, 0, 0, 0, "pt"),
axis.line.y.right = element_line(color = "#000000", linewidth = 0.5),
axis.line.x.bottom = element_line(color = "#000000", linewidth = 0.5),
plot.background = element_blank(),
legend.position = "none"
# plot.background = element_rect(fill = "#d9d9d9", color = "#d9d9d9")
)
p2
#####-----p3-----#####
p3 <- ggplot(data = data %>% dplyr::distinct(id, .keep_all = T)) +
geom_tile(aes(x = 1, y = id), fill = "#d9d9d9", height = 1) +
annotate(geom = "rect", ymin = 0, ymax = 3.6, xmin = -Inf, xmax = Inf, fill = "#d9d9d9", alpha = 1) +
annotate(geom = "rect", ymin = 4.4, ymax = 27.6, xmin = -Inf, xmax = Inf, fill = "#d9d9d9", alpha = 1) +
annotate(geom = "rect", ymin = 28.4, ymax = 40.6, xmin = -Inf, xmax = Inf, fill = "#d9d9d9", alpha = 1) +
annotate(geom = "rect", ymin = 41.4, ymax = Inf, xmin = -Inf, xmax = Inf, fill = "#d9d9d9", alpha = 1) +
annotate(geom = "rect", ymin = 3.6, ymax = 4.4, xmin = -Inf, xmax = Inf, fill = "#ffffffff") +
annotate(geom = "rect", ymin = 27.6, ymax = 28.4, xmin = -Inf, xmax = Inf, fill = "#ffffffff") +
annotate(geom = "rect", ymin = 40.6, ymax = 41.4, xmin = -Inf, xmax = Inf, fill = "#ffffffff") +
geom_text(aes(x = 1, y = id, label = Gene), fontface = "italic") +
scale_x_continuous(limits = c(0.5, 1.5),
expand = expansion(mult = c(0.1, 0.1))) +
guides(y.sec = guide_axis_manual(breaks = 1:44,
labels = 1:44)) +
labs(x = "", y = "") +
theme_bw() +
theme(axis.text = element_blank(),
panel.border = element_blank(),
panel.grid= element_blank(),
plot.margin = margin(0, 0, 0, 0, "pt"),
# plot.background = element_rect(fill = "#d9d9d9", color = "#d9d9d9"),
axis.ticks.length.y = unit(-0.25, "cm"),
axis.ticks.x = element_blank(),
plot.background = element_blank(),
# plot.background = element_rect(fill = "#d9d9d9", color = "#d9d9d9"),
legend.position = "bottom")
p3
#####-----p4-----#####
label_df <- data %>%
dplyr::distinct(id, .keep_all = T) %>%
dplyr::mutate(id2 = as.numeric(id)) %>%
dplyr::group_by(Function) %>%
dplyr::summarize(count = n()) %>%
dplyr::filter(!is.na(Function)) %>%
dplyr::mutate(Function = str_replace_all(Function, "\\\\n", "\n")) %>%
dplyr::mutate(Function = factor(Function,
levels = c("Inorganic P \n solublization", "Organic P \n mineralization",
"P transportation", "P regulation"),
ordered = T)) %>%
dplyr::arrange(Function) %>%
dplyr::mutate(count = count + c(0.5,1,2,0.5)) %>%
dplyr::mutate(cumsum = cumsum(count)) %>%
dplyr::mutate(position = cumsum - 0.5 * count)
p4 <- data %>%
dplyr::distinct(id, .keep_all = T) %>%
ggplot() +
geom_tile(aes(x = 1, y = id, fill = Function), color = "#bdbdbd", height = 1) +
scale_fill_manual(values = rep("#bdbdbd", each = 4)) +
annotate(geom = "rect", ymin = 3.6, ymax = 4.4, xmin = -Inf, xmax = Inf, fill = "#ffffffff") +
annotate(geom = "rect", ymin = 27.6, ymax = 28.4, xmin = -Inf, xmax = Inf, fill = "#ffffffff") +
annotate(geom = "rect", ymin = 40.6, ymax = 41.4, xmin = -Inf, xmax = Inf, fill = "#ffffffff") +
geom_text(data = label_df, aes(x = 1, y = position, label = Function), color = "#fc4e2a", size = 5) +
scale_x_continuous(limits = c(0.5, 1.5),
expand = expansion(mult = c(0.1, 0.1))) +
labs(x = "", y = "") +
theme_bw() +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
panel.grid= element_blank(),
plot.margin = margin(0, 0, 0, 0, "pt"),
# plot.background = element_blank(),
# plot.background = element_rect(fill = "#d9d9d9", color = "#d9d9d9"),
legend.position = "none")
#####-----p5-----#####
p5 <- ggplot(data = data.frame(x = 1:200, y = 1:200, z = "orange")) +
geom_tile(aes(x = x, y = 1, fill = z), height = 1, color = "#ef6548") +
geom_text(aes(x = 20, y = 1, label = "Growing"), size = 8) +
geom_text(aes(x = 100, y = 1, label = "Non-growing"), size = 8) +
geom_text(aes(x = 180, y = 1, label = "Function"), size = 8) +
scale_fill_manual(values = "#ef6548") +
labs(x = "", y = "") +
theme_bw() +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
panel.grid= element_blank(),
# plot.background = element_blank(),
# plot.background = element_rect(fill = "#d9d9d9", color = "#d9d9d9"),
legend.position = "none")
#####-----combine-----#####
p_combine2 <- p2 + p3 + p1 + p4 + plot_layout(nrow = 1, widths = c(3,1,3,3))
# p_combine2
p_combine3 <- p5 / p_combine2 + plot_layout(heights = c(1, 20), widths = c(10, 1))
ggsave(filename = "out.pdf",
plot = p_combine3,
height = 10,
width = 8.5)
版本信息
####----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] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggh4x_0.2.8.9000 ggplotify_0.1.2 ggfortify_0.4.16 cowplot_1.1.3 patchwork_1.2.0.9000
[6] aplot_0.2.3 readxl_1.4.3 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1
[11] dplyr_1.1.4 purrr_1.0.2 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] yulab.utils_0.1.5 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 timechange_0.2.0 pkgload_1.3.3 fastmap_1.2.0 cellranger_1.1.0 gridExtra_2.3
[13] fansi_1.0.6 scales_1.3.0 textshaping_0.3.7 cli_3.6.3 rlang_1.1.4 crayon_1.5.2
[19] munsell_0.5.1 withr_3.0.1 cachem_1.1.0 tools_4.3.0 tzdb_0.4.0 memoise_2.0.1
[25] colorspace_2.1-1 vctrs_0.6.5 R6_2.5.1 gridGraphics_0.5-1 lifecycle_1.0.4 fs_1.6.4
[31] ggfun_0.1.5 ragg_1.2.6 pkgconfig_2.0.3 pillar_1.9.0 gtable_0.3.5 glue_1.7.0
[37] systemfonts_1.1.0 tidyselect_1.2.1 rstudioapi_0.15.0 farver_2.1.2 labeling_0.4.3 compiler_4.3.0
历史绘图合集
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。