x轴上带有时间的过渡(Sankey)图 [英] Transition (Sankey) plot with time on x axis

查看:114
本文介绍了x轴上带有时间的过渡(Sankey)图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个转换矩阵,如下所示:

I have a transition matrix as following:

1.  A  A  B
2.  B  C  A
3.  A  C  C

其中每一列代表句点,每一行代表一个代理,每个字母代表一个状态.我想创建一个图,例如 Sankey图,该图显示了州中从州到州的过渡每个时期. 特工的身份并不重要.
所以我想有一个这样的情节:

where each column represents periods,each row represents an agent and each letter represents a state. I would like a create a plot such as Sankey Diagram which shows transitions from states to states in each period. Agents' identities are not important.
So I would like to have a plot like this:

.
似乎我可以使用networkD3或googleVis软件包.但是,由于每个节点的位置都是由程序包内部确定的,因此我不知道如何将时间方面放在X轴上.

.
It seems that I can use networkD3 or googleVis packages. However since the position of each node is endogenously determined by the packages, I have no clue how to put the time aspect on X axis.

我们将不胜感激任何帮助或其他可视化建议, 提前谢谢,

Any help or alternative visualization suggestions will be highly appreciated, Thanks a lot in advance,

您可以通过以下方式重现样本数据:

You can reproduce the sample data by:

    transitiondata <- data.frame("t1"=c("A","B","A"),
                                 "t2"=c("A","C","C"),
                                 "t3"=c("B","A","C"))

推荐答案

未来的自我解答:在这段时间内开发了非常适合该任务的ggalluvial软件包.为了使用它,我们需要提供整洁的数据.

Self-answering from the future: ggalluvial package, which is perfect for this task, was developed during that time. In order to use it, we need to provide tidy data.

让我们加载我们需要的库:

Let's load the libraries we need:

library(ggplot2)
library(ggalluvial)
library(tidyr)
library(dplyr

然后需要先为数据创建标识符,然后才能将其转换为整洁的格式.所以新数据是这样的:

Then need to create identifiers for the data before we convert it to tidy format. So the new data is like this:

transitiondata$id <- c("id1","id2","id3")

转换为整齐的格式

transitiondata_tidy <- transitiondata %>% 
                           gather(time, state, t1,t2,t3) %>% 
                           mutate(time = as.factor(time), state = as.factor(state))

这是我们的数据的样子:

Here is how our data looks like:

   id time state
1 id1   t1     A
2 id2   t1     B
3 id3   t1     A
4 id1   t2     A
5 id2   t2     C
6 id3   t2     C
7 id1   t3     B
8 id2   t3     A
9 id3   t3     C

ggplot2ggalluvial可以达到目的:

ggplot(transitiondata_tidy,
       aes(x = time, stratum = state, alluvium = id, fill = state, label = state)) +
  geom_stratum() +
  geom_text(stat = "stratum", size = 3) +
  geom_flow(fill = "darkgrey", color = "black")

我们的过渡(Sankey)图已准备就绪:

And our transition (Sankey) plot is ready:

这篇关于x轴上带有时间的过渡(Sankey)图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆