在模仿中精进数据可视化_基于ggplot2仿制一张雷达图
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
论文来源:
论文图片:
图片复现:
❝没有数据,基于功能基因的数据仿制了一张,其实写一写就真的成为雷达图了。
虽然和原始图片相差了一些,但是细节肯定都全了。coord_radial()
函数值得一研究。
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(readxl)
library(ggalt)
加载数据
####----load Data----####
df <- read_xlsx(path = "test_df.xlsx", col_names = T) %>%
dplyr::mutate(Gene = factor(Gene, levels = Gene, ordered = T)) %>%
na.omit() %>%
tidyr::pivot_longer(cols = -Gene, names_to = "Type", values_to = "Value")
可视化
####----Plot----####
p <- ggplot() +
geom_hline(yintercept = c(0, 25, 50, 75), color = "#bdbdbd") +
geom_hline(yintercept = 100, color = "#000000") +
stat_xspline(data = df %>% dplyr::filter(Type == "Type1"),
aes(x = Gene, y = Value, group = 1, fill = Type, color = Type),
geom = "area",
alpha = 0.1, spline_shape=-0.5) +
stat_xspline(data = df %>% dplyr::filter(Type == "Type2"),
aes(x = Gene, y = Value, group = 1, fill = Type, color = Type),
geom = "area",
alpha = 0.1, spline_shape=-0.5) +
stat_xspline(data = df %>% dplyr::filter(Type == "Type3"),
aes(x = Gene, y = Value, group = 1, fill = Type, color = Type),
geom = "area",
alpha = 0.1, spline_shape=-0.5) +
stat_xspline(data = df %>% dplyr::filter(Type == "Type4"),
aes(x = Gene, y = Value, group = 1, fill = Type, color = Type),
geom = "area",
alpha = 0.1, spline_shape=-0.5) +
geom_point(data = df %>% dplyr::filter(Type == "Type1"), aes(x = Gene, y = Value, size = Value, color = Type)) +
geom_point(data = df %>% dplyr::filter(Type == "Type2"), aes(x = Gene, y = Value, size = Value, color = Type)) +
geom_point(data = df %>% dplyr::filter(Type == "Type3"), aes(x = Gene, y = Value, size = Value, color = Type)) +
geom_point(data = df %>% dplyr::filter(Type == "Type4"), aes(x = Gene, y = Value, size = Value, color = Type)) +
scale_size(range = c(2, 8)) +
scale_y_continuous(limits = c(-10, 100), expand = c(0,0)) +
scale_fill_manual(values = c("Type1" = "#1f78b4",
"Type2" = "#33a02c",
"Type3" = "#fb9a99",
"Type4" = "#88419d")) +
scale_color_manual(values = c("Type1" = "#a6cee3",
"Type2" = "#b2df8a",
"Type3" = "#fb9a99",
"Type4" = "#cab2d6")) +
coord_radial(r_axis_inside = TRUE) +
labs(x = "", y = "") +
theme_bw() +
theme(
plot.background = element_blank(),
panel.border = element_blank(),
panel.grid = element_line(linewidth = 1, color = "#f0f0f0"),
axis.line.y.left = element_line(linewidth = 0.5),
axis.ticks.length.theta = unit(10, "pt"),
axis.text = element_text(size = 15),
axis.text.r = element_text(colour = "#ae017e", size = 12),
axis.line.r = element_line(color = "#ae017e")
)
ggsave(filename = "plot.pdf",
plot = p,
height = 8,
width = 9)
版本信息
####----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] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggalt_0.4.0 readxl_1.4.3 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] ash_1.0-15 utf8_1.2.4 generics_0.1.3 proj4_1.0-13 KernSmooth_2.23-22
[6] stringi_1.8.3 extrafontdb_1.0 hms_1.1.3 magrittr_2.0.3 grid_4.3.0
[11] timechange_0.2.0 RColorBrewer_1.1-3 maps_3.4.1.1 cellranger_1.1.0 fansi_1.0.6
[16] scales_1.3.0 textshaping_0.3.7 cli_3.6.3 rlang_1.1.4 crayon_1.5.2
[21] munsell_0.5.1 withr_3.0.1 tools_4.3.0 tzdb_0.4.0 colorspace_2.1-1
[26] vctrs_0.6.5 R6_2.5.1 lifecycle_1.0.4 MASS_7.3-60 ragg_1.2.6
[31] pkgconfig_2.0.3 pillar_1.9.0 gtable_0.3.5 glue_1.7.0 systemfonts_1.1.0
[36] tidyselect_1.2.1 rstudioapi_0.15.0 farver_2.1.2 extrafont_0.19 labeling_0.4.3
[41] Rttf2pt1_1.3.12 compiler_4.3.0
历史绘图合集
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。