使用具有不同值的节点的ggalluvial

发布于 2025-02-07 11:35:55 字数 1420 浏览 4 评论 0原文

我的数据是由人员完成的一组活动。一个人进行的活动序列各不相同。下面的数据显示了每个步骤(步骤1,Step2等)的活动。我想要一个在每个步骤中标记活动(每个节点1、2、3 ...)的冲积图,最好的方法是什么?到目前为止,这是我所拥有的:

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")

第一个

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌梦深 2025-02-14 11:35:55

如果您以此顺序有数据(每列都是一组不同的活动),请使用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))

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文