在模仿中精进数据可视化_再次使用circlize绘制circos圈图
❝持续学习
❝
在模仿中精进数据可视化
该系列推文中,我们将从各大顶级学术期刊的Figure
入手,
解读文章的绘图思路,
模仿文章的作图风格,
构建适宜的绘图数据,
并且将代码应用到自己的实际论文中。
绘图缘由:小伙伴们总会展示出一些非常好看且精美的图片。我大概率会去学习和复现一下。其实每个人的时间和精力都非常有限和异常宝贵的。之所以我会去做,主要有以下原因:
图片非常好看,我自己看着也手痒痒 图片我自己在Paper也用的上,储备着留着用 保持了持续学习的状态
❝今天又是好朋友提供的素材
原文
原图
❝其实图片是一眼就知道是
circlize
,但是其实这里面有一些很细节:
首先:最外面的圈,是GO的注释 其次:第二圈是pvalue 最里面是一个数值变量,但是注意这里面有正有负,这个是这个图的重点。
图片复现
❝基本上拿捏了。
直接上代码:
加载R
包
rm(list = ls())
####----load R Package----####
library(tidyverse)
library(circlize)
library(ComplexHeatmap)
library(readxl)
加载数据
####----load Data----####
df <- read_xlsx(path = "Input/Test.xlsx", col_names = T, sheet = 1) %>%
dplyr::mutate(start = 0,
end = 200)
绘图
####----Plot----####
pdf(file = "./Output/demo.pdf",
height = 10.5,
width = 10.5)
# 开始画图
circos.par("start.degree" = 90,
"track.margin" = c(0.02, 0.02),
"cell.padding" = c(0.02, 0.02, 0, 0))
df1 <- df %>%
dplyr::select(1, 5, 6, 2) %>%
dplyr::rename(ID = GO)
feature_color <- c("#a6cee3","#1f78b4","#b2df8a","#33a02c","#fb9a99",
"#e31a1c","#fdbf6f","#ff7f00","#cab2d6","#6a3d9a",
"#ffff99","#b15928","#ccebc5", "#fccde5")
# 第一圈 feature and ID
circos.initializeWithIdeogram(df1, plotType = NULL)
circos.track(
ylim = c(0, 1),
track.height = 0.05, # 轨道高度
bg.border = NA, # 不要边框
bg.col = feature_color, # 添加颜色
panel.fun = function(x, y) {
ylim = get.cell.meta.data("ycenter")
xlim = get.cell.meta.data("xcenter")
sector.name = get.cell.meta.data("sector.index") # 提取 GO Term
track.index = get.current.track.index()
description = df1[df1$ID == sector.name,]$Description
# circos.axis(h = "top", labels.cex = 0.7, major.tick.percentage = 0.4, labels.niceFacing = FALSE) # 刻度线
circos.text(xlim, ylim + 2, description, cex = 0.75, niceFacing = TRUE, facing = "bending.inside") # 添加 GO Term
} )
# 第二圈
df2 <- df %>%
dplyr::select(1, 5, 6, 4) %>%
dplyr::rename(ID = GO) %>%
dplyr::mutate(`-log10pvalue` = -log10(pval)) %>%
dplyr::select(1,2,3,5)
summary(df2$`-log10pvalue`)
col_fun1 = colorRamp2(breaks = c(1, 2, 4, 6, 8, 10), colors =c("#bfd3e6","#9ebcda","#8c96c6","#8c6bb1","#88419d","#810f7c"))
circos.genomicTrackPlotRegion(
df2,
track.height = 0.08,
bg.border = NA,
stack = TRUE,
panel.fun = function(region, value, ...) {
circos.genomicRect(
region,
value,
col = col_fun1(value[[1]]),
border = NA, ...
)
ylim = get.cell.meta.data("ycenter")
xlim = get.cell.meta.data("xcenter")
sector.name = get.cell.meta.data("sector.index")
circos.text(xlim, ylim + 0.8, sector.name, cex = 0.8, niceFacing = FALSE) # 添加 GO Term
} )
# 第三圈
df3 <- df %>%
dplyr::select(1, 5, 6, 3) %>%
dplyr::rename(ID = GO)
col_fun2 = colorRamp2(breaks = c(-2, 0, 1), colors =c("#f768a1","#ffffff","#4292c6"))
summary(df3$Value)
df3_up <- df3 %>% dplyr::filter(Value > 0)
df3_down <- df3 %>% dplyr::filter(Value < 0)
circos.genomicTrack(
df3_up,
ylim = c(0.05, 1),
track.height = 0.45,
bg.col = "#f0f0f0",
bg.border = NA,
track.margin = c(0, 0),
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,
...)
} )
circos.genomicTrack(
df3_down,
ylim = c(-1.5, 0),
track.height = 0.25,
bg.col = NA,
bg.border = NA,
track.margin = c(0, 0),
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 = 0,
ytop.column = 0,
ybottom = -1.5,
ybottom.column = -1.5,
...)
} )
circos.clear()
# 添加图例
pvalue_legend <- Legend(
title = "-log10(pvalue)",
labels = rev(c(1, 2, 4, 6, 8, 10)),
type = "points", pch = NA,
background = rev(c("#bfd3e6","#9ebcda","#8c96c6","#8c6bb1","#88419d","#810f7c")),
labels_gp = gpar(fontsize = 8), grid_height = unit(0.5, "cm"), grid_width = unit(0.5, "cm"))
legend_list <- lgd_list_vertical <- packLegend(pvalue_legend)
pushViewport(viewport(x = 0.9, y = 0.9))
grid.draw(lgd_list_vertical)
upViewport()
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 base
other attached packages:
[1] readxl_1.4.3 ComplexHeatmap_2.18.0 circlize_0.4.15 lubridate_1.9.3
[5] forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2
[9] readr_2.1.5 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 stringi_1.8.3
[5] hms_1.1.3 digest_0.6.37 magrittr_2.0.3 RColorBrewer_1.1-3
[9] timechange_0.2.0 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 scales_1.3.0
[17] codetools_0.2-19 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 parallel_4.3.0
[25] tzdb_0.4.0 colorspace_2.1-1 GetoptLong_1.0.5 BiocGenerics_0.48.1
[29] vctrs_0.6.5 R6_2.5.1 png_0.1-8 stats4_4.3.0
[33] matrixStats_1.1.0 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 pillar_1.9.0
[41] gtable_0.3.5 glue_1.8.0 tidyselect_1.2.1 rstudioapi_0.15.0
[45] rjson_0.2.21 compiler_4.3.0
历史绘图合集
公众号推文一览
进化树合集
环状图
散点图
基因家族合集
换一个排布方式:
首先查看基础版热图:
然后再看进阶版热图:
基因组共线性
WGCNA ggplot2版本
其他科研绘图
合作、联系和交流
有很多小伙伴在后台私信作者,非常抱歉,我经常看不到导致错过,请添加下面的微信联系作者,一起交流数据分析和可视化。