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

R语言circlize包Chord Diagram隐藏同一扇区指向箭头的方法问询

弦图隐藏同扇区指向箭头实现方法

要实现仅展示跨大洲迁移箭头、保留无箭头内部迁移区域且不改变扇区宽度的需求,无需修改原始数据(避免影响扇区总宽度计算),只需通过circlize包chordDiagram函数的参数控制不同类型链接的箭头显示即可。

核心逻辑

  • 保留所有原始数据(含大洲内部迁移记录),保证扇区总宽度和坐标轴刻度完全不变
  • 对同源同宿的内部迁移链接,将箭头长度设为0,同时取消流向高度差
  • 对跨大洲迁移链接,保留原有箭头和高度差设置

修改步骤

在原有代码的chordDiagram调用前,先添加两个参数向量的生成代码:

# 逐行判断是否为大洲内部迁移
is_self_loop <- data_long$rowname == data_long$key
# 定义箭头长度:内部迁移箭头长度为0,跨区域使用默认值0.02
link_arr_len <- ifelse(is_self_loop, 0, 0.02)
# 定义流向高度差:内部迁移无高度差,跨区域保留原设置
link_diff_height <- ifelse(is_self_loop, 0, -0.04)

然后修改chordDiagram的参数,将原有固定的diffHeight替换为自定义的link_diff_height,新增link.arr.length参数:

# 修改后的Base plot部分
chordDiagram(
  x = data_long, 
  grid.col = mycolor,
  transparency = 0.25,
  directional = 1,
  direction.type = c("arrows", "diffHeight"), 
  diffHeight  = link_diff_height, # 替换为自定义高度差向量
  link.arr.length = link_arr_len, # 新增自定义箭头长度向量
  annotationTrack = "grid", 
  annotationTrackHeight = c(0.05, 0.1),
  link.arr.type = "big.arrow", 
  link.sort = TRUE, 
  link.largest.ontop = TRUE)

完整可运行代码

# 加载依赖库
library(tidyverse)
library(viridis)
library(patchwork)
library(hrbrthemes)
library(circlize)
library(chorddiag)  

# 加载数据集
data <- read.table("https://raw.githubusercontent.com/holtzy/data_to_viz/master/Example_dataset/13_AdjacencyDirectedWeighted.csv", header=TRUE)

# 短名称处理
colnames(data) <- c("Africa", "East Asia", "Europe", "Latin Ame.",   "North Ame.",   "Oceania", "South Asia", "South East Asia", "Soviet Union", "West.Asia")
rownames(data) <- colnames(data)

# 转换为长格式
data_long <- data %>%
  rownames_to_column %>%
  gather(key = 'key', value = 'value', -rowname)

# 预处理参数
circos.clear()
circos.par(start.degree = 90, gap.degree = 4, track.margin = c(-0.1, 0.1), points.overflow.warning = FALSE)
par(mar = rep(0, 4))

# 颜色配置
mycolor <- viridis(10, alpha = 1, begin = 0, end = 1, option = "D")
mycolor <- mycolor[sample(1:10)]

# 新增:自定义箭头和高度差参数
is_self_loop <- data_long$rowname == data_long$key
link_arr_len <- ifelse(is_self_loop, 0, 0.02)
link_diff_height <- ifelse(is_self_loop, 0, -0.04)

# 弦图绘制
chordDiagram(
  x = data_long, 
  grid.col = mycolor,
  transparency = 0.25,
  directional = 1,
  direction.type = c("arrows", "diffHeight"), 
  diffHeight  = link_diff_height,
  link.arr.length = link_arr_len,
  annotationTrack = "grid", 
  annotationTrackHeight = c(0.05, 0.1),
  link.arr.type = "big.arrow", 
  link.sort = TRUE, 
  link.largest.ontop = TRUE)

# 添加文本和坐标轴
circos.trackPlotRegion(
  track.index = 1, 
  bg.border = NA, 
  panel.fun = function(x, y) {
    
    xlim = get.cell.meta.data("xlim")
    sector.index = get.cell.meta.data("sector.index")
    
    # 添加扇区名称
    circos.text(
      x = mean(xlim), 
      y = 3.2, 
      labels = sector.index, 
      facing = "bending", 
      cex = 0.8
      )

    # 添加刻度
    circos.axis(
      h = "top", 
      major.at = seq(from = 0, to = xlim[2], by = ifelse(test = xlim[2]>10, yes = 2, no = 1)), 
      minor.ticks = 1, 
      major.tick.percentage = 0.5,
      labels.niceFacing = FALSE)
  }
)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 19:45:03