Home > Software design >  Simple one about Alluvial plot in R
Simple one about Alluvial plot in R

Time:11-16

I would like to make a simple flow graph. Here is my code:

## Data
    x = tibble(qms = c("FLOW", "FLOW"),
               move1 = c("Birth", "Birth"),
               move2 = c("Direct", NA),
               freq = c(100, 50))
## Graph
    x %>% 
      mutate(id = qms) %>% 
      to_lodes_form(axis = 2:3, id = id) %>% 
      na.omit() %>% 
      ggplot(aes(x = x, stratum = stratum, alluvium = id,
                 y = freq, label = stratum))  
      scale_x_discrete(expand = c(.1, .1))  
      geom_flow(aes(fill = qms),stat = "alluvium")  
      geom_stratum(aes(fill = stratum), show.legend=FALSE)  
      geom_text(stat = "stratum", size = 3) 

This is the outcome:

enter image description here

My desired outcome is that:

enter image description here

How can I express the decreasing pattern with the missing value?

CodePudding user response:

By slightly reshaping your data you can get what you want. I think the key is to map the alluvium to something fixed like 1 so that it will be a single flow, and mapping stratum to the same variable as x.

library(tidyverse)
library(ggalluvial)

x <- tibble(x = c("Birth", "Direct"),
            y = c(100, 50))

x %>% 
  ggplot(aes(x, y, alluvium = 1, stratum = x))  
  geom_alluvium()  
  geom_stratum()

Created on 2022-11-15 with reprex v2.0.2

  • Related