-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsankey.R
More file actions
292 lines (233 loc) · 10.3 KB
/
Copy pathsankey.R
File metadata and controls
292 lines (233 loc) · 10.3 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
library(tidyverse)
library(patchwork)
library(MetBrewer); library(MoMAColors)
set.seed(81)
setwd("~/Documents/Github/pttp-evidence")
statements <- read_csv("statements.csv")
statements %>%
rename(SpeakerType = `Speaker Category`, CitationType = `Category (from Citation)`) -> statements
statements$Speaker <- NA
statements$Speaker[statements$SpeakerType == 'Expert Resource'] <- statements$Individuals[statements$SpeakerType == 'Expert Resource']
statements$Speaker[!(statements$SpeakerType == 'Expert Resource')] <- statements$Organization[!(statements$SpeakerType == 'Expert Resource')]
# Ask this BEFORE separating
prop.table(table(statements$SpeakerType, statements$`Self-citation`), margin = 1)
# separate speakers
statements -> statements.before
statements %>%
separate_rows(Speaker, sep = ',') -> statements
prop.table(table(statements$SpeakerType, statements$`Self-citation`), margin = 1)
### Clean up self-citation and long names
statements %>%
mutate(`Self-citation` = recode(`Self-citation`, !!!c('Individual advisor' = "Individual author"))) ->
statements
statements %>%
mutate(Citation = recode(Citation, !!!c("European Commission Joint Research Centre 2024" = "ECJRC 2024")),
Speaker = recode(Speaker, !!!c("International Disability Alliance" = "Int'l Disability Alliance"))) -> statements
### refactor some things real fast
statements %>%
select(Speaker, SpeakerType) %>%
arrange(SpeakerType, Speaker) %>%
pull(Speaker) %>% unique() -> speaker.levels
statements %>% mutate(Speaker = factor(Speaker, levels = speaker.levels)) -> statements
statements %>%
select(Citation, CitationType) %>%
arrange(CitationType, Citation) %>%
pull(Citation) %>% unique() -> citation.levels
statements %>% mutate(Citation = factor(Citation, levels = citation.levels)) -> statements
statements %>%
mutate(CitationType = recode(CitationType, !!!c("Journal Article" = "Journal Article, Preprint,\n or Academic Book",
"Preprint" = "Journal Article, Preprint,\n or Academic Book",
"Book" = "Journal Article, Preprint,\n or Academic Book",
"Report or White Paper" = "Technical Report,\n White Paper, or Blog",
"Editorial or Blog" = "Technical Report,\n White Paper, or Blog",
"\"IO Guidance, Strategy, or Tool\"" = "International Organization\nGuidance, Strategy, or Tool"))) %>%
mutate(CitationType = factor(CitationType, levels = c("Journal Article, Preprint,\n or Academic Book",
"Technical Report,\n White Paper, or Blog",
"International Organization\nGuidance, Strategy, or Tool"))) %>%
mutate(SpeakerType = factor(SpeakerType, levels = c('Expert Resource','Stakeholder','Member State'))) -> statements
### Sankey
library(ggalluvial)
statements %>%
select(SpeakerType, Speaker) %>% distinct() %>%
pull(SpeakerType) %>% table() -> levs1
statements %>%
select(CitationType, Citation) %>% distinct() %>%
pull(CitationType) %>% table() -> levs2
cols <- rev(c("#BD7C96FF", "#D97669FF", "#EEC7A0FF", "#C5EDE1", "lightblue", "#B5BB8A"))
statements %>%
group_by(SpeakerType, CitationType) %>%
summarize(Count = n()) %>%
ggplot(aes(axis1 = SpeakerType, axis2 = CitationType, y = Count)) +
geom_alluvium(aes(fill = `SpeakerType`), width = 0.5) +
scale_fill_manual(values = c("gray80",'gray50','black')) +
ggnewscale::new_scale_fill() +
geom_stratum(size = 0.4, width = 0.5) +
scale_fill_manual(values = cols) +
geom_text(stat = "stratum",
aes(label = after_stat(stratum)),
size = 3.4) +
theme_void() +
theme(legend.position = 'none') -> g1bw; g1bw
statements %>%
group_by(SpeakerType, CitationType) %>%
summarize(Count = n()) %>%
ggplot(aes(axis1 = SpeakerType, axis2 = CitationType, y = Count)) +
geom_alluvium(aes(fill = `SpeakerType`), width = 0.5) +
scale_fill_manual(values = cols[1:3]) +
ggnewscale::new_scale_fill() +
geom_stratum(aes(fill = after_stat(stratum)),
size = 0.4, width = 0.5) +
scale_fill_manual(values = cols) +
geom_text(stat = "stratum",
aes(label = after_stat(stratum)),
size = 3.4) +
theme_void() +
theme(legend.position = 'none') -> g1color; g1color
statements2 <- statements
statements2 %>%
select(Speaker, SpeakerType) %>%
arrange(SpeakerType, Speaker) %>%
pull(Speaker) %>% unique() -> speaker.levels
statements2 %>% mutate(Speaker = factor(Speaker, levels = speaker.levels)) -> statements2
statements2 %>%
select(Citation, CitationType) %>%
arrange(CitationType, Citation) %>%
pull(Citation) %>% unique() -> citation.levels
statements2 %>% mutate(Citation = factor(Citation, levels = citation.levels)) -> statements2
statements2 %>%
select(SpeakerType, Speaker) %>% distinct() %>%
pull(SpeakerType) %>% table() -> levs1
statements2 %>%
select(CitationType, Citation) %>% distinct() %>%
pull(CitationType) %>% table() -> levs2
cols <- rev(c("#BD7C96FF", "#D97669FF", "#EEC7A0FF", "#C5EDE1", "lightblue", ""))
cols <- c(rep("#B5BB8A", levs1[1]),
rep('lightblue', levs1[2]),
rep("#C5EDE1", levs1[3]),
rep("#EEC7A0FF", levs2[1]),
rep("#D97669FF", levs2[2]),
rep("#BD7C96FF", levs2[3])
)
statements2 %>%
group_by(SpeakerType, Speaker, CitationType, Citation, `Self-citation`) %>%
summarize(Count = n()) %>%
ggplot(aes(axis1 = Speaker, axis2 = Citation, y = Count)) +
geom_alluvium(aes(fill = `Self-citation`), alpha = 0.85, width = 0.5) +
scale_fill_manual(values = c("#B5BB8A",'gray87',"lightblue3")) +
ggnewscale::new_scale_fill() +
geom_stratum(aes(fill = after_stat(stratum)),
size = 0.4, width = 0.5) +
scale_fill_manual(values = cols) +
geom_text(stat = "stratum",
aes(label = after_stat(stratum)),
min.y = 5,
size = 3) +
theme_void() +
theme(legend.position = 'none') -> g2color; g2color
statements2 %>%
group_by(SpeakerType, Speaker, CitationType, Citation, `Self-citation`) %>%
summarize(Count = n()) %>%
ggplot(aes(axis1 = Speaker, axis2 = Citation, y = Count)) +
geom_alluvium(aes(fill = `Self-citation`), width = 0.5) +
scale_fill_manual(values = c("black",'gray80',"black")) +
ggnewscale::new_scale_fill() +
geom_stratum(size = 0.4, width = 0.5) +
scale_fill_manual(values = cols) +
geom_text(stat = "stratum",
aes(label = after_stat(stratum)),
min.y = 5,
size = 3) +
theme_void() +
theme(legend.position = 'none') -> g2bw; g2bw
g1color + g2color + plot_annotation(tag_levels = 'A', tag_prefix = "(", tag_suffix = ")") & theme(plot.margin = unit(c(0,0.5,0,0.5),'cm'), plot.tag = element_text(size = 16, hjust = 0, vjust = 0), plot.tag.position = c(-0.05, 0.93))
# g1bw + g2bw + plot_annotation(tag_levels = 'A', tag_prefix = "(", tag_suffix = ")") & theme(plot.margin = unit(c(0,0.5,0,0.5),'cm'), plot.tag = element_text(size = 16, hjust = 0, vjust = 0), plot.tag.position = c(-0.05, 0.93))
ggsave("Figure 1.pdf", width = 13, height = 10)
### Network
library(igraph); library(ggraph); library(tidygraph)
library(fs); library(magrittr)
### 81, 53, 46, 43, 37, 26, *****22
### 300, 269, 258, 234, 231, 220, 204
### 199, 198, 174, 151, 150, 105
###
statements.before %>%
group_by(Citation) %>% summarize(Before = n()) -> Before
statements %>%
group_by(Citation) %>% summarize(After = n()) -> Fix
Fix %>% left_join(Before) -> Fix
# set.seed(81)
## read in host-virus pairs -----
Edges <- statements %>%
rename(Mosquito = Speaker, Virus = Citation) %>%
group_by(Mosquito, Virus) %>% summarize(n = n()) %>%
group_by(Virus) %>%
mutate(nVirus=sum(n)) %>%
ungroup() %>%
group_by(Mosquito) %>%
mutate(nMosquito=sum(n)) %>%
ungroup()
# Implement duplication fix
for (i in 1:nrow(Edges)) {
Edges$nVirus[i] <- Fix$Before[Fix$Citation==Edges$Virus[i]]
}
## theme -----
pal <- MetBrewer::met.brewer('Kandinsky')
## unipartite network ----
NodesV <- Edges %>% select(Virus, nVirus) %>%
rename(n=nVirus, name=Virus) %>%
mutate(NodeCategory="Virus") %>%
distinct # keep one row per virus / count - 35 unique
NodesM <- Edges %>% select(Mosquito, nMosquito) %>%
rename(n=nMosquito, name=Mosquito) %>%
mutate(NodeCategory="Mosquito") %>%
distinct # keep one row per vector / count - 122 unique
Nodes <- bind_rows(NodesM, NodesV) %>%
as.data.frame() %>%
mutate(TextSize=sqrt(n)*5) %>% as.data.frame()
Edges %>%
select(Mosquito:n) %>%
rename(from=Mosquito, to=Virus, weights=n) %>%
graph_from_data_frame(., directed = F, Nodes) -> G
layout = create_layout(G, layout = "dh") %>%
mutate(name = ifelse(n > 3, name, NA))
cols <- MetBrewer::met.brewer("Cross",5); cols
ggraph(layout) +
geom_edge_link(aes(width=weights), alpha=0.5, edge_colour="gray") +
geom_node_point(aes(size=n,
#shape=NodeCategory,
#alpha=n,
fill=NodeCategory),
shape=21,
colour="white",
alpha=0.7) +
geom_node_text(aes(label=name,
colour=NodeCategory,
size = TextSize),
repel=TRUE,
show.legend = F,
alpha=1) +
theme_graph() +
scale_size(c(0,6))+
scale_colour_manual(values=c(cols[[4]],cols[[2]])) +
scale_fill_manual(values=c(cols[[4]],cols[[2]])) +
scale_edge_width(range = c(0.4, 5)) + # control edge width
theme(legend.position = "none") -> Net; print(Net)
#
# G %>% as_tbl_graph %>%
# ggraph(layout) +
# geom_edge_link(aes(width=weights), alpha=0.6, edge_colour="grey") +
# geom_node_point(aes(size=n,
# #shape=NodeCategory,
# #alpha=n,
# fill=NodeCategory),
# shape=21,
# colour= "black",
# alpha=0.8) +
# geom_node_text(aes(label = name, size=TextSize,
# colour=NodeCategory),
# repel=TRUE,
# show.legend = F) +
# theme_graph() +
# scale_colour_manual(values=pal[2:1]) +
# scale_fill_manual(values=pal[2:1]) +
# scale_edge_width(range = c(0.3, 6)) -> NetLegend
# Network only