1

My data is a set of activities completed by persons. The sequence of activities a person takes varies. The data below show the activities for each step (Step1, Step2, etc). I'd like an alluvial plot that labels the activities at each step (each a different node 1, 2, 3...) What is the best approach? Here's what I have so far:

df<-structure(list(acts_activity_id = c("9928131", "445661", "686203", "687868", "688564"),     Step1 = c("Unable to Reach", "Unable to Reach", 
    "Search Correspondence", "Unable to Reach", "Unable to Reach"), Step2 = c("Match Request",     NA, "Connection Made", NA, "Match Request"
), Step3 = c("Support Group Request", NA, "Connection Contact Attempt", NA, "Support Group     Request"),Step4 = c("Information Provided", 
  NA, "Not Available to Support", NA, "Information Provided"), 
 Step5 = c(NA_character_, NA_character_, NA_character_, NA_character_, 
  NA_character_)), class = c("grouped_df", "tbl_df", "tbl", 
  "data.frame"), 
  row.names = c(NA, -5L), 
  groups = structure(list(acts_activity_id = c("9928131", "445661", "686203", "687868",     "688564"), .rows = structure(list(1L, 2L, 3L, 4L, 5L), ptype = integer(0), class =     c("vctrs_list_of", 
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L),         .drop = TRUE))



df %>%
  ggplot( 
aes(
  axis1=Step1, #each step has different values; individuals go thru different sequence of     steps
  axis2=Step2,      axis3=Step3,      axis4=Step4,      axis5=Step5      ))+  
  geom_flow()+
  geom_stratum()+
  labs(title="Activity Sequence")

The first

Ben
  • 1,113
  • 10
  • 26

1 Answers1

0

If you have your data in this order (each column is a set of different activities), then use ggsankey:

df$acts_activity_id<-NULL 
x<-df %>% ggsankey::make_long(Step1,Step2,Step3,Step4,Step5)

ggplot(x, aes(x = x, next_x = next_x, 
           node = node, next_node = next_node, 
           fill = factor(node), label = node)) +
  geom_sankey(flow.alpha = 0.6, node.color = "gray30") +
  geom_sankey_label(size = 3, color = "white", fill = "gray40") +
  scale_fill_viridis_d() +
  theme_sankey(base_size = 18) +
  labs(x = NULL) +
  theme(legend.position = "none",
    plot.title = element_text(hjust = .5))
Ben
  • 1,113
  • 10
  • 26