# The package ggtext needs to be installed to run this chunk
library(ggtext)
library(openalexR)
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
jours_all <- oa_fetch(
  entity = "sources",
  works_count = ">1000000",
  verbose = TRUE
)
## Requesting url:
## <https://api.openalex.org/sources?filter=works_count%3A%3E1000000>
## ℹ Getting 1 page of results with a total of 32 records...
## ⠙ Converting [9/32] ■■■■■■■■■                         28% ETA:  3s
## ⠹ Converting [10/32] ■■■■■■■■■■                        31% ETA:  3s
## ⠸ Converting [13/32] ■■■■■■■■■■■■■                     41% ETA:  2s
## ⠼ Converting [14/32] ■■■■■■■■■■■■■■                    44% ETA:  2s
## ⠴ Converting [16/32] ■■■■■■■■■■■■■■■■                  50% ETA:  2s
## ⠦ Converting [18/32] ■■■■■■■■■■■■■■■■■■                56% ETA:  2s
## ⠧ Converting [20/32] ■■■■■■■■■■■■■■■■■■■■              62% ETA:  1s
## ⠇ Converting [22/32] ■■■■■■■■■■■■■■■■■■■■■■            69% ETA:  1s
## ⠏ Converting [24/32] ■■■■■■■■■■■■■■■■■■■■■■■           75% ETA:  1s
## ⠋ Converting [26/32] ■■■■■■■■■■■■■■■■■■■■■■■■■         81% ETA:  1s
## ⠙ Converting [27/32] ■■■■■■■■■■■■■■■■■■■■■■■■■■        84% ETA:  1s
## ⠹ Converting [28/32] ■■■■■■■■■■■■■■■■■■■■■■■■■■■       88% ETA:  0s
## ⠸ Converting [29/32] ■■■■■■■■■■■■■■■■■■■■■■■■■■■■      91% ETA:  0s
## ⠼ Converting [30/32] ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■     94% ETA:  0s
## ⠴ Converting [31/32] ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■    97% ETA:  0s
## ⠴ Converting [32/32] ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■  100% ETA:  0s
clean_journal_name <- function(x) {
  x |>
    gsub("\\(.*?\\)", "", x = _) |>
    gsub("Journal of the|Journal of", "J.", x = _) |>
    gsub("/.*", "", x = _)
}

jours <- jours_all |>
  filter(type == "journal") |>
  slice_max(cited_by_count, n = 9) |>
  distinct(display_name, .keep_all = TRUE) |>
  select(jour = display_name, topics) |>
  tidyr::unnest(topics) |>
  filter(type == "field") |>
  group_by(id, jour, display_name) |> 
  summarise(score = (sum(count))^(1/3), .groups = "drop") |> 
  left_join(concept_abbrev, by = join_by(id, display_name)) |>
  mutate(
    abbreviation = gsub(" ", "<br>", abbreviation),
    jour = clean_journal_name(jour),
  ) |>
  tidyr::complete(jour, abbreviation, fill = list(score = 0)) |>
  group_by(jour) |>
  mutate(
    color = if_else(score > 10, "#1A1A1A", "#D9D9D9"), # CCCCCC
    label = paste0("<span style='color:", color, "'>", abbreviation, "</span>")
  ) |>
  ungroup()

jours |>
  ggplot() +
  aes(fill = jour, y = score, x = abbreviation, group = jour) +
  facet_wrap(~jour) +
  geom_hline(yintercept = c(25, 50), colour = "grey90", linewidth = 0.2) +
  geom_segment(
    aes(x = abbreviation, xend = abbreviation, y = 0, yend = 55),
    color = "grey95"
  ) +
  geom_col(color = "grey20") +
  coord_polar(clip = "off") +
  theme_bw() +
  theme(
    plot.background = element_rect(fill = "transparent", colour = NA),
    panel.background = element_rect(fill = "transparent", colour = NA),
    panel.grid = element_blank(),
    panel.border = element_blank(),
    axis.text = element_blank(),
    axis.ticks.y = element_blank()
  ) +
  ggtext::geom_richtext(
    aes(y = 75, label = label),
    fill = NA, label.color = NA, size = 3
  ) +
  scale_fill_brewer(palette = "Set1", guide = "none") +
  labs(y = NULL, x = NULL, title = "Journal clocks")