-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_data_coauthors.R
132 lines (101 loc) · 4.17 KB
/
make_data_coauthors.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
library(dplyr)
library(stringr)
library(scholar)
library(igraph)
library(ggraph)
library(see)
library(tidygraph)
# Functions --------------------
find_coauthors <- function(id) {
df <- scholar:::list_coauthors(id, Inf)
df$id <- scholar:::grab_id(df$coauthors_url)
df[c("author", "coauthors", "id")]
}
list_coauthors <- function(df, sleep = 0, silent = FALSE) {
data <- data.frame()
for (i in 1:nrow(df)) {
if (silent == FALSE) {
cat(paste0(round(i / nrow(df) * 100, 2), "%\n"))
}
Sys.sleep(runif(1, 1, sleep))
if (!df$coauthors[i] %in% unique(df$author)) {
data <- rbind(data, find_coauthors(df$id[i]))
}
}
data
}
get_coauthors <- function(id = "bg0BZ-QAAAAJ", n_deep = 1, sleep = 3, silent = FALSE) {
stopifnot(is.numeric(n_deep), length(n_deep) >= 1, n_deep != 0)
df <- find_coauthors(id)
if (n_deep > 0) {
for (level in 1:n_deep) {
cat(paste0("level ", level, ":\n"))
df <- rbind(df, list_coauthors(df, sleep = sleep, silent = silent))
}
}
df$author <- stringr::str_to_title(df$author)
df$coauthors <- stringr::str_to_title(df$coauthors)
df <- df[!df$author %in% c("Sort By Citations", "Sort By Year", "Sort By Title"), ]
df <- df[!df$coauthors %in% c("Sort By Citations", "Sort By Year", "Sort By Title"), ]
df[c("author", "coauthors")]
}
create_graph <- function(data) {
data |>
tidygraph::as_tbl_graph(directed = FALSE) |>
dplyr::filter(name != "") |>
dplyr::mutate(
closeness = tidygraph::centrality_closeness(normalized = TRUE),
degree = tidygraph::centrality_degree(normalized = TRUE)
) |>
tidygraph::activate(edges) |>
dplyr::mutate(
importance = tidygraph::centrality_edge_betweenness(),
group = as.factor(from)
) |>
tidygraph::activate(nodes) |>
dplyr::filter(!name %in% c("Sort By Citations", "Sort By Year", "Sort By Title")) |>
dplyr::mutate(
name = stringr::str_remove(name, ",.*"),
# Other groupings: group_edge_betweenness(), group_walktrap(), group_spinglass(), group_louvain()
group = as.factor(tidygraph::group_edge_betweenness())
) |>
as.list()
}
# Get data --------------------------------
# Scrap data from google scholar
data <- get_coauthors("bg0BZ-QAAAAJ", n_deep = 2, sleep = 15)
# Save data so that it can be re-used
data |>
filter(!coauthors %in% c("About Scholar", "Search Help")) |>
filter(!author %in% c("About Scholar", "Search Help")) |>
write.csv("data/data_network.csv", row.names = FALSE)
# Process data ------------------------------------------------------------
data <- read.csv("data/data_network.csv")
# Prune
# 1. Find direct relations of DM
data1 <- data[(data$author == "Dominique Makowski" | data$coauthors == "Dominique Makowski"), ]
firstlevel <- unique(c(data1$author, data1$coauthors))
# 2. Find direct relations of these first-level co-authors
data2 <- data[(data$author %in% firstlevel | data$coauthors %in% firstlevel), ]
secondlevel <- unique(c(data2$author, data2$coauthors))
# 3. Find whether these have also coauthors in the list to link them between them
data3 <- data[(data$author %in% secondlevel & data$coauthors %in% secondlevel) |
(data$author %in% secondlevel & data$coauthors %in% secondlevel), ]
# Make plot --------------------------------
# Plot
data_graph <- create_graph(data=data3)
p <- tidygraph::tbl_graph(nodes = data_graph$nodes, edges = data_graph$edges, directed = FALSE) |>
ggraph::ggraph(layout = "nicely") + # fr, kk, nicely, lgl, graphopt, dh
ggraph::geom_edge_arc(aes(alpha = importance), show.legend = FALSE, strength = 0.1) +
ggraph::geom_node_point(aes(size = degree, colour = group), show.legend = FALSE) +
ggraph::geom_node_text(aes(label = name, size = degree), repel = TRUE, check_overlap = TRUE, show.legend = FALSE, max.overlaps = 20) +
# ggraph::geom_node_label(aes(label = name, size = degree), repel = TRUE, show.legend = FALSE) +
ggraph::theme_graph() +
scale_size_continuous(range = c(2, 6)) +
scale_edge_alpha_continuous(range = c(0.1, 0.8)) +
# scale_color_viridis_d() +
see::scale_color_material_d(palette = "rainbow", reverse = TRUE)
# Show plot
p
# Save
ggsave("img/collaboration_network.png", p, dpi = 500, width = 10, height = 10)