Skip to content

Instantly share code, notes, and snippets.

@adam3smith
Created September 27, 2024 18:58
Show Gist options
  • Save adam3smith/25d9eb89c217b432b8eefb91e60a21e7 to your computer and use it in GitHub Desktop.
Save adam3smith/25d9eb89c217b432b8eefb91e60a21e7 to your computer and use it in GitHub Desktop.
# Setup and Functions -----------------------------------------------------
library(httr2)
library(jsonlite)
library(dplyr)
library(ggplot2)
journals = c("AJPS", "APSR", "BJPS", "CPS", "EJIR", "EJPR", "IO", "ISQ", "JOP", "JCR")
issns = c("1540-5907", "0003-0554", "1469-2112", "0010-4140", "1354-0661", "1475-6765", "0020-8183", "1468-2478", "1468-2508", "1552-8766")
years = c("2020", "2021", "2022", "2023")
# Returns a list with totalArticles and oaArticles from CrossRef API
# Input is ISSN and Year, both as a string
getOAArticles = function(issn, year) {
dateString = paste0('from-pub-date:', year, '-01-01,until-pub-date:', year, '-12-31')
# Construct API Request
journalData = httr2::request("https://api.crossref.org/journals/") |>
req_url_path_append(issn) |> req_url_path_append("works") |>
req_url_query(`filter`=dateString, `facet`='license:*') |>
req_perform() |>resp_body_json()
totalArticles = journalData$message$`total-results`
licenseFacet = journalData$message$facets$license$values
# sum up all creativecommons license
oaArticles = sum(unlist(licenseFacet[grepl("creativecommons.org", names(licenseFacet))]))
return (list("totalArticles" = totalArticles, "oaArticles" = oaArticles))
}
# Data retrieval ----------------------------------------------------------
data = data.frame(matrix(ncol =5, nrow = 0))
colnames(data) = c("journal", "issn", "year", "totalArticles", "oaArticles" )
for (i in 1:length(years)) {
year = years[i]
for (j in 1:length(issns)) {
journal = journals[j]
issn = issns[j]
articles = getOAArticles(issn, year)
totalArticles = articles$totalArticles
oaArticles = articles$oaArticles
data = rbind(data, data.frame("journal" = journal, "issn" = issn, "year" = year, "totalArticles" = totalArticles, "oaArticles" = oaArticles))
Sys.sleep(2)
print(paste0("Done with ", journal, ", ", year))
}
}
# Clean and plot ----------------------------------------------------------
# Drop JOP, which has no crossref data
data = data |> filter(journal != "JOP")
data$oaPercentage = (data$oaArticles / data$totalArticles) * 100
# ggplot code with chatGPT assistance
# 2023 bar graph
data |> filter(year == "2023") |> ggplot(aes(x = journal, y = oaPercentage, fill = journal)) +
geom_bar(stat = "identity", width = 0.7) +
labs(title = "Share of OA Articles by Journal in 2023",
x = "Journal",
y = "Percentage of OA Articles (%)") +
theme_minimal(base_size = 15) + # Clean theme with larger base font size
theme(
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5),
axis.text.y = element_text(size = 12),
plot.title = element_text(hjust = 0.5, size = 18, face = "bold"),
legend.position = "none" # Remove legend for journal names
) +
scale_fill_brewer(palette = "Set3") # Add nice color palette for the bars
# All years
ggplot(data, aes(x = journal, y = oaPercentage, fill = as.factor(year))) +
geom_bar(stat = "identity", position = position_dodge(width = 0.7), width = 0.7) + # Group bars by journal
geom_text(aes(label = year),
position = position_dodge(width = 0.7), hjust = 1.5, size = 3.5, angle = 90) + # Add labels for years
labs(title = "Share of OA Articles by Journal (2020-2023)",
x = "Journal",
y = "Percentage of OA Articles (%)",
fill = "Year") +
theme_minimal(base_size = 15) + # Clean theme with larger base font size
theme(
axis.text.x = element_text(angle = 45, hjust = 1, size = 12), # Tilt journal names for better readability
axis.text.y = element_text(size = 12),
legend.position = "none",
plot.title = element_text(hjust = 0.5, size = 18, face = "bold"),
) +
scale_fill_brewer(palette = "Set3") # Consistent colors for the years
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment