0

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

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Jul 09 '23 at 00:56
  • Hi Luis! Welcome to StackOverflow. It appears that a Sankey diagram might be more up your alley - see https://stackoverflow.com/a/67114079/4145280 – Mark Jul 09 '23 at 02:40
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jul 09 '23 at 09:17

0 Answers0