I'm trying to plot the "flow" of individuals between two groups at certain timepoints.
In the following example:
d1=data.frame(c(1:6),
c("A","B","A","A","B","A"),
c("A","B","B","A","B","B")
)
names(d1)=c("id","time1","time2")
require(reshape2)
m1=melt(d1,id.vars="id")
require(ggplot2)
ggplot(m1,aes(x=variable,fill=value))+
geom_bar(stat="count",width = 0.5)
the individuals numbered 3 and 6 "switch" from group A to group B between observations "time1" and "time2". I am searching for a possibility to plot this. Tried geom_polygon but was not successful.
Here is what I like to get:
2 individuals staying in group A and 2 individuals switching to group 2.
A more complex example can be seen here: flow of voters between two elections.
[2] http://cdn1.salzburg24.at/2013/05/Waehlerstroeme-650x435.jpg
Is there a way to achieve this with ggplot?
Thanks aosmith!
This produced the result I expected:
require(alluvial)
d2=aggregate(d1[,2],
by= d1[, c('time1','time2')],
FUN=function(x) length(x))
names(d2)[3]="Freq"
alluvial(d2[,1:2],freq=d2$Freq)