library(ggalluvial)
library(ggplot2)
library(patchwork)
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,
15, 20, 10, 5, 3, 2,
10, 5, 10, 3, 2, 1,
5, 3, 5, 2, 1, 1
)
)
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"))
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) +
geom_stratum(aes(fill = after_stat(stratum)), width = 0.2, color = "black") +
geom_text(stat = "stratum", aes(label = after_stat(stratum)), size = 3) +
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"
)
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"))
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(
strip.text = element_text(size = 12, face = "bold")
)
final_plot <- sankey_plot + pie_plot + plot_layout(ncol = 2, widths = c(1, 0.5))
print(final_plot)
ggsave("final_plot.png", final_plot, width = 10, height = 6, dpi = 300)
ggsave("final_plot.svg", final_plot, width = 10, height = 6)