Skip to content

Commit 445f9bd

Browse files
committed
Revision style as Cata suggested
1 parent 5cdc792 commit 445f9bd

7 files changed

Lines changed: 450 additions & 174 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Version: 0.0.0.9000
44
Authors@R: c(
55
person("Catalin", "Covaci", , "catalin.covaci@csic.es", role = c("aut", "cre"), comment = c(ORCID = "0009-0005-2186-5972")),
66
person("Eduardo", "Aguilera", , "eduardo.aguilera@csic.es", role = c("aut"), comment = c(ORCID = "0000-0003-4382-124X")),
7-
person("João", "Serra", , "jserra@agro.au.dk", role = c("ctb"), comment = c(ORCID = "0000-0002-3561-5350"))
7+
person("João", "Serra", , "jserra@agro.au.dk", role = c("ctb"), comment = c(ORCID = "0000-0002-3561-5350")),
8+
person("Alice", "Beckmann", , "alice.beckmann@cchs.csic.es", role = c("aut"), comment = c(ORCID = "0009-0009-6840-0258"))
89
)
910
Description: Gather historical agricultural and trade data from 1850.
1011
License: MIT + file LICENSE

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3-
export(.create_prod_and_destiny_grafs)
3+
export(create_prod_and_destiny_grafs)
44
export(add_area_code)
55
export(add_area_name)
66
export(add_item_cbs_code)

R/Typologies_Julia.R

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
#'
1616
#' @export
1717
create_typologies_grafs_spain <- function(
18-
make_map = TRUE,
19-
shapefile_path = paste0(
20-
"C:/PhD/GRAFS/Production Boxes/",
21-
"Final Files/Inputs/ne_10m_admin_1_states_provinces.shp"
22-
),
23-
map_year = 1980) {
18+
make_map = TRUE,
19+
shapefile_path = paste0(
20+
"C:/PhD/GRAFS/Production Boxes/",
21+
"Final Files/Inputs/ne_10m_admin_1_states_provinces.shp"
22+
),
23+
map_year = 1980
24+
) {
2425
inputs_dir <- "C:/PhD/GRAFS/Production Boxes/Final Files/Inputs"
2526

2627
# Load datasets
@@ -131,6 +132,7 @@ create_typologies_grafs_spain <- function(
131132
}
132133

133134
#' Load input datasets ---------------------------------------------------------
135+
#'
134136
#' @param shapefile_path The local path where the input data are located.
135137
#' @param inputs_dir Path to the input data directory.
136138
#'
@@ -180,6 +182,7 @@ create_typologies_grafs_spain <- function(
180182
}
181183

182184
#' Prepare LU coefficients with Livestock_cat mapping --------------------------
185+
#'
183186
#' @param codes_coefs_df An excel file including coefficients.
184187
#' @keywords internal
185188
.prepare_lu_coefs <- function(codes_coefs_df) {
@@ -192,8 +195,8 @@ create_typologies_grafs_spain <- function(
192195
}
193196

194197
#' Calculate LU_total per row --------------------------------------------------
195-
#' @param livestock_df A data frame containing livestock data.
196198
#'
199+
#' @param livestock_df A data frame containing livestock data.
197200
#' @param lu_coefs_df A data frame with livestock unit coefficients.
198201
#'
199202
#' @return A tibble with columns 'Year', 'Province_name', 'Livestock_cat',
@@ -250,8 +253,8 @@ create_typologies_grafs_spain <- function(
250253
}
251254

252255
#' Calculate livestock density -------------------------------------------------
253-
#' @param lu_totals_df A data frame containing livestock total data.
254256
#'
257+
#' @param lu_totals_df A data frame containing livestock total data.
255258
#' @param area_df A data frame containing area information.
256259
#'
257260
#' @return A tibble with columns 'Year', 'Province_name', 'LU_total', 'Area_ha',
@@ -266,6 +269,7 @@ create_typologies_grafs_spain <- function(
266269
}
267270

268271
#' Aggregate Productivity for Cropland -----------------------------------------
272+
#'
269273
#' @param npp_df A data frame containing columns `Year`, `Province_name`,
270274
#' `LandUse`,`Prod_MgN`, and `Area_ygpit_ha`.
271275
#'
@@ -309,6 +313,7 @@ create_typologies_grafs_spain <- function(
309313
}
310314

311315
#' Aggregate Feed from Cropland -----------------------------------------------
316+
#'
312317
#' @param df A data frame containing columns `Year`, `Province_name`,
313318
#' `Box`, `Destiny`, and `MgN`.
314319
#'
@@ -371,9 +376,9 @@ create_typologies_grafs_spain <- function(
371376
}
372377

373378
#' Calculate feed domestic supply ---------------------------------------------
379+
#'
374380
#' @param grafs_df A data frame containing GRAFS data with the columns Destiny',
375381
#' 'Year', 'Province_name', and 'MgN'.
376-
#'
377382
#' @param lu_df A data frame with land use data.
378383
#'
379384
#' @return A tibble with columns 'Year', 'Province_name', and
@@ -388,7 +393,7 @@ create_typologies_grafs_spain <- function(
388393
na.rm = TRUE
389394
), .groups = "drop")
390395

391-
#' Add LU_total for use in further steps
396+
# Add LU_total for use in further steps
392397
domestic_feed |>
393398
dplyr::left_join(
394399
lu_df |> dplyr::select(Year, Province_name, LU_total),
@@ -397,8 +402,8 @@ create_typologies_grafs_spain <- function(
397402
}
398403

399404
#' Calculate feed import per province -----------------------------------------
400-
#' @param feed_df A data frame containing feed data.
401405
#'
406+
#' @param feed_df A data frame containing feed data.
402407
#' @param lu_df A data frame with land use information.
403408
#'
404409
#' @return A tibble with columns 'Year', 'Province_name', 'LU_total',
@@ -434,13 +439,16 @@ create_typologies_grafs_spain <- function(
434439
}
435440

436441
#' Calculate feed share of imported/consumed feed -----------------------------
442+
#'
437443
#' @param feed_import_by_province A data frame containing imported feed data.
438444
#' @param domestic_feed_by_province A data frame containing domestic feed data.
439445
#'
440446
#' @return A data frame with the imported feed share.
441447
#' @keywords internal
442-
.calculate_imported_feed_share <- function(feed_import_by_province,
443-
domestic_feed_by_province) {
448+
.calculate_imported_feed_share <- function(
449+
feed_import_by_province,
450+
domestic_feed_by_province
451+
) {
444452
feed_import_by_province |>
445453
dplyr::left_join(
446454
domestic_feed_by_province,
@@ -465,6 +473,7 @@ create_typologies_grafs_spain <- function(
465473
}
466474

467475
#' Assign Typologies and optionally plot map
476+
#'
468477
#' @param livestock_density A data frame with livestock density values.
469478
#' @param productivity A data frame with productivity (kgN/ha) values.
470479
#' @param semi_nat_share A data frame with semi-natural agroecosystem share.
@@ -476,8 +485,9 @@ create_typologies_grafs_spain <- function(
476485
#' typology for the specified year.
477486
#' @keywords internal
478487
.assign_decision_tree <- function(
479-
livestock_density, productivity, semi_nat_share, imported_feed_share,
480-
sf_provinces, year) {
488+
livestock_density, productivity, semi_nat_share, imported_feed_share,
489+
sf_provinces, year
490+
) {
481491
typologies <- livestock_density |>
482492
dplyr::inner_join(productivity, by = c("Year", "Province_name")) |>
483493
dplyr::inner_join(semi_nat_share, by = c("Year", "Province_name")) |>
@@ -555,3 +565,43 @@ create_typologies_grafs_spain <- function(
555565
Typologies_all_years = typologies_all_years
556566
)
557567
}
568+
569+
570+
# Create bar plots for N inputs per typology and year
571+
.create_bar_plots_typologies <- function(n_inputs_data,
572+
typologies, output_dir = NULL) {
573+
# Merging n_inputs_data with typologies (per Year and Province_name)
574+
typologies <- n_inputs_data %>%
575+
inner_join(typologies, by = c("Year", "Province_name"))
576+
577+
bar_plot_data <- typologies %>%
578+
group_by(Typology, Year) %>%
579+
summarise(
580+
MgN_dep = sum(MgN_dep, na.rm = TRUE),
581+
MgN_fix = sum(MgN_fix, na.rm = TRUE),
582+
MgN_syn = sum(MgN_syn, na.rm = TRUE),
583+
.groups = "drop"
584+
) %>%
585+
pivot_longer(
586+
cols = c(MgN_dep, MgN_fix, MgN_syn),
587+
names_to = "n_input",
588+
values_to = "MgN_amount"
589+
)
590+
591+
# Stacked Bar Plot erstellen
592+
p <- ggplot(bar_plot_data, aes(x = factor(Year), y = MgN_amount, fill = n_input)) +
593+
geom_bar(stat = "identity") +
594+
facet_wrap(~Typology) +
595+
labs(
596+
title = "Stacked Barplots of N_Inputs per Typology",
597+
x = "Year",
598+
y = "MgN",
599+
fill = "Input"
600+
) +
601+
theme_minimal() +
602+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
603+
604+
print(p)
605+
606+
return(p)
607+
}
Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
#' @title Create N Inputs and NUE Dataset for GRAFS Spain
1+
#' @title N Inputs and NUE for GRAFS Spain
22
#'
33
#' @description
44
#' N Inputs, Production and NUE in Spain:
55
#' This code is creating a dataset with nitrogen (N) inputs (deposition,
6-
#' fixation, synthetic, urban, manure)
7-
#' and N production in Spain between 1860 and 2020 for the GRAFS model on a
8-
#' provincial level
6+
#' fixation, synthetic, urban, manure) and N production in Spain between
7+
#' 1860 and 2020 for the GRAFS model on a provincial level
98
#'
109
#' @return
1110
#' A named list with two elements:
@@ -46,27 +45,31 @@ create_n_inputs_grafs_spain <- function() {
4645
)
4746
}
4847

49-
# N Inputs --------------------------------------------------------------------
50-
# load data -------------------------------------------------------------------
51-
# @keywords internal
48+
#' @title N Inputs -------------------------------------------------------------
49+
#' @description Loading all required data sets.
50+
#' @return A named list including four datasets.
51+
#' @keywords internal
52+
#' @noRd
5253
.load_inputs_n_inputs <- function() {
5354
result <-
5455
list(
5556
n_Excretion_ygs = readRDS(get_file_path("n_excretion_ygs")),
5657
# TODO: Excretion need to be added to dataset as an input of Livestock
5758
n_balance_ygpit_all = readRDS(get_file_path("n_balance_ygpit_all")),
58-
grafs_prod_destiny = readr::read_csv(get_file_path(
59-
"GRAFS_prod_destiny_git"
60-
)),
59+
grafs_prod_destiny = readr::read_csv(
60+
get_file_path("GRAFS_prod_destiny_git")
61+
),
6162
codes_coefs = readxl::read_excel(get_file_path("codes_coefs"),
6263
sheet = "Names_biomass_CB"
6364
)
6465
)
6566
result
6667
}
6768

68-
# Assign some special items to Boxes ------------------------------------------
69-
# @keywords internal
69+
#' @title Assign some special items to Boxes -----------------------------------
70+
#' @return A named list with assigned items.
71+
#' @keywords internal
72+
#' @noRd
7073
.assign_items <- function() {
7174
list(
7275
semi_natural_agroecosystems = c(
@@ -81,9 +84,21 @@ create_n_inputs_grafs_spain <- function() {
8184
)
8285
}
8386

84-
# Calculate n Inputs ----------------------------------------------------------
85-
# @keywords internal
86-
.calculate_n_inputs <- function(n_balance_ygpit_all, codes_coefs) {
87+
#' @title Calculate N Inputs ---------------------------------------------------
88+
#' @description Merges N balance data with items and aggregates deposition,
89+
#' fixation, synthetic, urban, and manure inputs for each combination of year,
90+
#' province, item, and box.
91+
#'
92+
#' @param n_balance_ygpit_all A data frame containing nitrogen balance data.
93+
#' @param codes_coefs A data frame merging biomass names to item names.
94+
#'
95+
#' @return A list with two tibbles: 'n_inputs_summary' and 'manure_summary'.
96+
#' @keywords internal
97+
#' @noRd
98+
.calculate_n_inputs <- function(
99+
n_balance_ygpit_all,
100+
codes_coefs
101+
) {
87102
categories <- .assign_items()
88103
firewood_biomass <- categories$Firewood_biomass
89104
semi_natural_agroecosystems <- categories$semi_natural_agroecosystems
@@ -145,9 +160,18 @@ create_n_inputs_grafs_spain <- function() {
145160
)
146161
}
147162

148-
# Combine all Inputs ----------------------------------------------------------
149-
# @keywords internal
150-
.summarise_inputs <- function(n_inputs_prepared) {
163+
#' @title Combine all Inputs ---------------------------------------------------
164+
#' @description Combines different N input sources and calculates total values
165+
#' per year, province, item, and box.
166+
#'
167+
#' @param n_inputs_prepared A list with 'n_inputs_summary' and 'manure_summary'
168+
#'
169+
#' @return A tibble summarising nitrogen input components by item and region.
170+
#' @keywords internal
171+
#' @noRd
172+
.summarise_inputs <- function(
173+
n_inputs_prepared
174+
) {
151175
n_inputs <- dplyr::full_join(
152176
n_inputs_prepared$n_inputs_summary,
153177
n_inputs_prepared$manure_summary,
@@ -168,11 +192,20 @@ create_n_inputs_grafs_spain <- function() {
168192
n_inputs_sum
169193
}
170194

171-
# GRAFS_Prod_Destiny ---------------------------------------------------------
172-
# Summarize and calculate new columns: Prod_MgN
173-
# Spread Destiny column to separate columns for Food, Feed, Other_uses, Export
174-
# @keywords internal
175-
.summarise_production <- function(grafs_prod_destiny, n_inputs_sum) {
195+
#' @title GRAFS_Prod_Destiny ---------------------------------------------------
196+
#' @description Summarize and calculate new columns: Prod_MgN
197+
#' Spread Destiny column to separate columns for Food, Feed, Other_uses, Export
198+
#'
199+
#' @param grafs_prod_destiny Data containing production values by destiny.
200+
#' @param n_inputs_sum A tibble of summarized N inputs.
201+
#'
202+
#' @return A tibble with combined N input and production data.
203+
#' @keywords internal
204+
#' @noRd
205+
.summarise_production <- function(
206+
grafs_prod_destiny,
207+
n_inputs_sum
208+
) {
176209
grafs_prod_destiny_summary <- grafs_prod_destiny |>
177210
tidyr::pivot_wider(
178211
names_from = Destiny, values_from = MgN, values_fn = sum,
@@ -194,18 +227,28 @@ create_n_inputs_grafs_spain <- function() {
194227
dplyr::select(Year, Province_name, Item, Box, Import_MgN, Prod_MgN)
195228

196229
# Combine with n_inputs dataset
197-
n_inputs_combined <- dplyr::full_join(n_inputs_sum,
198-
grafs_prod_destiny_summary,
230+
n_inputs_combined <- dplyr::full_join(
231+
n_inputs_sum, grafs_prod_destiny_summary,
199232
by = c("Year", "Province_name", "Item", "Box")
200233
) |>
201234
dplyr::filter(!is.na(Box))
202235

203236
n_inputs_combined
204237
}
205238

206-
# NUE for Cropland and Semi-natural agroecosystems ----------------------------
207-
# @keywords internal
208-
.calculate_nue <- function(n_inputs_combined) {
239+
#' @title NUE for Cropland and Semi-natural agroecosystems ---------------------
240+
#' @description Calculates NUE for cropland and semi-natural agroecosystems as
241+
#' the ratio of production to total N input.
242+
#'
243+
#' @param n_inputs_combined A data frame combining N inputs and production.
244+
#'
245+
#' @return A tibble with calculated NUE values for cropland and semi-natural
246+
#' agroecosystems.
247+
#' @keywords internal
248+
#' @noRd
249+
.calculate_nue <- function(
250+
n_inputs_combined
251+
) {
209252
nue <- n_inputs_combined |>
210253
dplyr::mutate(
211254
Inputs_MgN = MgN_dep + MgN_fix + MgN_syn + MgN_manure + MgN_urban

0 commit comments

Comments
 (0)