桑基图.R

# ---------------------------
# 1. 加载必要的包
# ---------------------------
# 如果尚未安装,请先安装:ggalluvial, ggplot2, patchwork
library(ggalluvial)
library(ggplot2)
library(patchwork)

# ---------------------------
# 2. 构造桑基图数据并设置因子顺序
# ---------------------------
sankey_data <- data.frame(
  left = rep(c("Core", "Softcore", "Dispensable", "Private"), each = 6),
  right = rep(c("WGD", "TD", "PD", "TRD", "DSD", "SL"), times = 4),
  freq = c(
    36, 10, 15,  8,  5,  2,    # Core -> 各事件
    15, 20, 10,  5,  3,  2,    # Softcore -> 各事件
    10,  5, 10,  3,  2,  1,    # Dispensable -> 各事件
    5,  3,  5,  2,  1,  1     # Private -> 各事件
  )
)
# 设置左侧因子顺序
sankey_data$left <- factor(sankey_data$left, levels = c("Core", "Softcore", "Dispensable", "Private"))
# 设置右侧因子顺序
sankey_data$right <- factor(sankey_data$right, levels = c("WGD", "TD", "PD", "TRD", "DSD", "SL"))

# ---------------------------
# 3. 绘制桑基图(左侧图)
# ---------------------------
sankey_plot <- ggplot(sankey_data,
                      aes(axis1 = left, axis2 = right, y = freq)) +
  scale_x_discrete(limits = c("Gene Category", "Event"), expand = c(.05, .05)) +
  
  # 绘制“河流”部分
  geom_alluvium(aes(fill = left), width = 1/12, alpha = 0.8) +
  
  # 绘制分块(箱体),宽度设为 0.2 以保证框内文字完整显示
  geom_stratum(aes(fill = after_stat(stratum)), width = 0.2, color = "black") +
  geom_text(stat = "stratum", aes(label = after_stat(stratum)), size = 3) +
  
  # 自定义分块填充颜色:
  # 左侧框按要求设为:Core-红、Softcore-蓝、Dispensable-橙、Private-绿,
  # 右侧事件框统一设为灰色(可根据需要修改)
  scale_fill_manual(values = c(
    "Core" = "red",
    "Softcore" = "blue",
    "Dispensable" = "orange",
    "Private" = "green",
    "WGD" = "purple",
    "TD" = "grey80",
    "PD" = "grey80",
    "TRD" = "grey80",
    "DSD" = "grey80",
    "SL" = "grey80"
  )) +
  
  theme_minimal() +
  theme(
    legend.position = "none",     # 删除桑基图图例
    axis.title = element_blank(),
    axis.text.y = element_blank(),
    panel.grid = element_blank()
  ) +
  labs(
    title = "Sankey Diagram with Custom Box Width"
  )

# ---------------------------
# 4. 构造饼图数据并设置因子顺序
# ---------------------------
pie_data <- data.frame(
  event = rep(c("WGD", "TD", "PD", "TRD", "DSD", "SL"), each = 2),
  group = rep(c("Core", "Variable"), 6),
  value = c(36,64, 58,42, 65,35, 61,39, 51,49, 77,23)
)
# 设置饼图中事件的因子顺序与右侧框一致
pie_data$event <- factor(pie_data$event, levels = c("WGD", "TD", "PD", "TRD", "DSD", "SL"))

# ---------------------------
# 5. 绘制饼图(右侧图),使用 facet_wrap 排列
# ---------------------------
pie_plot <- ggplot(pie_data, aes(x = "", y = value, fill = group)) +
  geom_bar(stat = "identity", width = 1, color = "white") +
  coord_polar("y") +
  facet_wrap(~ event, ncol = 1, strip.position = "right") +
  theme_void() +
  scale_fill_manual(values = c("Core" = "red", "Variable" = "blue")) +
  theme(
    # 保留图例,若需要可用 legend.position 调整位置,如 legend.position = "bottom"
    strip.text = element_text(size = 12, face = "bold")
  )

# ---------------------------
# 6. 拼合桑基图与饼图(无连接线)
# ---------------------------
final_plot <- sankey_plot + pie_plot + plot_layout(ncol = 2, widths = c(1, 0.5))
print(final_plot)

# ---------------------------
# 7. 保存为高分辨率图片
# ---------------------------
# 保存 PNG 文件,分辨率设置为 300 dpi
ggsave("final_plot.png", final_plot, width = 10, height = 6, dpi = 300)
# 保存 SVG 文件
ggsave("final_plot.svg", final_plot, width = 10, height = 6)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

终是蝶衣梦晓楼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值