-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjls-gender-authors.R
141 lines (127 loc) · 4.8 KB
/
jls-gender-authors.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
#devtools::install_github("kalimu/genderizeR")
library(neo4r)
library(dplyr)
library(tidyr)
library(ggplot2)
library(genderizeR)
library(stringr)
# parse a .env file including variable expansion
parse_dotenv <- function(filepath=".env") {
lines <- readLines(filepath, warn = FALSE)
env_vars <- list()
for (line in lines) {
if (grepl("^#", line) || grepl("^$", line)) { next }
kv <- strsplit(line, "=")[[1]]
key <- kv[1]
value <- kv[2]
value <- sub("^['\"]", "", value)
value <- sub("['\"]$", "", value)
for (var_name in names(env_vars)) {
value <- gsub(paste0("\\$", var_name), env_vars[[var_name]], value)
value <- gsub(paste0("\\$\\{", var_name, "\\}"), env_vars[[var_name]], value)
}
env_vars[[key]] <- value
}
return(env_vars)
}
# not currently used
getNeo4jConnection <- function(config){
neo4j_api$new(
#url = paste0('http://', config$NEO4J_HOST, ':', config$NEO4J_HTTP_PORT),
url = 'http://localhost:7474',
user = config$NEO4J_USERNAME,
password = config$NEO4J_PASSWORD
)
}
get_author_year <- function(filename) {
read.csv(filename, encoding = "UTF-8") |>
filter(!is.na(author)) |>
filter(author != "") |>
separate_rows(author, sep = ";") |>
mutate(author = str_to_lower(str_trim(author))) |>
separate(author, into = c("first_name", "last_name"), sep = "(?<= )(?=[^ ]+$)", remove = TRUE) |>
mutate(across(c(first_name, last_name), str_trim))
}
get_given_names_db <- function(author_year, api_key) {
row_names <- c('count','name','gender','probability','country_id')
if (file.exists("data/jls-names-gender.Rdata")) {
load("data/jls-names-gender.Rdata")
}
if (exists("given_names_db")) {
missing_names <- author_year |>
filter(!first_name %in% given_names_db$name &
!str_detect(first_name, "^\\s*([a-zA-Z]\\.\\s*)+$")) |>
pull(first_name)
} else {
missing_names <- author_year$first_name
}
if (!exists("given_names_db") || length(missing_names) > 0) {
missing_given_names_db <- genderizeR::findGivenNames(missing_names, apikey = api_key)
colnames(missing_given_names_db) <- row_names # fix wrong column names
if (exists("given_names_db")) {
given_names_db <- rbind(given_names_db, missing_given_names_db)
} else {
given_names_db <- missing_given_names_db
}
save(given_names_db, file = "data/jls-names-gender.Rdata")
}
given_names_db
}
get_author_year_gender <- function(author_year, given_names_db) {
first_names_gender <- author_year |>
filter(!is.na(first_name)) |>
distinct(first_name) |>
pull(first_name) |>
genderizeR::genderize(genderDB = given_names_db,)
author_year |>
merge(first_names_gender, by.x="first_name", by.y="givenName") |>
select(last_name, first_name, year, gender) |>
arrange(last_name, first_name, year) |>
unique()
}
plotAuthorsGenderTimeseries <- function(year_gender, filename, title="", year_steps = 5) {
gender_by_year <- year_gender |>
group_by(year) |>
count(gender) |>
pivot_wider(names_from = gender, values_from = n, values_fill=0)
gender_by_year$sum <- rowSums(gender_by_year[2:3])
max_num_articles <- max(gender_by_year$sum)
gender_by_year$female_percent <- as.numeric(gender_by_year$female/(gender_by_year$male+gender_by_year$female) * max_num_articles)
breaks_years <- seq(min(gender_by_year$year), max(gender_by_year$year))
breaks_years <- breaks_years[breaks_years %% year_steps == 0]
p <- gender_by_year[, 1:3] |> pivot_longer(-year) |>
ggplot(aes(x=year,y=value,fill=name)) +
coord_fixed(ratio = 0.3) +
ggtitle(title) +
geom_bar(stat = 'identity') +
geom_line(
data = gender_by_year[,-2:-4],
mapping = aes(x=year, y=female_percent, group=1),
size=1, color="red", inherit.aes = FALSE
) +
geom_smooth(
data = gender_by_year[,-2:-4],
mapping = aes(x=year, y=female_percent, group=1),
size=1, color="orange", inherit.aes = FALSE,
method = "lm",
) +
scale_y_continuous(
sec.axis = sec_axis(
trans = ~. / (max_num_articles),
name = "Percentage of female authors",
labels = function(b) { paste0(round(b * 100, 0), "%")}
)
) +
scale_x_continuous(breaks = breaks_years) +
theme(axis.text.x=element_text(angle=90,hjust=1,vjust=0.5),
axis.text.y.right = element_text(color = "red")) +
labs(x="Year", y="Number of articles", fill='Gender')
ggsave(filename, dpi=600, width = 10)
p
}
config <- parse_dotenv()
# "Journal of Law and Society - Gender of article authors (Source: constellate.org)"
author_year <- get_author_year("data/jls-author-year-constellate.csv")
given_names_db <- get_given_names_db(author_year, config$GENDERIZER_API_KEY)
get_author_year_gender(author_year, given_names_db) |>
plotAuthorsGenderTimeseries("docs/article-fig-06.png")