在模仿中精进数据可视化_使用R语言模仿表格以及组合图
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
❝今天提供素材的依旧是叶大师兄!
不过天知道,他咋总看预印版的论文。
可能就这种大佬才会如此跟进文献吧。
论文原图
图片复现:
❝细节自己调整吧,绘图代码依旧全是细节。
其实本身可以直接绘制表格的,但是我就想手搓一个!
对齐文字的确是一个有趣的地方,
除此之外,各位粉丝朋友还能看出有多少细节呢?
私聊我,如果我觉得你答对了,我就给你源代码和测试数据。
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(grid)
library(patchwork)
library(ggh4x)
加载数据
####----load Data----####
df <- read_delim(file = "Input/test_df.txt", col_names = T, delim = "\t")
id = df$ID
label = colnames(df)[1:3]
df2 <- read_delim(file = "Input/test_df2.txt", col_names = T, delim = "\t")
绘图
####----Plot1----####
p <- df %>%
tidyr::pivot_longer(cols = -ID, names_to = "Type", values_to = "Value") %>%
dplyr::mutate(
ID = factor(ID, levels = rev(id), ordered = T),
Type = factor(Type, levels = c("Genotype", "Tissus", "BioSample_Acc."), ordered = T),
Value = str_to_upper(str_pad(Value, width = max(str_length(Value)), side = "right"))
) %>%
ggplot() +
geom_tile(aes(x = Type, y = ID), fill = "#ffffff", color = "#000000", linewidth = 0.25) +
geom_text(aes(x = Type, y = ID, label = Value), hjust = "left", size = 3, color = "#000000") +
guides(x = "axis_nested",
x.sec = guide_axis_manual(breaks = 1:3, labels = label)) +
labs(x = "", y = "") +
theme_bw() +
theme(
axis.text.y = element_blank(),
axis.text.x.bottom = element_blank(),
axis.text.x.top = element_text(hjust = 0, color = "#000000", size = 10),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank(),
plot.margin=unit(c(1,1,1,1),unit="cm")
) +
coord_cartesian(clip = "off")
p
for (i in c(0:3)) {
if (i < 3) {
p <- p + annotation_custom(
grob = linesGrob(gp = gpar(lwd = 1, col = "#000000")),
xmin = i + 0.5, xmax = i + 0.5, ymin = 0.5, ymax = 9
)
}else{
p <- p + annotation_custom(
grob = linesGrob(gp = gpar(lwd = 2, col = "#ffffff")),
xmin = i + 0.5, xmax = i + 0.5, ymin = 0.5, ymax = 9
)
}
}
p
for (j in c(0:7)) {
if (j <= 4) {
p <- p + annotation_custom(
grob = rectGrob(x = unit(0.5, "native"), y = unit(0.5, "native"),
width = unit(0.5, "native"), height = unit(0.5, "native"),
gp = gpar(col = "#000000", fill = "#d6604d", lwd = 2, alpha = 0.75)),
xmin = 0.25, xmax = 0.5, ymin = j + 0.65, ymax = j + 1.35
)
}else{
p <- p + annotation_custom(
grob = rectGrob(x = unit(0.5, "native"), y = unit(0.5, "native"),
width = unit(0.5, "native"), height = unit(0.5, "native"),
gp = gpar(col = "#000000", fill = "#43a2ca", lwd = 2, alpha = 0.75)),
xmin = 0.25, xmax = 0.5, ymin = j + 0.65, ymax = j + 1.35
)
}
}
p
####----Plot2----####
p2 <- df2 %>%
dplyr::mutate(ID = factor(ID, levels = rev(id), ordered = T)) %>%
ggplot() +
geom_bar(aes(x = Number, y = ID), stat = "identity", fill = "#feb24c") +
geom_text(aes(x = Number + 2, y = ID, label = str_c(Number, "%")), size = 5) +
scale_x_continuous(expand = expansion(mult = c(0, 0.1)),
sec.axis = sec_axis(~.)) +
ggtitle(label = "Percentage of SRAV viRNAs") +
labs(x = "", y = "") +
theme_classic() +
theme(
axis.line.x.bottom = element_blank(),
axis.text.x.bottom = element_blank(),
axis.text.x.top = element_text(size = 15),
axis.ticks.x.bottom = element_blank(),
axis.text.y = element_blank(),
axis.ticks.length.y = unit(8, "pt"),
plot.title = element_text(hjust = 0.5, size = 17.5),
plot.margin=unit(c(1,1,1,0), unit="cm")
) +
coord_cartesian(clip = "off") +
annotation_custom(
grob = rectGrob(x = unit(0.5, "native"), y = unit(0.5, "native"),
width = unit(0.5, "native"), height = unit(0.5, "native"),
gp = gpar(col = "#000000", fill = "#fb6a4a", lwd = 2)),
xmin = 5, xmax = 8, ymin = 2.65, ymax = 3.65
) +
annotation_custom(
grob = textGrob(label = "Confident",
gp = gpar(col = "#000000")),
xmin = 9.5, xmax = 9.5, ymin = 2.65, ymax = 3.65
) +
annotation_custom(
grob = rectGrob(x = unit(0.5, "native"), y = unit(0.5, "native"),
width = unit(0.5, "native"), height = unit(0.5, "native"),
gp = gpar(col = "#000000", fill = "#74a9cf", lwd = 2)),
xmin = 5, xmax = 8, ymin = 1.5, ymax = 2.5
) +
annotation_custom(
grob = textGrob(label = "Unconfident",
gp = gpar(col = "#000000")),
xmin = 10, xmax = 10, ymin = 1.5, ymax = 2.5
)
p2
####----combine----####
p_combine <- p + p2 + plot_layout(widths = c(2.5, 2))
p_combine
ggsave(filename = "Output/plot.pdf",
plot = p_combine,
height = 7.5,
width = 13)
版本信息
####----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] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggh4x_0.2.8.9000 patchwork_1.2.0.9000 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] bit_4.0.5 gtable_0.3.5 compiler_4.3.0 crayon_1.5.2 tidyselect_1.2.1 parallel_4.3.0 textshaping_0.3.7 systemfonts_1.1.0
[9] scales_1.3.0 R6_2.5.1 labeling_0.4.3 generics_0.1.3 munsell_0.5.1 pillar_1.9.0 tzdb_0.4.0 rlang_1.1.4
[17] utf8_1.2.4 stringi_1.8.3 bit64_4.0.5 timechange_0.2.0 cli_3.6.3 withr_3.0.1 magrittr_2.0.3 vroom_1.6.4
[25] rstudioapi_0.15.0 hms_1.1.3 lifecycle_1.0.4 vctrs_0.6.5 glue_1.7.0 farver_2.1.2 ragg_1.2.6 fansi_1.0.6
[33] colorspace_2.1-1 tools_4.3.0 pkgconfig_2.0.3
历史绘图合集
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。