1515# '
1616# ' @export
1717create_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+ }
0 commit comments