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

ggplot轴刻度标签位置调整及矩形转箭头图形需求

环形基因组图问题解决方案

原始数据集

filld <- structure(list(start = c(1, 899, 1648, 2024, 4101, 6001, 7124, 8982), end = c(898, 1647, 2023, 4100, 6000, 7123, 8981, 10000), gene = c("A", "B", "C", "X", "P", "N", "O", "F"), annotation = c("Replicase", "transcription", "Metabolic Pathways", "Metabolic Pathways", "transcription", "transcription", "Antibiotic Resistance", "transcription"), direction = c(1, 1, 1, -1, 1, -1, 1, 1), p = c(449.5, 1273, 1835.5, 3062, 5050.5, 6562, 8052.5, 9491)), row.names = c(NA, -8L), class = "data.frame")

原始绘图代码

# Mean
filld$p <- rowMeans(subset(filld, select = c(start, end)))

# Plot
ggplot(filld, aes(xmin = start, xmax = end, ymin = 4, ymax = 5, fill = annotation)) + # 
  geom_rect() +
  scale_x_continuous(label = label_bytes("kB"), breaks = scales::pretty_breaks(n = 10)) +
  geom_segment(data = subset(filld, gene %in% gene[duplicated(gene)]),
               aes(x = p, y = 0, xend = p, yend = 4, colour = annotation),
               size = 2) +
  geom_text_repel(aes(x = p, y = 4.5,
                     label = gene, 
                     family = 'Roboto Condensed',
                  ),  nudge_x = .15,
                  box.padding = 0.5,
                  nudge_y = 1,
                  segment.curvature = -0.1,
                  segment.ncp = 3,
                  segment.angle = 20,
                  size=3.5, min.segment.length = 0, max.overlaps = Inf) +
  coord_polar() + 
  scale_y_continuous(limits = c(0, 5.5)) +
  theme_classic() +
  theme(axis.line = element_blank(), 
        #axis.text.x = element_blank(), 
        axis.text.y = element_blank(),
        axis.ticks = element_blank(), 
        axis.title.x = element_blank(), 
        axis.title.y = element_blank(), 
        #axis.ticks.margin = unit(c(0,0,0,0), "lines"), 
        legend.position = "right", 
        panel.background = element_rect(fill = "white"), 
        panel.border = element_blank(), 
        #panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()
        )

需求

  • 调整x轴刻度标签(1Kb、2Kb…10Kb)到环形内部,避免与长基因名重叠
  • 将环形上的矩形替换为匹配基因方向的箭头形状

修改后代码及说明

完整代码

library(ggplot2)
library(scales)

# 计算基因区间宽度
filld$width <- filld$end - filld$start

ggplot(filld, aes(fill = annotation)) +
  # 绘制环形填充条(替代原geom_rect)
  geom_bar(aes(x = p, width = width, y = 1), 
           stat = "identity", position = "identity") +
  # 根据基因方向添加箭头
  geom_segment(aes(x = ifelse(direction == 1, start, end), 
                   xend = ifelse(direction == 1, end, start),
                   y = 1, yend = 1),
               arrow = arrow(length = unit(0.1, "inches"), type = "closed"),
               linewidth = 1) +
  # 保留重复基因的垂直线段(调整y范围避免遮挡标签)
  geom_segment(data = subset(filld, gene %in% gene[duplicated(gene)]),
               aes(x = p, y = 0, xend = p, yend = 0.8, colour = annotation),
               linewidth = 2) +
  # 基因标签(调整y位置适配新的环形大小)
  geom_text_repel(aes(x = p, y = 1.2,
                      label = gene, 
                      family = 'Roboto Condensed'),  
                  nudge_x = .15,
                  box.padding = 0.5,
                  nudge_y = 0.3,
                  segment.curvature = -0.1,
                  segment.ncp = 3,
                  segment.angle = 20,
                  size=3.5, min.segment.length = 0, max.overlaps = Inf) +
  # 极坐标转换
  coord_polar(start = 0) +
  # 坐标轴设置
  scale_x_continuous(label = label_bytes("kB"), breaks = scales::pretty_breaks(n = 10)) +
  scale_y_continuous(limits = c(0, 1.8), expand = c(0, 0)) +
  # 主题调整:将x轴标签移至环形内部
  theme_classic() +
  theme(axis.line = element_blank(), 
        axis.text.x = element_text(vjust = 1.5, size = 8),  # vjust>1将标签移到内侧
        axis.text.y = element_blank(),
        axis.ticks = element_blank(), 
        axis.title.x = element_blank(), 
        axis.title.y = element_blank(), 
        legend.position = "right", 
        panel.background = element_rect(fill = "white"), 
        panel.border = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.grid.major.y = element_blank())

关键修改说明

  1. x轴标签移至环形内部

    • 通过theme(axis.text.x = element_text(vjust = 1.5, size = 8))调整标签垂直位置:vjust值大于1时,标签会从环形外侧移动到内侧;
    • 缩小scale_y_continuous的范围,确保环形内部有足够空间放置标签,同时避免与基因标签重叠。
  2. 矩形替换为箭头形状

    • 用geom_bar绘制环形填充条,替代原geom_rect,更适配极坐标的环形绘制;
    • 添加geom_segment并结合arrow()参数,根据direction列动态设置箭头的起始/终点:
      • direction=1(正方向):箭头指向基因区间的end位置;
      • direction=-1(反方向):箭头指向基因区间的start位置;
    • arrow(type="closed")设置实心箭头,更符合基因组图的常见样式。

内容的提问来源于stack exchange,提问作者Prakki Rama

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 23:07:06