Skip to content

Instantly share code, notes, and snippets.

@aezarebski
Created January 8, 2022 12:00
Show Gist options
  • Save aezarebski/766f36be4249cb7cd6b9bfcf88001524 to your computer and use it in GitHub Desktop.
Save aezarebski/766f36be4249cb7cd6b9bfcf88001524 to your computer and use it in GitHub Desktop.
Tool to summarise a .bib file
#!/usr/bin/env Rscript
#'
#' top-ten-0.1.0
#' =============
#'
#' This script prints a list of the top ten journals as ranked by the number of
#' references you have from each of them.
#'
#' Usage
#' -----
#'
#' ./top-ten.R -i references.bib -o journal-count.png
#'
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(argparse))
library(bib2df)
parser <- ArgumentParser()
parser$add_argument(
"-i",
"--input",
type = "character",
help = "Filepath to bibtex file."
)
parser$add_argument(
"-o",
"--output",
type = "character",
help = "Filepath of image file to make."
)
parser$add_argument(
"-n",
"--number",
type = "integer",
default = 15,
help = "Number of entries to show (defaults to 15)."
)
args <- parser$parse_args()
## x1 <- bib2df(file = "references.bib")
x1 <- bib2df(file = args$input)
x1 <- as.character(x1$JOURNAL)
x2 <- as.data.frame(table(x1))
x3 <- x2[order(x2$Freq, decreasing=T),]
names(x3) <- c("journal", "references")
x4 <- rbind(
head(x3, args$number),
data.frame(
journal = "other",
references = sum(tail(x3$references, -args$number))))
x4$journal <- factor(x = x4$journal, levels = rev(x4$journal))
g <- ggplot() +
geom_col(
data = x4,
mapping = aes(y = journal, x = references)
) +
geom_label(data = x4,
mapping = aes(y = journal, x = 1.1, label = references),
hjust = 0,
vjust = 0.5,
colour = "white",
fill = NA,
label.size = NA,
size = 6) +
scale_x_log10() +
ggtitle(
args$input,
subtitle = format(Sys.time(), "%d %b %Y")
) +
theme(axis.text.x = element_text(size = 20),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_text(size = 20),
title = element_text(size=30))
if (interactive()) {
print(g)
} else {
print.data.frame(x4, row.names = FALSE)
ggsave(
## filename = "journal-count.png",
filename = args$output,
plot = g,
height = 7.8,
width = 14.2
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment