-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.R
185 lines (168 loc) · 6.45 KB
/
app.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
pacman::p_load(
rio,
here,
igraph,
tidygraph,
ggraph,
janitor,
bslib,
shiny,
tidyverse,
viridis
)
result_df <- import(here("notebooks", "cosine_test_31 Oct.csv"))
ui <- page_sidebar(
title = "Network Analysis Visualization",
sidebar = sidebar(
selectInput("centrality_method", "Centrality Measure:",
choices = c("Degree" = "degree",
"Betweenness" = "betweenness",
"Closeness" = "closeness"),
selected = "betweenness"),
selectInput("community_method", "Community Detection:",
choices = c("Cosine Similarity" = "cosine",
"Infomap" = "infomap",
"Label Propagation" = "label_prop",
"Leading Eigenvector" = "leading_eigen"),
selected = "infomap"),
selectInput("layout_type", "Graph Layout:",
choices = c("Fruchterman-Reingold" = "fr",
"Kamada-Kawai" = "kk",
"Stress" = "stress",
"Circle" = "circle",
"Grid" = "grid")),
numericInput("small_cluster_threshold",
"Small Cluster Threshold:",
value = 5,
min = 1),
hr(),
helpText("Select different methods to analyze network centrality and community structure")
),
card(
card_header("Network Visualization"),
plotOutput("network_plot", height = "800px")
)
)
server <- function(input, output, session) {
get_cluster_colors <- function(n) {
viridis(n, option = "D")
}
network_data <- reactive({
# First create nodes dataframe with cluster information
nodes_df <- result_df %>%
select(Cause_category, Cause_cluster) %>%
rename(name = Cause_category, cluster = Cause_cluster) %>%
bind_rows(
result_df %>%
select(Effect_category, Effect_cluster) %>%
rename(name = Effect_category, cluster = Effect_cluster)
) %>%
# Keep original name and cluster before transformation
mutate(
original_name = name,
original_cluster = cluster,
name = case_when(
str_detect(name, "transmission|spread") ~ "disease transmission",
TRUE ~ name
)
) %>%
# Group by the new name and keep all unique clusters
group_by(name) %>%
mutate(
all_clusters = paste(sort(unique(original_cluster)), collapse = "_")
) %>%
ungroup() %>%
# Keep one row per unique name-cluster combination
distinct(name, all_clusters, .keep_all = TRUE) %>%
filter(!name %in% c("No content", "No context"))
# Create edges dataframe
edges_df <- result_df %>%
select(Cause_category, Effect_category) %>%
filter(!Cause_category %in% c("No content", "No context")) %>%
mutate(across(c(Cause_category, Effect_category),
~case_when(
str_detect(., "transmission|spread") ~ "disease transmission",
TRUE ~ .
)))
# Create graph with node attributes
network_recode1 <- tbl_graph(nodes = nodes_df,
edges = edges_df,
directed = TRUE)
# Calculate centrality and community
network_recode1 <- network_recode1 %>%
mutate(
centrality = case_when(
input$centrality_method == "degree" ~ centrality_degree(),
input$centrality_method == "betweenness" ~ centrality_betweenness(),
input$centrality_method == "closeness" ~ centrality_closeness()
),
community = case_when(
input$community_method == "cosine" ~ as.factor(all_clusters),
input$community_method == "infomap" ~ as.factor(group_infomap()),
input$community_method == "label_prop" ~ as.factor(group_label_prop()),
input$community_method == "leading_eigen" ~ as.factor(group_leading_eigen())
)
)
# Only apply small cluster threshold for non-cosine methods
if(input$community_method != "cosine") {
network_recode1 <- network_recode1 %>%
mutate(
community = if_else(
community %in% names(which(table(community) <= input$small_cluster_threshold)),
"Unclassified",
"Cluster"
),
community = as.factor(community)
)
}
network_recode1 %>%
activate(edges) %>%
mutate(edge_type = if_else(.N()$centrality[from] > 3,
"link to endpoint",
"link between drivers"))
})
cluster_colors <- reactive({
g <- network_data()
if(input$community_method == "cosine") {
unique_communities <- sort(unique(as.character(V(g)$community)))
n_communities <- length(unique_communities)
colors <- get_cluster_colors(n_communities)
names(colors) <- unique_communities
colors
} else {
c("Unclassified" = "#1b9e77", "Cluster" = "#7570b3")
}
})
output$network_plot <- renderPlot({
g <- network_data()
centrality_scaled <- scales::rescale(V(g)$centrality, to = c(0.3, 1))
ggraph(g, layout = input$layout_type) +
geom_edge_link(aes(color = edge_type),
arrow = arrow(length = unit(4, 'mm'),
type = "closed"),
end_cap = circle(2, 'mm'),
alpha = 0.7,
edge_width = 1) +
geom_node_point(aes(size = centrality,
color = community),
show.legend = TRUE) +
geom_node_text(aes(label = name,
size = centrality,
alpha = centrality),
repel = TRUE) +
scale_color_manual(values = cluster_colors(),
name = "Cluster") +
scale_edge_color_manual(values = c("link between drivers" = "gray",
"link to endpoint" = "#d95f02")) +
scale_size(range = c(3, 10)) +
scale_alpha(range = c(0.3, 1)) +
theme_graph() +
labs(title = paste("Network Analysis using",
tools::toTitleCase(input$centrality_method),
"centrality and",
tools::toTitleCase(input$community_method),
"community detection")) +
guides(alpha = "none")
})
}
shinyApp(ui, server)