Skip to content

GRAFS plot dataset#63

Open
AliceBeckmann wants to merge 164 commits into
mainfrom
alice/grafs_plot
Open

GRAFS plot dataset#63
AliceBeckmann wants to merge 164 commits into
mainfrom
alice/grafs_plot

Conversation

@AliceBeckmann
Copy link
Copy Markdown
Collaborator

@AliceBeckmann AliceBeckmann commented Oct 27, 2025

TODOs:

  • Changing the <CRP_LS> label from emissions to losses in crop production (the part that was lost and not used).

GRAFS plot dataset description
This PR introduces the main function create_grafs_plot_df(), which includes helper functions to generate a dataset of nitrogen (MgN) flows by province and year as well as land use information in Spain. The data are assigned to labels, defined by Alfredo Rodriguez, which are needed to create GRAFS plots with Alfredo’s R package.

The resulting tibble is designed to feed directly into the GRAFS visualization R package.


Label description

Land use and land N production
Land use areas and associated N production are taken from n_balance_ygpit_all.
They are aggregated by province and year and mapped to labels such as:
cropland N and area ({ARAiN}, {ARArN}, {ARAiha}, {ARArha}),
permanent crops ({PERiN}, {PERrN}),
horticulture ({HORiN}, {HORrN}),
forest and semi-natural systems ({FORN}),
grassland ({HAGRASS}, {GRASSMha})
greenhouse systems ({GREHN}).
Both irrigated and rainfed systems are treated separately.

Crop and grass N consumption
N flows (consumption) from cropland and grassland are taken from create_n_prov_destiny() and tranformed to labels including:
crops to population ({CROPS_TO_POP}),
crops to livestock ({CROPS_TO_LIVESTOCK}),
grass to livestock ({GRASS_TO_LIVESTOCK}),
crop exports ({CROP_EXPORT}),
crop N used for other uses ({CRP_OTHUSES}).

Livestock production
Livestock N production is taken from create_n_prov_destiny() and livestock_prod_ygps.
It is separated into edible and non-edible products and mapped to:
edible livestock products ({LV_EDBL}),
non-edible livestock products ({LVSTCK_NOEDIBLE}),
milk and dairy products ({LVST_MILK}),
exported livestock products ({LIVESTOCK_EXPORTED}).

Livestock losses and totals
Livestock N losses are taken from n_excretion_ygs and population-related other uses.
They are represented using the labels:
livestock losses ({AN_LS}),
livestock other uses ({AN_OTH}),
total livestock losses ({AN_LS_OTH}),
total livestock N ({LVSTCKTOTN}),
gaseous N losses ({LIVGASLOSS}).

Feed and internal transfers
Feed flows from cropland to livestock are taken from create_n_prov_destiny() and split into:
ruminant feed ({RCRTOLVSTCK_R}),
monogastric feed ({MCRTOLVSTCK_M}).

Imports and N soil inputs
N imports and N soil inputs are taken from create_n_prov_destiny() and n_balance_ygpit_all.
They are mapped to labels such as:
crop and animal product imports ({CROP_POPIMPORT}, {IMANOT}, {IMPORT_ANIMALCR}),
synthetic fertilizers ({SYNTHF}, {SYNTHF_TOTAL}),
biological fixation and atmospheric deposition ({FIXCR}, {OXDEPCROPS}, {FIX_DEP_CR}, {FIX_DEP_GRASS}),
wastewater inputs from population ({WASTEWATER}).

Livestock units and population
Livestock units (LU) are calculated from livestock_prod_ygps stock numbers and included using:
ruminant LU ({RUMIANTSLU}, {RUMIANTSMLU}),
monogastric LU ({MONOGLU}, {MONOGMLU}).
Population data are taken from population_yg and included as {POPULATIONM}.

Final steps
All datasets are merged into a single standardized table.
Missing labels required for plotting are added with zero values.

res_new <- env_new$create_n_prov_destiny()
nat_new <- env_new$create_n_nat_destiny()

cat("res_new imports total:", sum(filter(res_new, Origin == "Outside")$MgN, na.rm = TRUE), "\n")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
cat("res_new imports total:", sum(filter(res_new, Origin == "Outside")$MgN, na.rm = TRUE), "\n")
cat(
"res_new imports total:",
sum(filter(res_new, Origin == "Outside")$MgN, na.rm = TRUE),
"\n"
)


cat("res_new imports total:", sum(filter(res_new, Origin == "Outside")$MgN, na.rm = TRUE), "\n")
cat("res_new import rows:", nrow(filter(res_new, Origin == "Outside")), "\n")
cat("nat_new imports total:", sum(filter(nat_new, Origin == "Outside")$MgN, na.rm = TRUE), "\n")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
cat("nat_new imports total:", sum(filter(nat_new, Origin == "Outside")$MgN, na.rm = TRUE), "\n")
cat(
"nat_new imports total:",
sum(filter(nat_new, Origin == "Outside")$MgN, na.rm = TRUE),
"\n"
)

crop_area <- whep_read_file("crop_area_npp_ygpitr_no_fallow")

cat("n_balance columns include Item:", "Item" %in% names(n_balance), "\n")
cat("n_balance columns include Name_biomass:", "Name_biomass" %in% names(n_balance), "\n")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
cat("n_balance columns include Name_biomass:", "Name_biomass" %in% names(n_balance), "\n")
cat(
"n_balance columns include Name_biomass:",
"Name_biomass" %in% names(n_balance),
"\n"
)

cat(
"n_balance rows with straw token:",
sum(
grepl("straw", apply(n_balance, 1, paste, collapse = "|"), ignore.case = TRUE)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
grepl("straw", apply(n_balance, 1, paste, collapse = "|"), ignore.case = TRUE)
grepl(
"straw",
apply(n_balance, 1, paste, collapse = "|"),
ignore.case = TRUE
)

)

if ("Item" %in% names(n_balance)) {
cat("n_balance Item==Straw rows:", nrow(filter(n_balance, Item == "Straw")), "\n")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
cat("n_balance Item==Straw rows:", nrow(filter(n_balance, Item == "Straw")), "\n")
cat(
"n_balance Item==Straw rows:",
nrow(filter(n_balance, Item == "Straw")),
"\n"
)

}

if ("Item" %in% names(crop_area)) {
cat("crop_area Item==Straw rows:", nrow(filter(crop_area, Item == "Straw")), "\n")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
cat("crop_area Item==Straw rows:", nrow(filter(crop_area, Item == "Straw")), "\n")
cat(
"crop_area Item==Straw rows:",
nrow(filter(crop_area, Item == "Straw")),
"\n"
)

Comment thread R/n_prov_destiny.R
Comment on lines +64 to +65


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

Comment thread R/n_prov_destiny.R

nat_soil_inputs <- prov |>
dplyr::filter(
Origin %in% c("Deposition", "Fixation", "Synthetic", "Livestock", "People"),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
Origin %in% c("Deposition", "Fixation", "Synthetic", "Livestock", "People"),
Origin %in%
c("Deposition", "Fixation", "Synthetic", "Livestock", "People"),

Comment thread R/n_prov_destiny.R
Province_name = "Spain"
) |>
dplyr::filter(!is.na(Box), MgN > 0) |>
dplyr::select(Year, Province_name, Item, Irrig_cat, Box, Origin, Destiny, MgN)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
dplyr::select(Year, Province_name, Item, Irrig_cat, Box, Origin, Destiny, MgN)
dplyr::select(
Year,
Province_name,
Item,
Irrig_cat,
Box,
Origin,
Destiny,
MgN
)

Comment thread R/n_prov_destiny.R
values_from = feed_amount,
values_fill = 0
) |>
dplyr::mutate(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
dplyr::mutate(
dplyr::mutate(

Comment thread R/n_prov_destiny.R
Comment on lines +1383 to +1385
dplyr::mutate(
share_rum = dplyr::coalesce(share_rum, 0),
share_mono = dplyr::coalesce(share_mono, 0),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
dplyr::mutate(
share_rum = dplyr::coalesce(share_rum, 0),
share_mono = dplyr::coalesce(share_mono, 0),
dplyr::mutate(
share_rum = dplyr::coalesce(share_rum, 0),
share_mono = dplyr::coalesce(share_mono, 0),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants