问题
I have a transition matrix as following:
1. A A B
2. B C A
3. A C C
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:
.
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"))
回答1:
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")
Convert to tidy format
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
And ggplot2
and ggalluvial
does the trick:
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")
And our transition (Sankey) plot is ready:
来源:https://stackoverflow.com/questions/45936144/transition-sankey-plot-with-time-on-x-axis