#order of legend labels
ordered_input_flows <- c("Seed", "Atmospheric Deposition", "Biological Fixation", "Manure applied to Soils", "Mineral fertilizers")
ordered_output_flows <- c("Leaching", "Volatilisation", "Crop Removal")
#function for graph inputs flows
f_graph_inputs <- function(country_selected, nutrient_name, nutrient_label){
#inputs flows, in selected country
temp <- data_balance_countries %>%
filter(
country%in%country_selected,
flows %in% ordered_input_flows
)
#order input flows legend, in selected country
temp$flows <- factor(
temp$flows,
levels = ordered_input_flows
)
#total input, in selected country
temp2 <- data_balance_countries %>%
filter(
country%in%country_selected,
flows=="Input"
)
#graph
gg1 <- ggplot(temp2) +
#different input flows area
geom_area(
data = temp,
aes(Year, {{ nutrient_name }}/10^3, fill=flows),
alpha=.7
) +
#total inputs line
geom_line(
aes(Year, {{ nutrient_name }}/10^3, color=" input"),
size=1.5
) +
scale_color_manual(
values = c("black")
) +
#labels
labs(
title = paste(nutrient_label, "inputs balance on croplands,", country_selected),
fill=paste("input flows"),
color="", y=paste0("kt", nutrient_label), x=""
)
return(gg1)
}
#function for graph outputs flows
f_graph_output <- function(country_selected, nutrient_name, nutrient_label){
#outputs in selected country
temp <- data_balance_countries %>%
filter(
country==country_selected,
flows %in% ordered_output_flows
)
#order output flows legend, in selected country
temp$flows <- factor(
temp$flows,
levels = ordered_output_flows
)
#total outputs by FAO definition, in selected country
temp2 <- data_balance_countries %>%
filter(
country==country_selected,
flows=="Outputs"
)
#total inputs, in selected country
temp3 <- data_balance_countries %>%
filter(
country==country_selected,
flows=="Input"
)
#graph
gg <- ggplot(temp2) +
#different output flows area
geom_area(
data = temp,
aes(Year, {{ nutrient_name }}/10^3, fill=flows),
alpha=.7
) +
#total output line by FAO definition
geom_line(
aes(Year, {{ nutrient_name }}/10^3, color='output, by\nFAO definition'),
size=1, linetype="dotted"
) +
#total inputs line
geom_line(
data = temp3,
aes(Year, {{ nutrient_name }}/10^3, color="input"), size=2
) +
scale_color_manual(values = c("black", "black")) +
#labels
labs(
title = paste(nutrient_label, "output balance on croplands,", country_selected),
fill=paste("output flows"),
color="", y=paste0("kt", nutrient_label), x=""
)
return(gg)
}
#function for graph inputs outputs flows side by side
f_graph_inputs_outputs <- function(country_selected, nutrient_name, nutrient_label){
plot_grid(
f_graph_inputs(country_selected, {{ nutrient_name }}, nutrient_label),
f_graph_output(country_selected, {{ nutrient_name }}, nutrient_label),
nrow = 2, align = "v"
)
}
#save graph with example countries for thesis manuscript
gg <- f_graph_inputs(c("France", "Myanmar", "Turkey"), `Cropland nitrogen (t)`, "N") +
facet_wrap(vars(country), ncol=1, scales = "free_y") +
labs(title = "N inputs on croplands", caption="based on FAOSTAT data")
gg