在模仿中精进数据可视化_再次使用circlize绘制GO/KEGG的富集分析
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
❝今天继续使用
circlize
进行GO/KEGG
富集分析的可视化。
主要将对标的是GoPlot
包绘制的结果。
❝
circlize
版本
❝哪一个好看,大家自行评价吧。
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(readxl)
library(circlize)
library(ComplexHeatmap)
加载数据
####----load Data----####
GO_df <- read_xlsx(path = "Input/GO.xlsx", col_names = T)
GO_ID <- GO_df %>%
dplyr::select(GO,ID,FDR) %>%
tidyr::separate_rows(ID, sep = "/")
DEG <- read_xlsx(path = "Input/DEG.xlsx", col_names = T)
plot_df <- GO_ID %>%
dplyr::left_join(DEG, by = c("ID" = "ID")) %>%
dplyr::mutate(`-log10(FDR)` = -log10(FDR))
normalize <- function(x, new_min = 1, new_max = 2) {
(x - min(x)) / (max(x) - min(x)) * (new_max - new_min) + new_min
}
GO_ID <- plot_df %>%
dplyr::select(GO) %>%
dplyr::distinct(GO) %>%
dplyr::rename(Chr = GO) %>%
dplyr::mutate(Start = 1,
End = 3)
Gene_df <- plot_df %>%
dplyr::select(GO, ID,log2FoldChange, Change) %>%
dplyr::group_by(GO) %>%
dplyr::mutate(location = 1:n()) %>%
dplyr::mutate(location = normalize(location,1.1,2.9)) %>%
dplyr::ungroup() %>%
dplyr::rename(Chr = GO,
Start = location) %>%
dplyr::mutate(End = Start) %>%
dplyr::select(1,5,6,3)
Gene_df2 <- plot_df %>%
dplyr::select(GO, `-log10(FDR)`) %>%
dplyr::distinct(GO, .keep_all = T) %>%
dplyr::mutate(Start = 1.5,
End = 2.5) %>%
dplyr::select(1,3,4,2)
绘图
####----Plot----####
pdf(file = "./Output/Output.pdf",
height = 8,
width = 9)
circos.par("start.degree" = 180)
circos.genomicInitialize(GO_ID,
plotType = c("labels"),
axis.labels.cex = 0.8*par("cex"),
labels.cex = 1*par("cex"),
track.height = 0.05
)
circos.genomicTrackPlotRegion(
GO_ID,
track.height = 0.1,
stack = TRUE,
bg.border = NA,
track.margin = c(0, 0),
panel.fun = function(region, value, ...) {
circos.genomicRect(region, value, col = "#a8ddb5", border = "black", ...)
} )
circos.genomicTrack(
Gene_df,
track.height = 0.25,
bg.col = "#f0f0f0",
bg.border = NA,
panel.fun = function(region, value, ...){
for (i in c(-4,-2,0,2,4)) {
circos.lines(seq(1,3,0.05),
rep(i, 41),
col = "#000000",
lwd = 0.15,
lty = 2)
}
circos.yaxis(labels.cex = 0.5,
lwd = 0.1,
tick.length = convert_x(0.2, "mm"))
circos.genomicPoints(
region,
value,
col = "#000000",
pch = 21,
cex = 1,
bg = ifelse(value > 0, "#fa9fb5", "#2ca25f"))
}
)
col_fun2 = colorRamp2(breaks = c(0, 7.5, 15), colors =c("#fde0dd","#fa9fb5","#c51b8a"))
circos.genomicTrack(
Gene_df2,
track.height = 0.35,
bg.col = NA,
bg.border = NA,
panel.fun = function(region, value, ...) {
sector.name = get.cell.meta.data("sector.index")
circos.genomicRect(region, value,
col = col_fun2(value[[1]]),
border = NA,
ytop.column = 1,
ybottom = 0,
...)
}
)
legend1 <- Legend(
at = c(1, 2),
labels = c("Up regulated","Down regulated"),
title = "log2(FoldChange)",
type = "points", pch = NA,
background = c("#fa9fb5", "#2ca25f"))
legend2 <- Legend(
col_fun = col_fun2,
title = "-log10(Pvalue)",
direction = "horizontal")
pushViewport(viewport(x = 0.1, y = 0.2))
grid.draw(legend1)
y_coord <- 0.2
upViewport()
pushViewport(viewport(x = 0.9, y = 0.2))
grid.draw(legend2)
y_coord <- 0.2
upViewport()
circos.clear()
dev.off()
版本信息
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] grid stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] ComplexHeatmap_2.18.0 circlize_0.4.15 readxl_1.4.3
[4] lubridate_1.9.3 forcats_1.0.0 stringr_1.5.1
[7] dplyr_1.1.4 purrr_1.0.2 readr_2.1.5
[10] tidyr_1.3.1 tibble_3.2.1 ggplot2_3.5.1
[13] tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] utf8_1.2.4 generics_0.1.3 shape_1.4.6
[4] stringi_1.8.3 hms_1.1.3 digest_0.6.37
[7] magrittr_2.0.3 RColorBrewer_1.1-3 timechange_0.2.0
[10] iterators_1.0.14 cellranger_1.1.0 foreach_1.5.2
[13] doParallel_1.0.17 GlobalOptions_0.1.2 fansi_1.0.6
[16] scales_1.3.0 codetools_0.2-19 cli_3.6.3
[19] rlang_1.1.4 crayon_1.5.2 munsell_0.5.1
[22] withr_3.0.1 tools_4.3.0 parallel_4.3.0
[25] tzdb_0.4.0 colorspace_2.1-1 BiocGenerics_0.48.1
[28] GetoptLong_1.0.5 vctrs_0.6.5 R6_2.5.1
[31] png_0.1-8 stats4_4.3.0 matrixStats_1.1.0
[34] lifecycle_1.0.4 S4Vectors_0.40.2 IRanges_2.36.0
[37] clue_0.3-65 cluster_2.1.6 pkgconfig_2.0.3
[40] pillar_1.9.0 gtable_0.3.5 glue_1.8.0
[43] tidyselect_1.2.1 rstudioapi_0.15.0 rjson_0.2.21
[46] compiler_4.3.0
历史绘图合集
公众号推文一览
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。