Created
February 2, 2026 12:42
-
-
Save rguliev/fb79b08bbffc8a28bb4310bb462d297e to your computer and use it in GitHub Desktop.
Plot CRAN package trends
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # install.packages(c("cranlogs", "tidyverse", "lubridate", "ggrepel")) | |
| library(cranlogs) | |
| library(tidyverse) | |
| library(lubridate) | |
| library(ggrepel) | |
| packages <- c( | |
| "albatross", "baseline", "BMRBr", "ChemoSpec", "ChemoSpec2D", | |
| "corr2D", "EEM", "eemR", "hyperSpec", "ir", "iSTATS", "lightr", | |
| "mrbin", "OpenSpecy", "opusreader2", "pavo", "photobiology", | |
| "PlotFTIR", "prospectr", "Rnmr1D", "serrsBayes", | |
| "tidyspec", "tidyDenovix", "waves" | |
| ) | |
| dl <- cran_downloads( | |
| packages = packages, | |
| from = Sys.Date() - years(2), | |
| to = Sys.Date() | |
| ) | |
| weekly_pkg <- dl %>% | |
| mutate(week = floor_date(date, "week")) %>% | |
| group_by(package, week) %>% | |
| summarise( | |
| weekly_downloads = sum(count), | |
| .groups = "drop" | |
| ) %>% | |
| arrange(package, week) %>% | |
| group_by(package) %>% | |
| mutate( | |
| cumulative_downloads = cumsum(weekly_downloads) | |
| ) %>% | |
| ungroup() | |
| # ---- data for tail labels (last point per package) ---- | |
| label_data <- weekly_pkg %>% | |
| group_by(package) %>% | |
| slice_max(week, n = 1) %>% | |
| ungroup() | |
| p <- ggplot(weekly_pkg, aes(week, cumulative_downloads, group = package, colour=package)) + | |
| geom_line(linewidth = 1, alpha = 0.9) + | |
| geom_text_repel( | |
| data = label_data, | |
| aes(label = package), | |
| nudge_x = 10, # push labels slightly to the right | |
| direction = "y", | |
| hjust = 0, | |
| segment.color = NA, # no leader lines (cleaner) | |
| size = 14/.pt | |
| ) + | |
| scale_x_date(expand = expansion(mult = c(0.01, 0.15)), breaks = scales::date_breaks("2 months"), labels=scales::label_date("%Y, %b")) + | |
| scale_y_continuous(labels = scales::label_number(scale=0.001, suffix = "k")) + | |
| labs( | |
| title = "Cumulative CRAN Downloads by Package", | |
| subtitle = "Weekly cumulative sum (last 2 years)", | |
| x = "Week", | |
| y = "Cumulative downloads" | |
| ) + | |
| theme_minimal() + | |
| theme( | |
| legend.position = "none", | |
| plot.margin = margin(5.5, 80, 5.5, 5.5), | |
| axis.text = element_text(size=12), | |
| axis.title = element_text(size=14), | |
| title = element_text(size=16), | |
| plot.subtitle = element_text(size=14), | |
| axis.text.x = element_text(angle=35), | |
| axis.title.x = element_blank() | |
| ) | |
| p | |
| # ggsave("cran_downloads (cumsum).png", plot=p, width = 9, height = 8, units = "in", dpi=120) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment