I want my stratum the same size in my alluvial graph

69 Views Asked by At

I'm making an alluvial graph in R with two stratum. Each stratum has the same categories but when I graph, it has different size for every category because the number of observations is different. This is how it looks:

image1

and this is the code:

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\nvalidation 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

I'm expecting something like this:

image2

0

There are 0 best solutions below