# ==============================================================================
# Supplementary File S3
# Consolidated R scripts for all bibliometric analyses reported in:
# "Mapping the Sustainability Transition in Liquid and Water-Soluble
# Fertilizer Research: A Scopus-Based Bibliometric Analysis (2021-2026)"
#
# Requires: bibliometrix (v.5.4.1), R (v.4.5.1 or later)
# Input file: Scopus_forVOSviewer_201.csv (Supplementary File S2)
# ==============================================================================

# ------------------------------------------------------------------------------
# 0. SETUP
# ------------------------------------------------------------------------------
# install.packages("bibliometrix")  # run once if not already installed
library(bibliometrix)

M <- convert2df("Scopus_forVOSviewer_201.csv", dbsource = "scopus", format = "csv")
cat("Documents loaded:", nrow(M), "\n")  # should read 201

# ------------------------------------------------------------------------------
# 1. MAIN DESCRIPTIVE ANALYSIS (Section 3.1)
# ------------------------------------------------------------------------------
results <- biblioAnalysis(M, sep = ";")
summary(results, k = 20, pause = FALSE)
# plot(x = results, k = 20, pause = FALSE)   # annual production, top authors/sources, etc.

# ------------------------------------------------------------------------------
# 2. LOTKA'S LAW (Section 3.2 / Figure 4)
# ------------------------------------------------------------------------------
lotka_res <- lotka(results)
lotka_res$AuthorProd            # observed author productivity table
lotka_res$Beta                  # fitted Beta coefficient (reported as 2.95)
lotka_res$R2                    # goodness of fit
lotka_res$p.value                # Kolmogorov-Smirnov test p-value

# ------------------------------------------------------------------------------
# 3. BRADFORD'S LAW (Section 3.2 / Figure 3)
# ------------------------------------------------------------------------------
bradford_res <- bradford(M)
bradford_res$table               # Zone 1 / Zone 2 / Zone 3 journal counts

# ------------------------------------------------------------------------------
# 4. CONCEPTUAL STRUCTURE / THEMATIC EVOLUTION (Section 3.3)
# ------------------------------------------------------------------------------
# Keyword co-occurrence network was built and visualized in VOSviewer (v.1.6.20)
# using association-strength normalization and full counting, min. occurrence = 3.
# Thematic evolution (Cobo et al. framework) was generated via:
years <- c(2021, 2024, 2026)  # two sub-periods per manuscript: 2021-2023 and 2024-2026 (Fig. 7)
thematic_evo <- thematicEvolution(M, field = "ID", years = years, n = 250,
                                   minFreq = 2, size = 0.4, n.Label = 3)
plotThematicEvolution(thematic_evo$Nodes, thematic_evo$Edges)

# ------------------------------------------------------------------------------
# 5. COLLABORATION AND CITATION-BASED NETWORKS (Section 3.4 / Figures 8-9 and S8-S10)
# ------------------------------------------------------------------------------
# Author collaboration, country collaboration, bibliographic coupling, and
# co-citation networks (Figures 8-9 and S8-S10) were generated in VOSviewer (v.1.6.20)
# using the biblioNetwork() adjacency matrices below as a cross-check, or
# directly from the Scopus CSV within VOSviewer's "Create Map" wizard.

NetMatrix_coauthor <- biblioNetwork(M, analysis = "collaboration", network = "authors", sep = ";")
NetMatrix_country  <- biblioNetwork(M, analysis = "collaboration", network = "countries", sep = ";")
NetMatrix_coupling <- biblioNetwork(M, analysis = "coupling", network = "references", sep = ";")
NetMatrix_cocit    <- biblioNetwork(M, analysis = "co-citation", network = "authors", sep = ";")

# ------------------------------------------------------------------------------
# 6. DIRECT CITATION NETWORK VERIFICATION (Section 3.4 / Figure 10)
# ------------------------------------------------------------------------------
# VOSviewer's direct citation-network module (Create Map > Citation > Documents,
# threshold = 0) returned 0 links among all 201 documents. Because this null
# result was unexpected given the corpus's co-citation structure, it was
# independently cross-validated using bibliometrix's author-year-source
# matching, which does not require DOIs (unlike VOSviewer's apparent
# matching logic for this Scopus export format).

# 6a. Aggregate verification: how many documents/links exist within the corpus?
CR <- localCitations(M, sep = ";")
cited_docs <- CR$Papers[CR$Papers$LCS > 0, ]
cat("Documents cited at least once within the corpus:", nrow(cited_docs), "\n")  # 23
cat("Total internal citation links:", sum(cited_docs$LCS), "\n")                  # 26

# 6b. Build and plot the historical direct citation network (Figure 10)
histResults <- histNetwork(M, sep = ";", verbose = FALSE)

# Determine the exact number of documents participating in the network
# (as citing and/or cited) rather than using an arbitrary node count:
full_result <- histPlot(histResults, n = 201, size = 5, labelsize = 3)
n_exact <- nrow(full_result$layout)
cat("Documents participating in the citation network:", n_exact, "\n")  # 45

# Final plot as used for Figure 10 (generated via Biblioshiny's Historiograph
# panel with Number of Nodes = 45, Node Label = "Short id (1st Author, Year)",
# Remove Isolated Nodes = Yes, for optimal label spacing; equivalent R call:)
png("Figure10_DirectCitationNetwork.png", width = 4800, height = 1800, res = 320)
histPlot(histResults, n = n_exact, size = 5, labelsize = 2.6)
dev.off()

# ------------------------------------------------------------------------------
# 7. SESSION INFO (for reproducibility)
# ------------------------------------------------------------------------------
sessionInfo()
