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

求助:如何用R实现内层聚类外层症状的双层环形关联图

解决双层环形症状聚类关联图的问题

你的代码使用layout='circle'会将所有节点(聚类和症状)放在同一个环形上,无法实现内圈聚类、外圈症状的双层结构。要实现目标效果,需要手动指定节点布局,给两类节点分配不同的半径和对应角度,具体修改如下:

修改后的完整代码

library(igraph)
library(ggraph)
library(dplyr)
library(readxl)

# 加载数据(如果用本地文件可替换为read_excel("Final.xlsx"))
# 以下为你提供的示例数据构造
data <- tibble(
  Cluster = c("Arthritis/Myalgias", rep("Autonomic",2), rep("Cardiac",2), 
              rep("Cognitive",2), rep("Dermatologic",2), "Fatigue", 
              rep("Fever/Chills",2), rep("Gastrointestinal",2), 
              rep("Mood",3), "Multisystem Inflammatory Syndrome", 
              rep("Neurologic",5), rep("Respiratory",4), "Sleep"),
  Symptom = c("New Muscle Aches (myalgia), Joint Aches, Body Aches, or Pain",
              "Fast-beating or Pounding Heart (heart palpitations)", "Dizziness",
              "Fast-beating or Pounding Heart (heart palpitations)", "Chest Pain",
              "Brain Fog (worsened memory or concentration)", "Confusion",
              "Hair Loss", "Rash", "Fatigue or Tiredness",
              "Fever or Feeling Feverish", "Chills",
              "Diarrhea", "Nausea",
              "Feeling Anxious or Worried", "Feeling Depressed", "Other Mood Changes",
              "Multisystem Inflammatory Syndrome, MIS",
              "Headache", "Change in Smell", "Change in Taste", 
              "Loss or Change in Vision", "Tinnitus (ringing in ears)",
              "Cough", "Sore Throat", "Runny Nose and/or Nasal Congestion", 
              "Shortness of Breath (Dyspnea) or Difficulty Breathing",
              "Sleep Problems")
)

# 创建顶点列表并标记类型
vertices <- data.frame(
  name = unique(c(data$Cluster, data$Symptom)),
  type = ifelse(unique(c(data$Cluster, data$Symptom)) %in% data$Cluster, "Cluster", "Symptom")
)

# 创建边
edges <- data.frame(from = data$Cluster, to = data$Symptom)

# 构建图对象
graph <- graph_from_data_frame(d = edges, vertices = vertices, directed = FALSE)

# --- 手动生成双层环形布局 ---
# 分离聚类和症状节点
cluster_nodes <- vertices %>% filter(type == "Cluster") %>% pull(name)
symptom_nodes <- vertices %>% filter(type == "Symptom") %>% pull(name)

# 计算每个节点的角度(均分环形)
n_clusters <- length(cluster_nodes)
n_symptoms <- length(symptom_nodes)
cluster_angles <- seq(0, 2*pi, length.out = n_clusters + 1)[1:n_clusters]
symptom_angles <- seq(0, 2*pi, length.out = n_symptoms + 1)[1:n_symptoms]

# 分配半径:聚类在内圈(半径1),症状在外圈(半径2)
layout_df <- tibble(
  name = c(cluster_nodes, symptom_nodes),
  x = c(cos(cluster_angles)*1, cos(symptom_angles)*2),
  y = c(sin(cluster_angles)*1, sin(symptom_angles)*2),
  type = c(rep("Cluster", n_clusters), rep("Symptom", n_symptoms))
)

# 将布局关联到图对象
V(graph)$x <- layout_df$x[match(V(graph)$name, layout_df$name)]
V(graph)$y <- layout_df$y[match(V(graph)$name, layout_df$name)]

# 绘图
ggraph(graph, layout = "manual", x = V(graph)$x, y = V(graph)$y) +
  # 绘制连线,颜色对应聚类
  geom_edge_link(aes(color = from), alpha = 0.7, width = 0.8) +
  # 绘制节点:聚类节点更大,颜色区分
  geom_node_point(aes(color = type, size = type), alpha = 0.8) +
  # 节点文本:聚类在内圈外侧,症状在外圈外侧,避免重叠
  geom_node_text(aes(label = name, hjust = ifelse(type == "Cluster", 1.1, -0.1)), 
                 size = 3, color = "black") +
  # 自定义颜色和大小
  scale_color_manual(values = c("Cluster" = "#2c3e50", "Symptom" = "#e74c3c")) +
  scale_size_manual(values = c("Cluster" = 6, "Symptom" = 4)) +
  # 移除坐标轴和背景
  theme_void() +
  labs(title = "症状-聚类关联双层环形图") +
  # 调整图例位置
  theme(legend.position = "bottom")

关键修改说明

  1. 手动布局生成:
    • 给聚类节点分配较小的半径(1),症状节点分配较大的半径(2),强制形成内外双环
    • 对两类节点分别计算均分角度,保证环形分布均匀有序
  2. 节点与文本优化:
    • 聚类节点设置更大尺寸,强化层级区分
    • 根据节点类型调整文本位置,避免内外圈文本互相遮挡
  3. 连线可视化优化:
    • 用聚类名称作为连线颜色,清晰展示每个症状对应的所属聚类

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 01:45:06