在模仿中精进数据可视化_使用R语言绘制对称的柱形图
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
论文出处:
❝我的好师兄,好朋友叶哥给我推荐了一个文献,我一看,好家伙,全篇好图。值得学习一下。
论文原图:
图片复现:
❝自我点评一下,其实没啥难度,对我来说,画画图,暖暖手。
不过实质上,绘图代码里面还是有非常多的细节。
画这种图最重要的是绘图思路,如果想画的再好一些,完全脱离AI
修图,那么就需要进行一些无缝拼图的技巧。
在这里我就要祭出我之前写的无缝拼图的教程,大家可以参考参考。
往期链接:
在模仿中精进数据可视化_R语言实现Nature Microbiology中的无缝拼图
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(readxl)
library(ggh4x)
library(patchwork)
加载数据
####----load Data----####
df <- read_xlsx(path = "test.xlsx", col_names = T) %>%
dplyr::mutate(Gene = factor(Gene, levels = Gene, ordered = T))
可视化
####----Plot----####
p1 <- ggplot(data = df %>% dplyr::select(1, 2)) +
geom_tile(aes(x = "a", y = Gene, fill = Sample), width = 0.5) +
labs(x = "", y = "") +
scale_fill_manual(values = c("#fbb4ae", "#b3cde3", "#ccebc5", "#decbe4", "#ffffcc", "#cab2d6")) +
theme_bw() +
theme(
panel.grid = element_blank(),
panel.border = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(face = "italic", hjust = 1),
legend.position = "none"
)
p1
p2 <- df %>%
dplyr::select(1,3,4) %>%
tidyr::pivot_longer(cols = -Gene, names_to = "Type", values_to = "Value") %>%
ggplot() +
geom_bar(aes(x = Value, y = Gene, fill = Type), stat = "identity", width = 0.6) +
scale_x_continuous(expand = expansion(mult = c(0, 0.1))) +
scale_fill_manual(values = c("#ef6548", "#ffeda0")) +
labs(x = "", y = "") +
guides(y.sec = guide_axis_manual(breaks = 1:36)) +
theme_bw() +
theme(
panel.grid = element_blank(),
panel.border = element_blank(),
axis.line.x.bottom = element_line(linewidth = 0.5),
axis.line.y.right = element_line(linewidth = 0.5, linetype = 2),
axis.ticks.y = element_blank(),
axis.ticks.length.x = unit(5, "pt"),
axis.text.y = element_blank(),
legend.position = "top"
)
p2
p3 <- ggplot(data = df) +
geom_bar(aes(x = -1*Others, y = Gene, fill = "a"), stat = "identity", width = 0.6) +
scale_x_continuous(expand = expansion(mult = c(0, 0)),
breaks = c(-50, -40, -30, -20, -10, 0),
labels = c(50, 40, 30, 20, 10, 0)) +
scale_fill_manual(values = "#78c679") +
labs(x = "", y = "") +
theme_bw() +
theme(
panel.grid = element_blank(),
panel.border = element_blank(),
axis.line.x.bottom = element_line(linewidth = 0.5),
axis.ticks.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.length.x = unit(5, "pt"),
legend.position = "top"
)
p3
p4 <- ggplot(data = df %>% dplyr::select(1, 2)) +
geom_tile(aes(x = "a", y = Gene, fill = Sample), width = 0.5) +
labs(x = "", y = "") +
scale_fill_manual(values = c("#fbb4ae", "#b3cde3", "#ccebc5", "#decbe4", "#ffffcc", "#cab2d6")) +
guides(y.sec = guide_axis_manual(breaks = 1:36, labels = df$Gene)) +
theme_bw() +
theme(
panel.grid = element_blank(),
panel.border = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank(),
axis.text.y.left = element_blank(),
axis.text.y = element_text(face = "italic", hjust = 1),
legend.position = "top"
)
p4
# combine
p_combine <- p1 + p2 + p3 + p4 + plot_layout(nrow = 1,
widths = c(0.25, 1.25, 1.25, 0.25))
ggsave(filename = "p.pdf",
plot = p_combine,
height = 9,
width = 10)
版本信息
####----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] patchwork_1.2.0.9000 ggh4x_0.2.8.9000 cowplot_1.1.3 readxl_1.4.3 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1
[8] dplyr_1.1.4 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 crayon_1.5.2 compiler_4.3.0 tidyselect_1.2.1 textshaping_0.3.7 systemfonts_1.1.0 scales_1.3.0 R6_2.5.1 labeling_0.4.3
[10] generics_0.1.3 munsell_0.5.1 pillar_1.9.0 tzdb_0.4.0 rlang_1.1.4 utf8_1.2.4 stringi_1.8.3 pkgload_1.3.3 timechange_0.2.0
[19] cli_3.6.3 withr_3.0.1 magrittr_2.0.3 rstudioapi_0.15.0 hms_1.1.3 lifecycle_1.0.4 vctrs_0.6.5 writexl_1.4.2 glue_1.7.0
[28] farver_2.1.2 cellranger_1.1.0 ragg_1.2.6 fansi_1.0.6 colorspace_2.1-1 tools_4.3.0 pkgconfig_2.0.3
历史绘图合集
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。