You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ggplot中对覆盖图的染色体与图例进行数字排序

问题描述

我基于以下数据生成了一张覆盖图:

> head(data.frame(ali))
  qname    qlen  qstart    qend strand         tname    tlen  tstart    tend  nmatch    alen mapq
1     1 2535400   16999 2505278      +  chromosome_1 2608419    3145 2582047 2228136 2615956   60
2     1 2535400 2514043 2521414      + chromosome_23  557589  348928  356301    6955    7373   23
3     1 2535400 2514040 2521414      + chromosome_13  959323  306762  314131    6489    7374    0
4     1 2535400     350    6278      -  chromosome_4 1629129 1048335 1054264    5373    5929    4
5     1 2535400     350    6278      + chromosome_11 1087446 1070616 1076544    5306    5928    0
6     1 2535400     350    6278      +  chromosome_4 1629129  886366  892296    5150    5930    0
> str(data.frame(ali))
'data.frame':   1156 obs. of  12 variables:
 $ qname : chr  "1" "1" "1" "1" ...
 $ qlen  : num  2535400 2535400 2535400 2535400 2535400 ...
 $ qstart: num  16999 2514043 2514040 350 350 ...
 $ qend  : num  2505278 2521414 2521414 6278 6278 ...
 $ strand: chr  "+" "+" "+" "-" ...
 $ tname : chr  "chromosome_1" "chromosome_23" "chromosome_13" "chromosome_4" ...
 $ tlen  : num  2608419 557589 959323 1629129 1087446 ...
 $ tstart: num  3145 348928 306762 1048335 1070616 ...
 $ tend  : num  2582047 356301 314131 1054264 1076544 ...
 $ nmatch: num  2228136 6955 6489 5373 5306 ...
 $ alen  : num  2615956 7373 7374 5929 5928 ...
 $ mapq  : num  60 23 0 4 0 0 22 0 15 0 ...

我希望对图做以下调整:

  • 将左侧的25条chromosome按数字排序,chromosome_1位于顶部,chromosome_25位于底部;
  • 图例中的前33个数字按1到33的顺序排列,后续的bd_##x##、chloroplast等元素顺序不限,只需排在33之后。

当前绘制代码如下:

# 加载包
library(pafr, quietly = TRUE)
library(RColorBrewer)

# 导入比对数据
alignment = system.file("extdata", "oldToNewMap.paf", package="pafr")
ali <- read_paf(alignment)

# 创建颜色调色板
nb.cols <- 89
mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)

# 绘图
plot_coverage(ali, fill='qname') +
  scale_fill_manual(values = mycolors) 
解决方案

可以通过设置因子水平来实现染色体和图例的自定义排序,修改后的完整代码如下:

# 加载所需包
library(pafr, quietly = TRUE)
library(RColorBrewer)
library(stringr)

# 导入比对数据
alignment = system.file("extdata", "oldToNewMap.paf", package="pafr")
ali <- read_paf(alignment)

# --- 1. 自定义染色体排序(左侧y轴) ---
# 生成按数字排序的25条染色体名称列表
sorted_chroms <- paste0("chromosome_", 1:25)
# 提取其他非目标序列名称
other_seqs <- setdiff(unique(ali$tname), sorted_chroms)
# 将tname转为有序因子,先排目标染色体,再排其他序列
ali$tname <- factor(ali$tname, levels = c(sorted_chroms, other_seqs))

# --- 2. 自定义图例qname排序 ---
# 生成1-33的有序qname列表
qname_numeric <- as.character(1:33)
# 提取其余qname项
qname_other <- setdiff(unique(ali$qname), qname_numeric)
# 将qname转为有序因子,先排1-33,再排其余项
ali$qname <- factor(ali$qname, levels = c(qname_numeric, qname_other))

# 创建匹配qname类别的颜色调色板
nb.cols <- length(levels(ali$qname))
mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)

# 绘制覆盖图
plot_coverage(ali, fill='qname') +
  scale_fill_manual(values = mycolors) +
  scale_y_discrete(limits = levels(ali$tname)) # 确保y轴严格按因子顺序排列

关键步骤说明:

  1. 染色体排序:
    手动生成chromosome_1到chromosome_25的有序列表,将其设为tname因子的前序水平,绘图时y轴会直接按这个顺序渲染,chromosome_1自然在顶部,25在底部。

  2. 图例排序:
    把1-33的qname单独列为因子的前序水平,剩余项放在后面,ggplot的图例会跟随因子水平的顺序展示,从而实现数字在前、其他项在后的效果。

  3. 颜色适配:
    将调色板长度改为length(levels(ali$qname)),确保颜色数量和qname的类别数完全匹配,避免颜色缺失或冗余问题。

内容的提问来源于stack exchange,提问作者Mark Pampuch

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 15:02:01