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

如何调整ggsankey图所有文本大小并保持标签文本居中

解决ggsankey图字体大小调整与标签居中问题

问题背景

调整ggsankey图的所有字体大小时,遇到两个问题:

  • 修改theme_sankey(base_size)仅改变了坐标轴和图例文本,节点标签字号不受影响;
  • 调整geom_sankey_label(size)缩小标签文本后,文本无法在标签框内居中。

用户原始代码:

data <- data.frame(
  Before = c("Poor", "Poor", "Poor", "Intermediate", "Poor", "Intermediate", "Intermediate", "Intermediate", "Poor", "Normal",
             "Intermediate", "Intermediate", "Normal", "Intermediate", "Poor", "Normal", "Poor", "Poor", "Normal", "Normal",
             "Normal", "Normal", "Poor", "Intermediate", "Poor", "Intermediate", "Normal", "Intermediate", "Poor",
             "Intermediate", "Normal", "Poor", "Poor", "Normal", "Poor", "Intermediate", "Normal", "Poor", "Normal",
             "Normal", "Intermediate", "Poor", "Poor", "Normal", "Poor", "Normal", "Poor", "Intermediate", "Normal",
             "Intermediate", "Normal", "Normal", "Poor", "Normal", "Intermediate", "Normal", "Normal", "Poor", "Normal",
             "Intermediate", "Normal", "Poor", "Normal", "Poor", "Intermediate", "Poor", "Intermediate", "Intermediate",
             "Poor", "Intermediate", "Intermediate", "Normal", "Intermediate", "Normal", "Normal", "Intermediate",
             "Poor", "Poor", "Normal", "Intermediate", "Normal", "Intermediate", "Normal", "Poor", "Poor", "Intermediate",
             "Poor", "Normal", "Intermediate", "Intermediate", "Poor", "Intermediate", "Normal", "Intermediate", "Poor", "Poor", "Normal", "Poor", "Normal", "Poor"),
  After = c("Poor", "Poor", "Poor", "Intermediate", "Poor", "Intermediate", "Intermediate", "Poor", "Poor", "Normal",
            "Intermediate", "Intermediate", "Normal", "Intermediate", "Poor", "Normal", "Poor", "Poor", "Normal",
            "Normal", "Normal", "Normal", "Intermediate", "Intermediate", "Poor", "Intermediate", "Normal", "Intermediate",
            "Poor", "Intermediate", "Normal", "Poor", "Poor", "Normal", "Poor", "Intermediate", "Normal", "Poor",
            "Normal", "Normal", "Intermediate", "Poor", "Poor", "Normal", "Poor", "Normal", "Poor", "Intermediate",
            "Normal", "Intermediate", "Normal", "Normal", "Poor", "Normal", "Intermediate", "Normal", "Normal", "Poor",
            "Normal", "Intermediate", "Normal", "Poor", "Normal", "Poor", "Intermediate", "Poor", "Intermediate",
            "Intermediate", "Poor", "Intermediate", "Intermediate", "Normal", "Intermediate", "Normal", "Normal",
            "Intermediate", "Intermediate", "Poor", "Normal", "Intermediate", "Normal", "Normal", "Poor", "Poor",
            "Intermediate", "Normal", "Normal", "Poor", "Intermediate", "Normal", "Poor", "Poor","Normal", "Intermediate", "Poor", "Poor", "Normal", "Poor", "Normal", "Poor")
)


transition_counts <-  data %>% make_long(Before, After)

# Create Sankey plot
ggplot(transition_counts, aes(x = x, next_x = next_x, node = node, next_node = next_node, fill = factor(node), label = node)) +
  geom_sankey(flow.alpha = .6,
              node.color = "gray30") +
  geom_sankey_label(size=3,color = "white", fill = "gray40", hjust = -0.2) +
  scale_fill_viridis_d() +
  theme_sankey(base_size = 10) +
  labs(x = NULL) +
  theme(legend.position = "bottom")  +
  guides(fill = guide_legend(ncol = 2))

解决方案

1. 修复标签居中问题

标签不居中的核心原因是设置了hjust = -0.2,这个参数强制文本向左偏移,和字体大小无关。要让文本在标签框内居中,需将hjust设为0.5,同时可以通过nudge_x调整标签相对于节点的位置,避免文本和节点重叠。

2. 统一调整所有文本大小

  • 用theme_sankey(base_size)控制坐标轴、图例等主题文本的字号;
  • 用geom_sankey_label(size)设置节点标签的字号,配合hjust=0.5实现居中;
  • 如需调整图例标题等其他文本,可在theme()中单独设置。

修改后的完整代码

data <- data.frame(
  Before = c("Poor", "Poor", "Poor", "Intermediate", "Poor", "Intermediate", "Intermediate", "Intermediate", "Poor", "Normal",
             "Intermediate", "Intermediate", "Normal", "Intermediate", "Poor", "Normal", "Poor", "Poor", "Normal", "Normal",
             "Normal", "Normal", "Poor", "Intermediate", "Poor", "Intermediate", "Normal", "Intermediate", "Poor",
             "Intermediate", "Normal", "Poor", "Poor", "Normal", "Poor", "Intermediate", "Normal", "Poor", "Normal",
             "Normal", "Intermediate", "Poor", "Poor", "Normal", "Poor", "Normal", "Poor", "Intermediate", "Normal",
             "Intermediate", "Normal", "Normal", "Poor", "Normal", "Intermediate", "Normal", "Normal", "Poor", "Normal",
             "Intermediate", "Normal", "Poor", "Normal", "Poor", "Intermediate", "Poor", "Intermediate", "Intermediate",
             "Poor", "Intermediate", "Intermediate", "Normal", "Intermediate", "Normal", "Normal", "Intermediate",
             "Poor", "Poor", "Normal", "Intermediate", "Normal", "Intermediate", "Normal", "Poor", "Poor", "Intermediate",
             "Poor", "Normal", "Intermediate", "Intermediate", "Poor", "Intermediate", "Normal", "Intermediate", "Poor", "Poor", "Normal", "Poor", "Normal", "Poor"),
  After = c("Poor", "Poor", "Poor", "Intermediate", "Poor", "Intermediate", "Intermediate", "Poor", "Poor", "Normal",
            "Intermediate", "Intermediate", "Normal", "Intermediate", "Poor", "Normal", "Poor", "Poor", "Normal",
            "Normal", "Normal", "Normal", "Intermediate", "Intermediate", "Poor", "Intermediate", "Normal", "Intermediate",
            "Poor", "Intermediate", "Normal", "Poor", "Poor", "Normal", "Poor", "Intermediate", "Normal", "Poor",
            "Normal", "Normal", "Intermediate", "Poor", "Poor", "Normal", "Poor", "Normal", "Poor", "Intermediate",
            "Normal", "Intermediate", "Normal", "Normal", "Poor", "Normal", "Intermediate", "Normal", "Normal", "Poor",
            "Normal", "Intermediate", "Normal", "Poor", "Normal", "Poor", "Intermediate", "Poor", "Intermediate",
            "Intermediate", "Poor", "Intermediate", "Intermediate", "Normal", "Intermediate", "Normal", "Normal",
            "Intermediate", "Intermediate", "Poor", "Normal", "Intermediate", "Normal", "Normal", "Poor", "Poor",
            "Intermediate", "Normal", "Normal", "Poor", "Intermediate", "Normal", "Poor", "Poor","Normal", "Intermediate", "Poor", "Poor", "Normal", "Poor", "Normal", "Poor")
)


transition_counts <-  data %>% make_long(Before, After)

# Create Sankey plot
ggplot(transition_counts, aes(x = x, next_x = next_x, node = node, next_node = next_node, fill = factor(node), label = node)) +
  geom_sankey(flow.alpha = .6,
              node.color = "gray30") +
  # 调整size设置标签字号,hjust=0.5实现居中,nudge_x调整标签位置避免和节点重叠
  geom_sankey_label(size=2, color = "white", fill = "gray40", hjust = 0.5, nudge_x = 0.2) +
  scale_fill_viridis_d() +
  # 设置主题基础字号,控制坐标轴、图例文本
  theme_sankey(base_size = 8) +
  labs(x = NULL) +
  theme(
    legend.position = "bottom",
    # 如需调整图例标题字号,可添加以下设置
    # legend.title = element_text(size=7)
  )  +
  guides(fill = guide_legend(ncol = 2))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 05:25:59