#prepare inputs file
f_prepare_inputs <- function(dataset){
dataset <- dataset %>%select(
#ountry and population
country, `Population (Mhab)`,
#total excretions and urine,
`human excreta (ktN)`, `human urine (ktN)`, percent_food_waste,
`human excreta % of croplands inputs`, `human urine % of synthetic fertilization`,
#total fertilizers used
`N mineral fertilization (ktN)`,
#inputs to croplands
`N croplands Input (ktN)`,
`N croplands Atmospheric Deposition (ktN)`, `N croplands Biological Fixation (ktN)`,
`N croplands Manure applied to Soils (ktN)`, `N croplands Mineral fertilizers (ktN)`,
) %>%
gather(
input_flow, value,
c(`N croplands Atmospheric Deposition (ktN)`,
`N croplands Biological Fixation (ktN)`,
`N croplands Manure applied to Soils (ktN)`,
`N croplands Mineral fertilizers (ktN)`)
) %>%
mutate(
input_flow = gsub("N croplands ", "", input_flow),
input_flow = gsub(" \\(ktN\\)", "", input_flow)
)
return(dataset)
}
temp_croplands_inputs <- f_prepare_inputs(FAO_countries_2018_2022)
temp_croplands_inputs_world <- f_prepare_inputs(FAO_world_2018_2022)
f_graph_comparison_inputs_per_cap <- function(dataset, variable_inputs, variable_order, variable_excretions){
data_excretions <- dataset %>%
select(country, `Population (Mhab)`, {{ variable_excretions }}, percent_food_waste) %>%
distinct()
#graph comparison absolute per cap : excretions vs inputs
gg <- ggplot(dataset) +
#bars columns inputs N in different colors
geom_col(
aes(
{{ variable_inputs }}/`Population (Mhab)`, reorder(country, {{ variable_order }}/`Population (Mhab)`),
fill=input_flow
),
alpha=.85
) +
#error bars of excretions, based on min and max values of excretions
# geom_linerange(
# data = data_excretions,
# aes(
# xmin={{ variable_excretions }}/`Population (Mhab)`*(1-percent_food_waste_min)/(1-percent_food_waste),
# xmax={{ variable_excretions }}/`Population (Mhab)`*(1-percent_food_waste_max)/(1-percent_food_waste),
# y=country,
# color="human excreta"
# )
# ) +
#points excretions N in red
geom_point(
data = data_excretions,
aes({{ variable_excretions }}/`Population (Mhab)`, country, color="human excreta")
) +
scale_color_manual(values="red") +
#other options
scale_x_reverse(
breaks = seq(0, 1000, 10),
minor_breaks = seq(0, 1000, 5)
) +
theme(
axis.text.y = element_text(hjust=0.5),
legend.position = c(0.35, 0.35),
legend.background = element_rect(
fill = "transparent",
colour = NA),
axis.ticks.y = element_blank(),
axis.line.y = element_blank(),
panel.grid.major.y= element_blank(),
panel.grid.major.x= element_line(),
panel.grid.minor.x= element_line(),
) +
labs(
y="", x=expression("kgN" %.% capita^-1 %.% year^-1), color="", fill=""
)
return(gg)
}
f_graph_comparison_inputs_per_cap_add_world <- function(graph_countries, dataset_world, variable_inputs, variable_excretions){
#columns
gg <- graph_countries +
#bars columns inputs N in different colors
geom_col(
data = dataset_world,
aes(
{{ variable_inputs }}/`Population (Mhab)`, country,
fill=input_flow
),
alpha=.85
) +
#error bars of excretions, based on min and max values of excretions
# geom_linerange(
# data = dataset_world,
# aes(
# xmin={{ variable_excretions }}/`Population (Mhab)`*(1-percent_food_waste_min)/(1-percent_food_waste),
# xmax={{ variable_excretions }}/`Population (Mhab)`*(1-percent_food_waste_max)/(1-percent_food_waste),
# y=country,
# color="human excreta"
# )
# ) +
#points excretions N in red
geom_point(
data = dataset_world,
aes({{ variable_excretions }}/`Population (Mhab)`, country, color="human excreta")
)
return(gg)
}
f_graph_potential_percent <- function(dataset, variable, variable_order){
gg <- ggplot(dataset) +
#columns
geom_col(
aes(
{{ variable }}, reorder(country, {{ variable_order }}/`Population (Mhab)`)
),
alpha=.85
) +
#add % value atht the end of the column
geom_text(
aes(
{{ variable }}, country,
label = paste0(round({{ variable }}*100), "%")
),
hjust=0
) +
# geom_text(
# aes(
# {{ variable }}, country,
# label = paste0(
# "(",
# round({{ variable }}*(1-percent_food_waste_max)/(1-percent_food_waste)*100),
# "-",
# round({{ variable }}*(1-percent_food_waste_min)/(1-percent_food_waste)*100),
# ")"
# )
# ),
# hjust=-1.3, vjust=0.5, size=2, fontface="italic"
# ) +
#other options
theme(
axis.text.y = element_text(hjust=0.5),
axis.ticks.y = element_blank(),
axis.line.y = element_blank(),
panel.grid.major.y= element_blank(),
panel.grid.major.x= element_line(),
panel.grid.minor.x= element_line()
) +
labs(
x="", y=""
) +
coord_cartesian(
xlim = c(0, 1)
) +
scale_x_continuous(
labels=scales::percent,
breaks=seq(0, 10, .2),
minor_breaks = seq(0, 10, .05)
)
return(gg)
}
f_graph_potential_percent_add_world <- function(graph_countries, dataset_world, variable){
#columns
gg <- graph_countries +
geom_col(
data = dataset_world,
aes(
{{ variable }}, country
),
alpha=.85
) +
#add % value atht the end of the column
geom_text(
data = dataset_world,
aes(
{{ variable }}, country,
label = paste0(round({{ variable }}*100), "%")
),
hjust=0
)
# geom_text(
# data = dataset_world,
# aes(
# {{ variable }}, country,
# label = paste0(
# "(",
# round({{ variable }}*(1-percent_food_waste_max)/(1-percent_food_waste)*100),
# "-",
# round({{ variable }}*(1-percent_food_waste_min)/(1-percent_food_waste)*100),
# ")"
# )
# ),
# hjust=-1.3, vjust=0.5, size=2, fontface="italic"
# )
return(gg)
}