在模仿中精进数据可视化_ggplot2绘制多变量的柱形图
❝首先,先庆祝自己生日快乐~。
祝爱我的人和我爱的人,一切都好,一切顺利。
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
❝今天复现好朋友提供的一张图
原图
复现
❝测试数据都是编造的,没有任何实际意义。
怎么讲呢,属于是ggplot2
的较好的使用了。如果有机会,在评论区留言一下,说一说一打眼,这张图有什么细节。
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(readxl)
library(ggnewscale)
加载数据
####----load Data----####
writexl::write_xlsx(df2, path = "Input/test.xlsx")
df2 <- read_xlsx(path = "Input/test.xlsx")
df2_A <- df2 %>%
dplyr::select(1,2,3) %>%
tidyr::pivot_longer(cols = -Group, values_to = "Value", names_to = "Kind") %>%
dplyr::mutate(Group_new = "GroupA") %>%
dplyr::mutate(id = rep(seq(1,16, 2), each = 2))
df2_B <- df2 %>%
dplyr::select(1,4,5) %>%
tidyr::pivot_longer(cols = -Group, values_to = "Value", names_to = "Kind") %>%
dplyr::mutate(Group_new = "GroupB") %>%
dplyr::mutate(id = rep(seq(1,16, 2), each = 2))
df2_C <- df2 %>%
dplyr::select(1,6,7) %>%
tidyr::pivot_longer(cols = -Group, values_to = "Value", names_to = "Kind") %>%
dplyr::mutate(GroupC = "GroupC") %>%
dplyr::mutate(id = rep(seq(1,16, 2), each = 2))
绘图
####----Plot----####
p <- ggplot() +
geom_bar(data = df2_A, aes(x = id + 0.3, y = Value, fill = Kind), stat = "identity", width = 0.5, color = "#31a354", size = 1.25) +
geom_text(data = df2_A %>% dplyr::filter(Kind == "A"),
aes(x = id + 0.3, y = Value + 1, label = abs(Value))) +
geom_text(data = df2_A %>% dplyr::filter(Kind == "A_1"),
aes(x = id + 0.3, y = Value + 0.5, label = abs(Value))) +
scale_fill_manual(values = c("#a1d99b", "#31a354")) +
new_scale_fill() +
geom_bar(data = df2_B, aes(x = id-0.3, y = Value, fill = Kind), stat = "identity", width = 0.5, color = "#d95f0e", size = 1.25) +
geom_text(data = df2_B %>% dplyr::filter(Kind == "B"),
aes(x = id - 0.3, y = Value + 0.5, label = abs(Value))) +
geom_text(data = df2_B %>% dplyr::filter(Kind == "B_1"),
aes(x = id - 0.3, y = Value + 0.5, label = abs(Value))) +
scale_fill_manual(values = c("#fec44f", "#d95f0e")) +
new_scale_fill() +
geom_bar(data = df2_C, aes(x = id, y = Value, fill = Kind), stat = "identity", width = 0.5, color = "#756bb1", size = 1.25) +
geom_text(data = df2_C %>% dplyr::filter(Kind == "C"),
aes(x = id, y = Value + 0.5, label = abs(Value))) +
geom_text(data = df2_C %>% dplyr::filter(Kind == "C_1"),
aes(x = id, y = Value - 0.5, label = abs(Value))) +
scale_fill_manual(values = c("#bcbddc", "#756bb1")) +
geom_hline(yintercept = 0, linewidth = 0.8) +
labs(x = "", y = "") +
scale_y_continuous(limits = c(-25, 25)) +
coord_flip() +
theme_bw() +
theme(
panel.border = element_blank(),
panel.grid = element_blank(),
axis.line.x = element_line(color = "#000000", linewidth = 0.8),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.margin = margin(10,10,10,10,"pt")
)
ggsave(filename = "Output/p.pdf",
plot = p,
height = 8,
width = 7)
版本信息
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] ggnewscale_0.5.0 readxl_1.4.3 lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2 readr_2.1.5
[9] 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
[8] 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
[15] utf8_1.2.4 stringi_1.8.3 timechange_0.2.0 cli_3.6.3 withr_3.0.1 magrittr_2.0.3 grid_4.3.0
[22] rstudioapi_0.15.0 hms_1.1.3 lifecycle_1.0.4 vctrs_0.6.5 writexl_1.4.2 glue_1.8.0 farver_2.1.2
[29] 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版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。