在模仿中精进数据可视化_使用R语言绘制小提琴+显著性可视化
❝希望今天顺利且成功!
同时也祝大家中秋节快乐!
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
论文来源:
论文图片:
图片复现:
❝今天的可视化依旧是细节满满!
依旧是稳稳拿捏!
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(readxl)
library(writexl)
library(ggimage)
library(ggbeeswarm)
library(grid)
加载数据
####----load Data----####
data <- read_xlsx(path = "data.xlsx") %>%
dplyr::mutate(out = factor(out,
levels = c("Non-Sphagnum_Waterlogged",
"Non-Sphagnum_Drained",
"Sphagnum_Waterlogged",
"Sphagnum_Drained"),
ordered = T))
stat <- read_xlsx(path = "stat.xlsx") %>%
dplyr::mutate(out = factor(out,
levels = c("Non-Sphagnum_Waterlogged",
"Non-Sphagnum_Drained",
"Sphagnum_Waterlogged",
"Sphagnum_Drained"),
ordered = T))
img1 <- "Plant1.jpg"
img2 <- "Plant2.jpg"
可视化
####----plot----####
p <- ggplot(data = data, aes(x = out, y = SOC)) +
geom_violin(aes(color = Type), width = 0.75, key_glyph = "boxplot") +
stat_summary(
fun = "mean",
geom = "crossbar",
width = 0.25,
color = "#000000"
) +
geom_beeswarm(aes(color = Type), cex = 1.75, show.legend = F) +
geom_vline(xintercept = 2.5, linetype = 5) +
geom_image(aes(0.75, 350), image = img1, size = .1) +
geom_image(aes(4.35, 350), image = img2, size = .1) +
geom_text(data = stat, aes(x = out, y = 425, label = stat), size = 5) +
scale_color_manual(
name = "",
values = c(
"Drained" = "#74add1",
"Waterlogged" = "#f46d43"
)
) +
scale_y_continuous(
name = expression("SOC (mg g"^-1 *")"),
limits = c(0, 450),
breaks = c(0, 100, 200, 300, 400),
labels = c(0, "", 200, "", 400),
expand = expansion(mult = c(0, 0.1))
) +
scale_x_discrete(
name = "",
labels = c("(70)", "(70)", "(56)", "(56)")
) +
guides(color = guide_legend(nrow = 1,
override.aes = list(size = 8))
) +
theme_bw() +
theme(
panel.grid = element_blank(),
panel.border = element_rect(linewidth = 1),
axis.text.y = element_text(size = 20, color = "#000000"),
axis.text.x = element_text(color = c("#238443", "#238443",
"#ec7014", "#ec7014"),
size = 20),
axis.title = element_text(size = 20),
axis.ticks.length.y.left = unit(8, "pt"),
axis.ticks.length.x = unit(8, "pt"),
plot.margin = margin(t = 1, r = 1, b = 1, l = 1, unit = "cm"),
legend.position = "top"
) +
coord_cartesian(clip = "off") +
annotation_custom(grob = textGrob(label = "Non-Sphagnum",
gp = gpar(col = "#238443",
cex = 1.5,
fontface = "italic")),
xmin = unit(1.5, "native"),
xmax = unit(1.5, "native"),
ymin = unit(-65, "native"),
ymax = unit(-65, "native")) +
annotation_custom(grob = textGrob(label = "Sphagnum",
gp = gpar(col = "#ec7014",
cex = 1.5,
fontface = "italic")),
xmin = unit(3.5, "native"),
xmax = unit(3.5, "native"),
ymin = unit(-65, "native"),
ymax = unit(-65, "native"))
p
ggsave(filename = "out.pdf",
plot = p,
height = 6,
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] ggbeeswarm_0.7.2 ggimage_0.3.3 writexl_1.4.2 readxl_1.4.3
[5] lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4
[9] purrr_1.0.2 readr_2.1.5 tidyr_1.3.1 tibble_3.2.1
[13] 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 ggplotify_0.1.2
[5] stringi_1.8.3 hms_1.1.3 digest_0.6.36 magrittr_2.0.3
[9] timechange_0.2.0 fastmap_1.2.0 cellranger_1.1.0 jsonlite_1.8.7
[13] fansi_1.0.6 scales_1.3.0 textshaping_0.3.7 cli_3.6.3
[17] rlang_1.1.4 munsell_0.5.1 withr_3.0.1 cachem_1.1.0
[21] tools_4.3.0 tzdb_0.4.0 memoise_2.0.1 colorspace_2.1-1
[25] vctrs_0.6.5 R6_2.5.1 gridGraphics_0.5-1 lifecycle_1.0.4
[29] magick_2.8.1 fs_1.6.4 ggfun_0.1.5 vipor_0.4.5
[33] ragg_1.2.6 beeswarm_0.4.0 pkgconfig_2.0.3 pillar_1.9.0
[37] gtable_0.3.5 Rcpp_1.0.11 glue_1.7.0 systemfonts_1.0.5
[41] tidyselect_1.2.1 rstudioapi_0.15.0 farver_2.1.2 compiler_4.3.0
历史绘图合集
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。