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

如何在R中创建环形冲积图?

在R中实现环形冲积图(或优化物种归属展示)

针对你要突出「所有物种最终归属同一标签」的需求,以下提供两种可行方案:

方案1:用circlize创建环形桑基图(匹配你想要的环形样式)

circlize包专门用于环形可视化,能实现类似环形冲积图的流量效果,通过线条粗细直观体现各物种的样本数量:

步骤1:安装并加载包

install.packages("circlize")
library(circlize)

步骤2:准备数据并统计流量

# 你的示例数据
Sample.ID <- data.frame(
  Sample = c("L1", "L2", "L3", "L4", "L5", "L6", "L7", "L8", "L9", "L0"),
  Species = c("Fish", "Shark", "Fish", "Crab", "Crab", "Shark", "Seal", "Crab", "Fish", "Fish"),
  Label = rep("Seafood", 10)
)

# 统计各层级间的流量
sample_species_flow <- table(Sample.ID$Sample, Sample.ID$Species)
species_label_flow <- table(Sample.ID$Species, Sample.ID$Label)

# 合并为环形图所需的矩阵
flow_matrix <- rbind(sample_species_flow, species_label_flow)
rownames(flow_matrix) <- c(paste0("Sample_", rownames(sample_species_flow)),
                           paste0("Species_", rownames(species_label_flow)))
colnames(flow_matrix) <- c(colnames(sample_species_flow), "Label_Seafood")

步骤3:绘制环形桑基图

circos.clear()
circos.par(start.degree = 90, gap.degree = 5) # 调整环形起始角度和间隙
chordDiagram(flow_matrix,
             grid.col = c(Fish = "#4292c6", Shark = "#fdae61", Crab = "#238b45", Seal = "#d73027",
                          Sample_L1 = "#cccccc", Sample_L2 = "#cccccc", Sample_L3 = "#cccccc",
                          Sample_L4 = "#cccccc", Sample_L5 = "#cccccc", Sample_L6 = "#cccccc",
                          Sample_L7 = "#cccccc", Sample_L8 = "#cccccc", Sample_L9 = "#cccccc",
                          Sample_L0 = "#cccccc", Label_Seafood = "#9e9ac8"),
             transparency = 0.2,
             directional = 1,
             direction.type = c("arrows", "diffHeight"),
             link.arr.type = "big.arrow")
title("环形样本-物种-标签流量图")

该图会将三个层级(样本、物种、标签)分布在环形边缘,流量线条的粗细对应样本数量,清晰展示所有物种最终汇聚到Seafood标签的关系。

方案2:优化现有ggplot冲积图,强化归属关系

如果更倾向于使用ggplot生态,可修改原有代码,让「所有物种归为同一标签」的信息更直观:

library(ggplot2)
library(ggalluvial)

ggplot(data = Sample.ID,
       aes(y = 1, axis1 = Sample, axis2 = Species, axis3 = Label)) +
  scale_x_discrete(limits = c("Sample", "Species", "Label"), expand = c(.2, .05)) +
  xlab("Classification") +
  geom_alluvium(aes(fill = Species, width = ..count..)) + # 用宽度体现样本数量
  geom_stratum(aes(fill = Species)) +
  geom_text(stat = "stratum", aes(label = after_stat(stratum))) + # 添加层级标签
  theme_minimal() +
  ggtitle("样本-物种-标签归属(所有物种均为海鲜)") +
  theme(legend.position = "bottom")

修改后的图会让所有流向最终汇聚到同一个标签节点,通过线条宽度区分各物种的样本量,同时添加标签提升可读性。

补充:ggplot生态实现环形图

若想在ggplot中实现环形效果,可结合ggsankey和极坐标:

install.packages("ggsankey")
library(ggsankey)

# 转换为桑基图格式数据
df_sankey <- Sample.ID %>%
  make_long(Sample, Species, Label)

ggplot(df_sankey, aes(x = x, next_x = next_x, node = node, next_node = next_node,
                      fill = factor(node), label = node)) +
  geom_sankey(flow.alpha = 0.5, node.color = "black") +
  geom_sankey_label(size = 3, color = "white", fill = "black") +
  scale_fill_viridis_d() +
  coord_polar() +
  theme_void() +
  ggtitle("环形桑基图(样本-物种-标签)")

可根据需求调整标签位置、颜色等样式优化可读性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 23:31:13