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

在R中统一多个ggraph的权重与节点尺寸缩放

问题

使用igraph和ggraph绘制两个网络图时,边粗细(权重映射)和节点尺寸的缩放不一致,导致视觉对比存在误导性:

  • 左侧图权重范围140-180,右侧仅40-100,但边粗细视觉范围相近;
  • 右侧图freq=200的节点视觉尺寸未达到freq=100节点的两倍。

曾尝试为权重和节点定义相同的全局最小/最大值limits,并设置相同的range来修复,但问题未完全解决:

scale_size_continuous(
   limits = c(83, 273),
   range  = c(3, 12)
)
scale_edge_width_continuous(
   limits = c(31, 197),
   range  = c(0.2, 3)
)

解决方案

要实现两图缩放比例完全对齐,需做到两点:统一全局映射范围,以及确保节点尺寸的线性视觉映射(解决数值翻倍但视觉尺寸未达两倍的问题)。

1. 计算全局映射范围

先提取两个图中f_norm(节点尺寸变量)和weight_norm(边权重变量)的全局最小/最大值:

  • f_norm全局范围:37.56 ~ 89.51
  • weight_norm全局范围:14.03 ~ 64.59

2. 统一两图的缩放设置

关键说明

默认scale_size_continuous是将数值映射到节点面积,这会导致数值翻倍时,视觉直径仅为√2≈1.41倍。若要让数值与视觉直径线性成正比(即数值两倍对应直径两倍),需改用scale_radius(直接映射直径的一半)。

修改后的右侧图代码

# 构建图:
g_S <- graph_from_data_frame(edges_weighted_S, vertices = node_freq_S, directed = FALSE)

# 绘制(调整边宽和节点尺寸):
ggraph(g_S, layout = "circle") + 
  geom_edge_link(aes(width = weight_norm), alpha = 0.6, color = "steelblue") +
  # 统一全局边宽缩放
  scale_edge_width_continuous(
    name = "Co-occurrences\nwithin TCUs\n(normalized per 100)",
    limits = c(14.03, 64.59),
    range  = c(0.2, 3)
  )+
  # 用radius映射节点直径,实现线性视觉比例
  geom_node_point(aes(radius = f_norm), color = "tomato") +
  geom_node_text(aes(label = name), repel = TRUE) +
  # 统一全局节点尺寸缩放
  scale_radius(
    name = "Occurrences\nacross TCUs\n(normalized per 100)",
    limits = c(37.56, 89.51),
    range  = c(1.5, 6) # 对应直径3到12,与原size范围匹配
  )+
  theme_minimal() +
  theme(legend.position = "right")+
  theme(
    axis.text.y = element_blank(),
    axis.text.x = element_blank()
  ) +
  labs(title = "Network of prototypical manifestations of multimodal variables\nin story TCUs (N = 221)",
       x ="",
       y = "")

修改后的左侧图代码

# 构建图:
g <- graph_from_data_frame(edges_weighted, vertices = node_freq, directed = FALSE)

# 绘制:
ggraph(g, layout = "circle") + 
  geom_edge_link(aes(width = weight_norm), alpha = 0.6, color = "steelblue") +
  # 统一全局边宽缩放
  scale_edge_width_continuous(
    name = "Co-occurrences\nwithin TCUs\n(normalized per 100)",
    limits = c(14.03, 64.59),
    range  = c(0.2, 3)
  )+
  # 用radius映射节点直径,实现线性视觉比例
  geom_node_point(aes(radius = f_norm), color = "tomato") +
  geom_node_text(aes(label = name), repel = TRUE) +
  # 统一全局节点尺寸缩放
  scale_radius(
    name = "Occurrences\nacross TCUs\n(normalized per 100)",
    limits = c(37.56, 89.51),
    range  = c(1.5, 6)
  )+
  theme_minimal() +
  theme(legend.position = "right")+
  theme(
    axis.text.y = element_blank(),
    axis.text.x = element_blank()
  ) +
  labs(title = "Network of prototypical manifestations of multimodal variables\nin question TCUs (N = 305)",
       x ="",
       y = "")

效果验证

  • 边粗细:两个图中相同weight_norm值对应完全一致的视觉宽度;
  • 节点尺寸:数值翻倍时,视觉直径也会翻倍,符合线性比例要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 03:12:02