-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscls_loaned_borrowed_chord.R
250 lines (206 loc) · 7.22 KB
/
scls_loaned_borrowed_chord.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
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
# Setup ------------------------------------------------------------------------
library(circlize)
library(dplyr)
# https://jokergoo.github.io/circlize_book/book/
# http://jokergoo.github.io/circlize/reference/index.html
# Function: get_county ---------------------------------------------------
# return county label from library code
get_county <- function(letter_code) {
if(letter_code %in% c('ACL', 'ROM') ){
return('Adams')
}
else if(letter_code %in% c('CIA', 'COL', 'LDI', 'PAR', 'POR', 'POY', 'RAN',
'RIO', 'WID', 'WYO') ){
return('Columbia')
}
else if(letter_code %in% c('BLV', 'BER', 'CBR', 'CSP', 'DCL', 'MRS', 'DEE',
'DFT', 'FCH', 'MAR', 'MAZ', 'MCF', 'MID', 'MOO',
'MTH', 'ORE', 'STO', 'SUN', 'VER', 'WAU') ){
return('Dane')
}
else if(letter_code %in% c('MAD', 'HPB', 'HAW', 'LAK', 'MEA', 'MSB', 'PIN',
'SEQ', 'SMB') ){
return('Madison PL')
}
else if(letter_code %in% c('ALB', 'BRD', 'MRO', 'MNT', 'NGL') ){
return('Green')
}
else if(letter_code %in% c('AMH', 'ALM', 'PLO', 'ROS', 'STP') ){
return('Portage')
}
else if(letter_code %in% c('BAR', 'LAV', 'NOF', 'PLA', 'PDS', 'REE', 'RKS',
'SKC', 'SGR') ){
return('Sauk')
}
else if(letter_code %in% c('ARP', 'MFD', 'NEK', 'PIT', 'VES', 'MCM') ){
return('Wood')
}
else {
'other'
}
}
# Load & Prepare Data ----------------------------------------------------------
scls_flow_edges <- read.csv("loaned_borrowed_data.csv")
# filter out network loops (sender == receiver)
scls_flow_edges <-
scls_flow_edges[scls_flow_edges$from_library != scls_flow_edges$to_library, ]
# select network edges with daily item averages above
scls_flow_edges_avg20 <- scls_flow_edges[scls_flow_edges$daily_average>=20.0, ]
# data frame w/ receivers listed as senders
rcvrs_df <-
cbind.data.frame(
scls_flow_edges_avg20$to_library,
rep("-", length(scls_flow_edges_avg20$to_library)),
scls_flow_edges_avg20$count,
rep(0, length(scls_flow_edges_avg20$to_library))
)
# add matching column names
colnames(rcvrs_df) <- colnames(scls_flow_edges_avg20)
# combine data frames, group by senders, sort by county & total item count
scls_flow_edges_grouped <-
scls_flow_edges_avg20 %>%
bind_rows(rcvrs_df) %>%
group_by(from_library) %>%
summarise(total_count = sum(count)) %>%
mutate(county = sapply(from_library, get_county)) %>%
arrange(county, desc(total_count) )
# county labels for sending libraries after grouping
county_grouping <-
structure(
scls_flow_edges_grouped$county,
names = scls_flow_edges_grouped$from_library
)
# Plot Formatting Setup --------------------------------------------------------
# color sequence for library sectors (22 colors)
# sub-sequences run blue, yellow, red, green, purple, orange
sector_colors =
structure(
c("#0F4C81", "#E09F3E", "#9E2A2B", "#659E2A", "#7A306C", "#BB3E03",
"#4F6980", "#DB9E68", "#E07972", "#638B66", "#8175AA", "#F47942",
"#849DB1", "#BFBB60", "#C23D49", "#005500", "#3F1CC3", "#F45909",
"#030A8C", "#FCC30B", "#DC3080", "#743023"
),
names = scls_flow_edges_grouped$from_library
)
# formatting for county sectors
county_sector_formats <- list(
Adams = c(list(bg="#b6992d", txt="#FFFFFF")),
Columbia = c(list(bg="#79706e", txt="#FFFFFF")),
Dane = c(list(bg="#f28e28", txt="#FFFFFF")),
Green = c(list(bg="#59a14f", txt="#FFFFFF")),
"Madison PL" = c(list(bg="#4e79a7", txt="#FFFFFF")),
Portage = c(list(bg="#76448a", txt="#FFFFFF")),
Sauk = c(list(bg="#e15759", txt="#FFFFFF")),
Wood = c(list(bg="#499894", txt="#FFFFFF")),
Other = c(list(bg="#3C3B3B", txt="#FFFFFF"))
)
# Plot Chord Diagram -----------------------------------------------------------
# setup
par(bg='gray90', mar=c(0, 0, 0, 0), oma=c(0, 0, 2, 0))
# chord diagram set up
circos.clear()
circos.par(
track.margin = c(0.01, 0.01)
)
# draw chord diagram, group libraries by county
chordDiagram(
scls_flow_edges_avg20[ , c(1,2,4)],
grid.col = sector_colors,
annotationTrack = c('grid'),
annotationTrackHeight = 0.05,
preAllocateTracks = list(
list(track.height = mm_h(5)),
list(track.height = mm_h(10))
),
directional = T,
direction.type = c("diffHeight", "arrows"),
link.arr.type = "big.arrow",
link.sort = TRUE,
link.decreasing = TRUE,
group = county_grouping,
order = sort(scls_flow_edges_grouped$from_library, decreasing = T)
)
# sector text labels (3-letter codes)
circos.track(
track.index = 2, panel.fun = function(x, y) {
circos.text(
CELL_META$xcenter,
CELL_META$ylim[1],
CELL_META$sector.index,
facing = 'clockwise',
niceFacing = T, adj = c(0, 0.5)
)
}, bg.border = NA
)
# Outer Sectors: County Names --------------------------------------------------
# calculate span of county grouping sectors in degrees
county_spans <-
sapply(unique(county_grouping), function(county){
max(sapply(names(which(county_grouping==county)), function(library){
if(get.cell.meta.data("cell.start.degree", library)!=0){
get.cell.meta.data("cell.start.degree", library)
}else{
360.0
}
})
)-min(sapply(names(which(county_grouping==county)), function(library){
get.cell.meta.data("cell.end.degree", library)
})
)
}
)
# highlight counties with enough libraries to fit county name on outer sector
highlight_counties <- names(which(county_spans > 20))
# all other libraries
other_sect <- names(county_grouping[which(!county_grouping %in% highlight_counties)])
# county highlight sectors
for (sect in highlight_counties){
highlight.sector(
names(which(county_grouping==sect)),
track.index = 1,
col = county_sector_formats[[sect]]$bg,
text = toupper(sect),
text.col = county_sector_formats[[sect]]$txt,
cex = 0.8,
facing = 'bending.inside',
niceFacing = T,
text.vjust = 0.3,
font = 2
)
}
# other library highlight sector
highlight.sector(
other_sect,
track.index = 1,
col = county_sector_formats$Other$bg,
text = "OTHER",
text.col = county_sector_formats$Other$txt,
cex = 0.8,
facing = 'bending.inside',
niceFacing = T,
text.vjust = 0.3,
font = 2
)
# plot title & subtitle
# https://stackoverflow.com/a/55059687
# https://www.r-graph-gallery.com/74-margin-and-oma-cheatsheet.html
mtext(
"Daily Average Items Loaned",
side=3, line=1, at=-1, adj=0, cex=1.5, font=2
)
mtext(
"Libraries lending more than 20 items/day",
side=3, line=0, at=-1, adj=0, cex=1
)
#' Notes & Misc. -----------------------------------------------------------
#circos.info()
#get.all.sector.index()
# DONE: change sector/link colors - automatic selection
# DONE: label county groupings
# DONE: change order of sectors w/in groups - ? dplyr sort from_lib by sum of counts ?
# DONE: change color sequence
# DONE: orient labels w/ spacing; horizontal/vertical?
# DONE: add plot title, subtitle (no caption?)
# skip: add interactivity with Shiny? tootips on hover would be really helpful
# DONE: create highlight sectors automatically with loop? use "sapply" instead
# TODO: assign bg and txt colors to county sectors