R语言冲积图实现所有Stratum尺寸一致的技术问询
让ggplot2冲积图的两个Stratum尺寸一致
我用R语言绘制包含两个stratum的冲积图,两个stratum拥有相同类别,但因观测数量不同,绘图后各类别的尺寸存在差异,希望实现所有stratum尺寸一致的效果。
原绘图代码:
g_36a_flow <- ggplot(t_14_long, aes(alluvium = oper_num, x = class, stratum = factor(rating_qal), fill = factor(rating_qal))) + geom_flow(position = position_dodge(width = 0), aes(fill = factor(rating_qal)), width = 0.2) + geom_stratum(width = 0.1, color = "white") + geom_text(data = t_14_long, aes(x = as.numeric(factor(class)) + .075 * ifelse(class == "MGMT", 1.5, -1.5), y = after_stat(y), label = paste0(label_num, "%"), hjust = ifelse(class == "MGMT", 1, 0)), stat = "stratum", size = 10, color = "black", family = "montserrat") + geom_text(aes(label = after_stat(stratum), hjust = ifelse(class == "MGMT", 1, 0), x = as.numeric(factor(class)) + .075 * ifelse(class == "MGMT", -1, 1),), stat = "stratum", size = 10, color = "black", family = "montserrat" ) + ggtitle("g_36a_flow: Management vs OVE: % of projects by rating validation cycle 2018-2023 ") + scale_fill_manual(values = c("#78B743", "#A9CD74", "#DBE9AA", "#F9BEC4", "#F4817D", "#EF3648"), breaks = levels(t_14_long$rating_qal_aux), labels = levels(t_14_long$rating_qal_aux)) + labs(x = "", y = "") + theme_void() + theme(text = element_text(family = "montserrat", size = 30, color = "black"), axis.text = element_text(size = 30, color = "black"), axis.title = element_text(size = 30, color = "black"), plot.title = element_text(size = 30, color = "black"), legend.position = "none", axis.title.y = element_text(angle = 90, vjust = 0.5), plot.background = element_rect(fill = "white"), panel.border = element_blank()) + scale_x_discrete(expand = c(0.1, 0.1)) + scale_y_discrete(expand = c(0.1, 0.1)) g_36a_flow
解决方案
要让两个stratum的每个类别尺寸一致,核心是让每个类别的权重相同,忽略实际观测数量的影响:
- 先给数据添加权重列,确保每个
class和rating_qal的组合权重一致:
library(dplyr) t_14_long <- t_14_long %>% group_by(class, rating_qal) %>% mutate(stratum_weight = 1) %>% # 每个组合权重设为1 ungroup()
- 修改ggplot的映射,加入
weight = stratum_weight参数,强制每个stratum按相同权重计算高度:
g_36a_flow <- ggplot(t_14_long, aes(alluvium = oper_num, x = class, stratum = factor(rating_qal), fill = factor(rating_qal), weight = stratum_weight)) + # 新增权重映射 geom_flow(position = position_dodge(width = 0), aes(fill = factor(rating_qal)), width = 0.2) + geom_stratum(width = 0.1, color = "white") + # 以下文本标注代码保持不变 geom_text(data = t_14_long, aes(x = as.numeric(factor(class)) + .075 * ifelse(class == "MGMT", 1.5, -1.5), y = after_stat(y), label = paste0(label_num, "%"), hjust = ifelse(class == "MGMT", 1, 0)), stat = "stratum", size = 10, color = "black", family = "montserrat") + geom_text(aes(label = after_stat(stratum), hjust = ifelse(class == "MGMT", 1, 0), x = as.numeric(factor(class)) + .075 * ifelse(class == "MGMT", -1, 1),), stat = "stratum", size = 10, color = "black", family = "montserrat" ) + ggtitle("g_36a_flow: Management vs OVE: % of projects by rating validation cycle 2018-2023 ") + scale_fill_manual(values = c("#78B743", "#A9CD74", "#DBE9AA", "#F9BEC4", "#F4817D", "#EF3648"), breaks = levels(t_14_long$rating_qal_aux), labels = levels(t_14_long$rating_qal_aux)) + labs(x = "", y = "") + theme_void() + theme(text = element_text(family = "montserrat", size = 30, color = "black"), axis.text = element_text(size = 30, color = "black"), axis.title = element_text(size = 30, color = "black"), plot.title = element_text(size = 30, color = "black"), legend.position = "none", axis.title.y = element_text(angle = 90, vjust = 0.5), plot.background = element_rect(fill = "white"), panel.border = element_blank()) + scale_x_discrete(expand = c(0.1, 0.1)) + scale_y_discrete(expand = c(0.1, 0.1)) g_36a_flow
这样修改后,两个stratum的每个类别高度会保持一致,达到预期效果。
内容的提问来源于stack exchange,提问作者Luis Fernando Corrales
相关产品推荐
相关产品推荐

