-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathggnetwork_gganimate.R
107 lines (68 loc) · 2.68 KB
/
ggnetwork_gganimate.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
detach(package:sna)
detach(package:network)
detach(package:igraph)
library(igraph)
library(dplyr)
library(ggnetwork)
library(gganimate)
library(ggplot2)
### ggnetwork + gganimate
N=120
set.seed(140)
g <- sample_forestfire(N, fw.prob=0.3,bw.factor=.9)
V(g)$Group <- cluster_edge_betweenness(g)$membership
df<-ggnetwork(g,arrow.gap=0,layout = "fruchtermanreingold", cell.jitter = 0.05)
df1 <- df[(N+1):nrow(df),]
results=NULL
for(i in 1:nrow(df1)){
results[[i]] <- df1[1:i,]
}
df2 <- do.call('rbind', Map(cbind, results, time=1:nrow(df1)))
## need to find which time each node first appears and then add an NA for that node from that time onwards
# this could be tidied up a bit
library(dplyr)
head(df2)
mintimes<-rbind(
df2 %>% group_by(x,y) %>% summarise(time=min(time)),
df2 %>% group_by(xend,yend) %>% summarise(time=min(time)) %>% rename(x=xend,y=yend)
) %>% group_by(x,y) %>% summarise(time=min(time)) %>% ungroup() %>% as.data.frame
dfx <- as.data.frame.matrix(df[1:N,])
mintimes$x<-as.numeric(mintimes$x)
mintimes$y<-as.numeric(mintimes$y)
nodetimes <- dfx %>% left_join(mintimes)
#expand rows to fill up to end of time
mt<-max(df2$time)
df.expanded <- nodetimes[rep(row.names(nodetimes), (mt-nodetimes$time)), 1:9]
head(df.expanded,20)
df.expanded1 <- df.expanded %>% group_by(vertex.names) %>% mutate(time = time + row_number() - 1)
## bind back
df3 <- rbind(df.expanded1, df2)
p=ggplot(df3, aes(x = x, y = y, xend = xend, yend = yend, frame=time)) +
geom_edges(color = "black") +
geom_nodes(aes(color = factor(Group)), size = 8) +
geom_nodetext(aes(label = vertex.names),color="black", fontface = "bold") +
theme_blank() +
theme(legend.position="none") +
guides(color=guide_legend(keyheight=0.3,default.unit="inch",override.aes = list(size=6)))
animation::ani.options(interval=0.25)
gg_animate(p, 'animation1.mp4', title_frame = FALSE)
## Animation 3.
# take df3 from above
head(df3)
df3.sp <- split(df3, df3$time)
#setwd for output
setwd("C:/Users/curley1/Dropbox/Work/R/RMeetup/presentation/gists/gistimgsvg1")
#Time loop starts
for(i in 1:max(df3$time)){
tmp<-rbind(df3.sp[[i]],data.frame(x=NA,y=NA,Group=1:max(df3$Group),na.x=NA,vertex.names=NA,xend=NA,yend=NA,na.y=NA,time=NA))
pp <- ggplot(tmp, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "black") +
geom_nodes(aes(color = factor(Group)), size = 8) +
geom_nodetext(aes(label = vertex.names),color="black", fontface = "bold") +
theme_blank() +
xlim(0,1)+ylim(0,1) +
theme(legend.position="none") +
guides(color=guide_legend(keyheight=0.3,default.unit="inch",override.aes = list(size=6)))
ggsave(pp,filename=paste0("output_",i,".png", sep=""))
}
dev.off()