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

如何调整ggalluvial桑基图中stock与newspaper标签的长度

问题描述

我运行了以下用于绘制桑基图的代码:

library(ggalluvial)
library(ggplot2)
library(dplyr)

df <- data.frame(status = c("open", "close", "close", "open/close", "close"), 
                 stock = c("google", "amazon", "amazon", "yahoo", "amazon"), 
                 newspaper = c("times", "newyork", "london", "times", "times"))

# 统计每个桑流的出现次数
df <- df %>% dplyr::group_by(stock, newspaper, status) %>% 
  summarise(n = n()) 

# 定义因子顺序
df$status <- factor(df$status, levels = c("open", "open/close", "close"))
df$stock <- factor(df$stock, levels = c("google", "amazon", "yahoo"))
df$newspaper <- factor(df$newspaper, levels = c("times", "newyork", "london"))

# 绘制桑基图
ggplot2::ggplot(df.2, aes(y = n, axis1 = stock, axis2 = newspaper)) +
  ggalluvial::geom_alluvium(aes(fill = status), width = 1/12) +
  ggalluvial::geom_stratum(width = 1/12, fill = "black", color = "grey") +
  ggplot2::geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  ggplot2::scale_x_discrete(limits = c("stock", "newspaper"), expand = c(.05, .05)) +
  ggplot2::scale_fill_brewer(type = "qual", palette = "Set1") +
  ggplot2::ggtitle("Alluvial-Test")

注意:代码中存在一处小错误——ggplot调用时使用了未定义的df.2,实际应改为df才能正常运行。

请问是否有方法可以缩短图中stock和newspaper这两个轴的标签长度?

解决方法

针对轴标签缩短的需求,有两种常用实现方式:

1. 直接自定义轴标题标签

在scale_x_discrete函数中通过labels参数,将原轴标题替换为更简短的文本:

ggplot2::ggplot(df, aes(y = n, axis1 = stock, axis2 = newspaper)) +
  ggalluvial::geom_alluvium(aes(fill = status), width = 1/12) +
  ggalluvial::geom_stratum(width = 1/12, fill = "black", color = "grey") +
  ggplot2::geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  ggplot2::scale_x_discrete(limits = c("stock", "newspaper"), 
                            labels = c("股票", "报刊"),  # 自定义短标题
                            expand = c(.05, .05)) +
  ggplot2::scale_fill_brewer(type = "qual", palette = "Set1") +
  ggplot2::ggtitle("桑基图测试")

2. 截断轴上的类别标签(若类别文本过长)

如果是stock或newspaper的类别名称(如"amazon""newyork")太长需要缩短,可通过两种方式处理:

自动截断文本

借助stringr包的str_trunc函数,自动将长文本截断为指定长度:

library(stringr)

ggplot2::ggplot(df, aes(y = n, axis1 = stock, axis2 = newspaper)) +
  ggalluvial::geom_alluvium(aes(fill = status), width = 1/12) +
  ggalluvial::geom_stratum(width = 1/12, fill = "black", color = "grey") +
  ggplot2::geom_label(stat = "stratum", aes(label = str_trunc(after_stat(stratum), 4))) +  # 截断为4个字符
  ggplot2::scale_x_discrete(limits = c("stock", "newspaper"), 
                            labels = c("股票", "报刊"),
                            expand = c(.05, .05)) +
  ggplot2::scale_fill_brewer(type = "qual", palette = "Set1") +
  ggplot2::ggtitle("桑基图测试")

手动修改因子标签

提前修改数据框中因子的标签为更简短的文本:

df$stock <- factor(df$stock, 
                   levels = c("google", "amazon", "yahoo"),
                   labels = c("谷歌", "亚马逊", "雅虎"))  # 自定义短类别标签
df$newspaper <- factor(df$newspaper, 
                       levels = c("times", "newyork", "london"),
                       labels = c("时报", "纽约报", "伦敦报"))

# 绘制修改后的桑基图
ggplot2::ggplot(df, aes(y = n, axis1 = stock, axis2 = newspaper)) +
  ggalluvial::geom_alluvium(aes(fill = status), width = 1/12) +
  ggalluvial::geom_stratum(width = 1/12, fill = "black", color = "grey") +
  ggplot2::geom_label(stat = "stratum", aes(label = after_stat(stratum))) +
  ggplot2::scale_x_discrete(limits = c("stock", "newspaper"), 
                            labels = c("股票", "报刊"),
                            expand = c(.05, .05)) +
  ggplot2::scale_fill_brewer(type = "qual", palette = "Set1") +
  ggplot2::ggtitle("桑基图测试")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 15:15:37