Skip to content

Commit 63bd238

Browse files
committed
Add dplyr refs on filter and rowwise
1 parent 187bb60 commit 63bd238

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

R/Interactions_class.r

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ make_graph_from_csv <- function(file_path, delimiter = ";", column_name = "Autho
4949
# Create edges: pairwise combinations of co-authors
5050
edges <- data %>%
5151
dplyr::mutate(Authors = stringr::str_split(!!col_sym, delimiter)) %>%
52-
filter(purrr::map_int(Authors, length) > 1) %>% # Remove papers with less than 2 authors (no interaction can be inferred from them)
52+
dplyr::filter(purrr::map_int(Authors, length) > 1) %>% # Remove papers with less than 2 authors (no interaction can be inferred from them)
5353
dplyr::mutate(pairs = purrr::map(strsplit(!!col_sym, delimiter), ~combn(.x, 2, simplify = FALSE))) %>% # Generate all pairs of authors
5454
tidyr::unnest(pairs) %>%
5555
tidyr::unnest_wider(pairs, names_sep = "_") %>%
5656
dplyr::mutate(pairs_1 = trimws(pairs_1), pairs_2 = trimws(pairs_2)) %>% # Remove leading/trailing whitespaces
57-
rowwise() %>%
57+
dplyr::rowwise() %>%
5858
dplyr::mutate(Author1 = min(pairs_1, pairs_2), Author2 = max(pairs_1, pairs_2)) %>% # Order author pairs alphabetically
5959
dplyr::ungroup() %>%
6060
dplyr::count(Author1, Author2, name = "weight") # Count the frequency of each pair
@@ -139,3 +139,32 @@ get_diameter.Interactions <- function(interactions) {
139139
get_diameter <- function(interactions) {
140140
UseMethod("get_diameter", interactions)
141141
}
142+
143+
#' @export
144+
plot_graph.Interactions <- function(interactions) {
145+
grDevices::png(filename = "graph.png")
146+
comm <- cluster_louvain(interactions$graph)
147+
colors <- rainbow(length(unique(comm$membership)), alpha = 0.4)
148+
149+
centrality <- get_centrality(interactions)
150+
vertex_size <- 1 + (centrality / max(centrality)) * 15
151+
152+
par(mfrow = c(1, 1), mar = c(1, 1, 1, 1))
153+
plot(
154+
interactions$graph,
155+
vertex.label = NA,
156+
vertex.size = vertex_size,
157+
vertex.color = colors[comm$membership],
158+
mark.groups = split(1:vcount(interactions$graph), comm$membership),
159+
mark.col = colors,
160+
mark.border = NA,
161+
edge.width = 0.8,
162+
edge.color = "gray",
163+
layout = layout.fruchterman.reingold
164+
)
165+
dev.off()
166+
}
167+
168+
plot_graph <- function(interactions) {
169+
UseMethod("plot_graph", interactions)
170+
}

0 commit comments

Comments
 (0)