-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path-ILRIKE-21154.Rhistory
512 lines (512 loc) · 25.9 KB
/
-ILRIKE-21154.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
manure_methane_per_ha_kg_co2_e,
manure_direct_N2O_tot_kg_co2_e,
manure_Indirect_N2O_tot_kg_co2_e,
soil_direct_N2O_tot_kg_co2_e,
soil_indirect_N2O_tot_kg_co2_e,
burning,
rice_production_methane_tot_kg_co2_e,
on_farm_fertilizer_emission,
"", rough_of_Soil_direct_N2O_tot_kg_co2_e,
rough_of_soil_indirect_N2O_tot_kg_co2_e,
rough_of_fertilizer_emission,
"", conc_of_Soil_direct_N2O_tot_kg_co2_e,
conc_of_soil_indirect_N2O_tot_kg_co2_e,
conc_of_fertilizer_emission,
"", conc_ip_Soil_direct_N2O_tot_kg_co2_e,
conc_ip_soil_indirect_N2O_tot_kg_co2_e,
conc_ip_fertilizer_emission),
kg_co2_e_per_kg_fpcm = c("", enteric_fermentation_methane_kg_co2_e_per_kg_fpcm,
manure_methane_kg_co2_e_per_kg_fpcm,
manure_direct_N2O_kg_co2_e_per_kg_fpcm,
manure_Indirect_N2O_kg_co2_e_per_kg_fpcm,
soil_direct_N2O_kg_co2_e_per_kg_fpcm,
soil_indirect_N2O_kg_co2_e_per_kg_fpcm,
burning_kg_co2_e_per_kg_fpcm,
rice_production_methane_kg_co2_e_per_kg_fpcm,
on_farm_fertilizer_emission_kg_co2_e_per_kg_fpcm,
"", rough_of_Soil_direct_N2O_kg_co2_e_per_kg_fpcm,
rough_of_soil_indirect_N2O_kg_co2_e_per_kg_fpcm,
rough_of_fertilizer_emission_kg_co2_e_per_kg_fpcm,
"", conc_of_Soil_direct_N2O_kg_co2_e_per_kg_fpcm,
conc_of_soil_indirect_N2O_kg_co2_e_per_kg_fpcm,
conc_of_fertilizer_emission_kg_co2_e_per_kg_fpcm,
"", conc_ip_Soil_direct_N2O_kg_co2_e_per_kg_fpcm,
conc_ip_soil_indirect_N2O_kg_co2_e_per_kg_fpcm,
conc_ip_fertilizer_emission_kg_co2_e_per_kg_fpcm)
)
View(ghg_balance)
View(nitrogen_balance)
feed_types <- unique(land_required[["land_requirements_all"]]$feed)
n_balance <- list()
for (feed in feed_types){
feed_production <- unnest(para[["feed_items"]], cols = c(feed_type_name))
feed_selected_frac <- land_required[["feed_items_frac"]] %>%
as.data.frame() %>%
dplyr::filter(feed_item_name == feed)
feed_selected <- feed_production[feed_production$feed_item_name == feed,]
# feed_production <- unnest(para[["feed_production"]], cols = c(feed_type_name))
#
# feed_production <- na_if(feed_production, "NA") %>%
# as.data.frame()
#
# feed_production[is.na(feed_production)] <- 0
#
# feed_selected <- feed_production[feed_production$feed_type_name == feed,]
dry_yield <- feed_selected_frac$dry_yield
residue_dry_yield <- feed_selected_frac$residue_dry_yield
main_n <- feed_selected_frac$main_n
residue_n <- as.numeric(feed_selected$residue_n)
n_fixing <- ifelse(feed_selected$category == "Legume", 0.5*(residue_n*residue_dry_yield+main_n*dry_yield)*1000, 0)
feed_selected_land_required <- land_required[["land_requirements_all"]][land_required[["land_requirements_all"]]$feed == feed,]
area_total <- sum(feed_selected_land_required$area_feed)
# feed_item_selected <- as.data.frame(feed_selected[["feed_items"]])
#
# feed_item_selected <- na_if(feed_item_selected, "NA") %>%
# as.data.frame()
#
# feed_item_selected[is.na(feed_item_selected)] <- 0
manure_fraction <- as.numeric(feed_selected$fraction_as_fertilizer)
# Computing fertilizer rate
ammonia_n_frac <- ifelse(feed_selected$ammonia==0,0,
as.numeric(para[["fertilizer"]][which(para[["fertilizer"]]$fertilizer_desc == "Ammonia"),]$fraction))
ammonium_nitrate_n_frac <- ifelse(feed_selected$ammonium_nitrate==0,0,
as.numeric(para[["fertilizer"]][which(para[["fertilizer"]]$fertilizer_desc == "Ammonium nitrate"),]$fraction))
ammonium_sulfate_n_frac <- ifelse(feed_selected$ammonium_sulfate==0,0,
as.numeric(para[["fertilizer"]][which(para[["fertilizer"]]$fertilizer_desc == "Ammonium sulfate"),]$fraction))
dap_n_frac <- ifelse(feed_selected$dap==0,0,
as.numeric(para[["fertilizer"]][which(para[["fertilizer"]]$fertilizer_desc == "DAP"),]$fraction))
n_solutions_n_frac <- ifelse(feed_selected$n_solutions==0,0,
as.numeric(para[["fertilizer"]][which(para[["fertilizer"]]$fertilizer_desc == "N solutions"),]$fraction))
npk_n_frac <- ifelse(feed_selected$npk==0,0,
as.numeric(para[["fertilizer"]][which(para[["fertilizer"]]$fertilizer_desc == "NPK"),]$fraction))
urea_n_frac <- ifelse(feed_selected$urea==0,0,
as.numeric(para[["fertilizer"]][which(para[["fertilizer"]]$fertilizer_desc == "Urea"),]$fraction))
fertilizer_rate <- (feed_selected$ammonia*ammonia_n_frac)+(feed_selected$ammonium_nitrate*ammonium_nitrate_n_frac)+(feed_selected$ammonium_sulfate*ammonium_sulfate_n_frac)+
(feed_selected$dap*dap_n_frac)+(feed_selected$n_solutions*n_solutions_n_frac)+(feed_selected$npk*npk_n_frac)+(feed_selected$urea*urea_n_frac)
main_product_removal <- as.numeric(feed_selected$main_product_removal)
residue_removal <- as.numeric(feed_selected$residue_removal)
sum_n_content_manure_grazing <- energy_required[["annual_results"]] %>%
as.data.frame() %>%
summarise(sum(n_content_manure_grazing)) %>%
as.numeric()
yield_dm_ha <- as.numeric(dry_yield)*1000
main_product_removed_kg_ha <- yield_dm_ha*main_product_removal
n_content_manure_collected <- energy_required[["annual_results"]] %>%
as.data.frame() %>%
summarise(sum(n_content_manure_collected)) %>%
as.numeric()
animal_manure_collected <- n_content_manure_collected*manure_fraction
organic_n_imported <- manure_fraction*(as.numeric(para$purchased_manure)+as.numeric(para$purchased_compost)+as.numeric(para$purchased_organic_n)+as.numeric(para$purchased_bedding))
crop_residue_dm_ha <- as.numeric(feed_selected_frac$residue_dry_yield)*1000
residue_removal <- as.numeric(feed_selected$residue_removal)
main_product_removed_kg <- area_total*main_product_removed_kg_ha
residue_removed_dm_ha <- crop_residue_dm_ha*residue_removal
residue_removed_kg <- area_total*residue_removed_dm_ha
annual_precipitation <- as.numeric(para[["annual_prec"]])
soil_n <- as.numeric(para[["soil_n"]])
ntot_kg_ha_20cm <- soil_n*20*as.numeric(para[["soil_bulk"]])*10
n_mineralized_kg_ha_year <- ntot_kg_ha_20cm*0.03
# Soil type
soil_type <- para[["soil_description"]]
# Soil carbon
soil_c <- as.numeric(para[["soil_c"]])
# Soil clay
soil_clay <- as.numeric(para[["soil_clay"]])
# N content (kg N/kg DM)
ncrop <- as.numeric(feed_selected$main_n)
# N content (kg N /kg DM)
nres <- as.numeric(feed_selected$residue_n)
# Mineral fertilizer
in1 <- area_total*fertilizer_rate
# calculate in2
# Atmospheric deposition
in3 <- 0.14*sqrt(annual_precipitation)*area_total
# Non-symbiotic N fixation
in4a <- ifelse(area_total > 0, (2 + (annual_precipitation - 1350) * 0.005) * area_total, 0)
# Symbiotic N-fixation
in4b <- n_fixing * area_total
# Crop yield (kgN)
out1 <- area_total*main_product_removed_kg_ha*ncrop
# Crop residue (KgN)
out2 <- ifelse(feed_selected$source_type == "Main", 0, residue_removed_dm_ha * nres * area_total)
# soil loss per plot per feed type
soil_loss_plot <- as.numeric(soil_erosion[soil_erosion$feed_type == feed,]$soil_loss_plot)
# Soil erosion
out5 <- soil_loss_plot*soil_n*1.5
# N content (kgN/kg DM ) from GHG parameters
nfertilizer <- 0 # to fox from GHG
# write data into a dataframe
n_balance[[feed]] <- as.data.frame(cbind(feed,
n_fixing,
area_total,
fertilizer_rate,
animal_manure_collected,
organic_n_imported,
yield_dm_ha,
crop_residue_dm_ha,
residue_removal,
main_product_removal,
main_product_removed_kg_ha,
main_product_removed_kg,
residue_removed_dm_ha,
residue_removed_kg,
annual_precipitation,
soil_n,
ntot_kg_ha_20cm,
n_mineralized_kg_ha_year,
soil_type,
soil_c,
soil_clay,
ncrop,
nres,
in1,
in3,
in4a,
in4b,
out1,
out2,
out5, nfertilizer))
}
n_balance_all <- n_balance %>%
bind_rows() %>%
mutate_at(c(-1, -19), as.numeric)
# Animal manure (N kg) grazing, Organic N (kg N) total, Organic N (kg N/ha) total
n_balance_all <- n_balance_all %>%
mutate(animal_manure_grazing = sum_n_content_manure_grazing * (main_product_removed_kg+residue_removed_kg)/(sum(n_balance_all$main_product_removed_kg)+sum(n_balance_all$residue_removed_kg)),
organic_n_kg_total = animal_manure_grazing+animal_manure_collected+organic_n_imported,
organic_n_kg_per_ha = ifelse(is.na(organic_n_kg_total/area_total), 0, organic_n_kg_total/area_total)) %>%
dplyr::mutate_if(is.numeric, list(~na_if(.,Inf))) %>%
replace(is.na(.), 0)
View(energy_required)
sum_n_content_manure_grazing <- energy_required[["annual_results"]] %>%
as.data.frame() %>%
summarise(sum(n_content_manure_grazing)) %>%
as.numeric()
sum_n_content_manure_grazing
view(energy_required[["annual_results"]])
energy_required[["annual_results"]]$n_content_manure_grazing
ghg_balance
# Global warming potential (CO2eq)
soil_on_farm <- ghg_balance[ghg_balance$GHG_balance == "Soil-Direct N2O", "kg_co2_e_per_ha"]
soil_on_farm
# Global warming potential (CO2eq)
soil_on_farm <- ghg_balance[ghg_balance$GHG_balance == "Soil-Direct N2O", "kg_co2_e_per_ha"][1]
# Global warming potential (CO2eq)
soil_on_farm <- as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Direct N2O", "kg_co2_e_per_ha"][1]) +
as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Indirect N2O", "kg_co2_e_per_ha"][1])
as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Direct N2O", "kg_co2_e_per_ha"][-1])
as.numeric(ghg_balance[ghg_balance$GHG_balance == "Manure-Methane", "kg_co2_e_per_ha"])
rice <- as.numeric(ghg_balance[ghg_balance$GHG_balance == "Rice production-Methane", "kg_co2_e_per_ha"])/1000
# Global warming potential (CO2eq)
soil_on_farm <- (as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Direct N2O", "kg_co2_e_per_ha"][1]) +
as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Indirect N2O", "kg_co2_e_per_ha"][1]))/1000
on_farm_table <- data.frame(
sources_and_sinks <- c("Soil","Off-farm Soil", "Liv. Manure", "Liv.enteric fermentation", "Burning", "Rice", "Fertilizer"),
t_CO2e_per_ha <- c(soil_on_farm, soil_off_farm, livestock_manure, livestock_enteric_fermentation, burning_emission, rice, fertilizer_on_farm)
)
# Global warming potential (CO2eq)
soil_on_farm <- (as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Direct N2O", "kg_co2_e_per_ha"][1]) +
as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Indirect N2O", "kg_co2_e_per_ha"][1]))/1000
soil_off_farm <- (sum(as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Direct N2O", "kg_co2_e_per_ha"][-1])) +
sum(as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Indirect N2O", "kg_co2_e_per_ha"][-1])))/1000
livestock_manure <- (as.numeric(ghg_balance[ghg_balance$GHG_balance == "Manure-Methane", "kg_co2_e_per_ha"]) +
as.numeric(ghg_balance[ghg_balance$GHG_balance == "Manure-Direct N2O", "kg_co2_e_per_ha"]) +
as.numeric(ghg_balance[ghg_balance$GHG_balance == "Manure-Indirect N2O", "kg_co2_e_per_ha"]))/1000
livestock_enteric_fermentation <- as.numeric(ghg_balance[ghg_balance$GHG_balance == "Enteric fermentation-Methane", "kg_co2_e_per_ha"])/1000
burning_emission <- as.numeric(ghg_balance[ghg_balance$GHG_balance == "Burning", "kg_co2_e_per_ha"])/1000
rice <- as.numeric(ghg_balance[ghg_balance$GHG_balance == "Rice production-Methane", "kg_co2_e_per_ha"])/1000
fertilizer_on_farm <- as.numeric(ghg_balance[ghg_balance$GHG_balance == "Production fertilizer", "kg_co2_e_per_ha"][1])/1000
soil_off_farm_rough <- (sum(as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Direct N2O", "kg_co2_e_per_ha"][2])) +
sum(as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Indirect N2O", "kg_co2_e_per_ha"][2])))/1000
fertilizer_off_farm_rough <- as.numeric(ghg_balance[ghg_balance$GHG_balance == "Production fertilizer", "kg_co2_e_per_ha"][2])/1000
soil_off_farm_conc <- (sum(as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Direct N2O", "kg_co2_e_per_ha"][3])) +
sum(as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Indirect N2O", "kg_co2_e_per_ha"][3])))/1000
fertilizer_off_farm_conc <- as.numeric(ghg_balance[ghg_balance$GHG_balance == "Production fertilizer", "kg_co2_e_per_ha"][3])/1000
soil_ip_farm_conc <- (sum(as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Direct N2O", "kg_co2_e_per_ha"][4])) +
sum(as.numeric(ghg_balance[ghg_balance$GHG_balance == "Soil-Indirect N2O", "kg_co2_e_per_ha"][4])))/1000
fertilizer_ip_farm_conc <- as.numeric(ghg_balance[ghg_balance$GHG_balance == "Production fertilizer", "kg_co2_e_per_ha"][4])/1000
on_farm_table <- data.frame(
sources_and_sinks <- c("Soil","Off-farm Soil", "Liv. Manure", "Liv.enteric fermentation", "Burning", "Rice", "Fertilizer"),
t_CO2e_per_ha <- c(soil_on_farm, soil_off_farm, livestock_manure, livestock_enteric_fermentation, burning_emission, rice, fertilizer_on_farm)
)
View(on_farm_table)
off_farm_table <- data.frame(
sources_and_sinks = c("Roughages off-farm", "Soil off-farm", "Fertilizer off-farm", "Concentrates off-farm", "Soil off-farm", "Fertilizer off-farm", "Imported concentrates", "Soil off-farm", "Fertilizer off-farm"),
t_CO2e_per_ha = c("", soil_off_farm_rough, fertilizer_off_farm_rough, "", soil_off_farm_conc, fertilizer_off_farm_conc, "", soil_ip_farm_conc, fertilizer_ip_farm_conc)
)
View(off_farm_table)
global_warming_potential <- rbind(data.frame(sources_and_sinks = c("On-farm"),t_CO2e_per_ha = c("")),
on_farm_table,
off_farm_table)
global_warming_potential <- rbind(data.frame(sources_and_sinks = c("On-farm"),t_CO2e_per_ha = c("")),
off_farm_table)
global_warming_potential <- rbind(data.frame(sources_and_sinks = c("On-farm"),t_CO2e_per_ha = c("")),
on_farm_table%>%mutate(t_CO2e_per_ha = as.character(t_CO2e_per_ha)),
off_farm_table)
View(global_warming_potential)
View(on_farm_table)
on_farm_table <- data.frame(
sources_and_sinks = c("Soil","Off-farm Soil", "Liv. Manure", "Liv.enteric fermentation", "Burning", "Rice", "Fertilizer"),
t_CO2e_per_ha = c(soil_on_farm, soil_off_farm, livestock_manure, livestock_enteric_fermentation, burning_emission, rice, fertilizer_on_farm)
)
View(on_farm_table)
global_warming_potential <- rbind(data.frame(sources_and_sinks = c("On-farm"),t_CO2e_per_ha = c("")),
on_farm_table%>%mutate(t_CO2e_per_ha = as.character(t_CO2e_per_ha)),
off_farm_table)
View(global_warming_potential)
on_farm_table <- data.frame(
sources_and_sinks = c("Soil","Off-farm Soil", "Liv. Manure", "Liv.enteric fermentation", "Burning", "Rice", "Fertilizer"),
t_CO2e_per_ha = c(soil_on_farm, soil_off_farm, livestock_manure, livestock_enteric_fermentation, burning_emission, rice, fertilizer_on_farm)
)
off_farm_table <- data.frame(
sources_and_sinks = c("Roughages off-farm", "Soil off-farm", "Fertilizer off-farm", "Concentrates off-farm", "Soil off-farm", "Fertilizer off-farm", "Imported concentrates", "Soil off-farm", "Fertilizer off-farm"),
t_CO2e_per_ha = c("", soil_off_farm_rough, fertilizer_off_farm_rough, "", soil_off_farm_conc, fertilizer_off_farm_conc, "", soil_ip_farm_conc, fertilizer_ip_farm_conc)
)
global_warming_potential <- rbind(data.frame(sources_and_sinks = c("On-farm"),t_CO2e_per_ha = c("")),
on_farm_table,
off_farm_table)
# Plotting GHG emission
on_farm_table %>%
ggplot2::ggplot(aes(x=sources_and_sinks, y=t_CO2e_per_ha))+
geom_bar(stat = "identity", width = 0.6)+
labs(x = "", y = "t CO2e/ha", title = "GHG emissions") +
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
on_farm_table %>%
ggplot2::ggplot(aes(x=sources_and_sinks, y=t_CO2e_per_ha))+
geom_bar(stat = "identity", width = 0.6)+
labs(x = "", y = "t CO2e/ha", title = "GHG emissions") +
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
ggsave(paste0(directoryPath, "/ghg_emission.png"), width = 150, height = 100, units = "mm")
# Plotting GHG emission
on_farm_table %>%
ggplot2::ggplot(aes(x=sources_and_sinks, y=t_CO2e_per_ha))+
geom_bar(stat = "identity", width = 0.6)+
labs(x = "", y = "t CO2e/ha", title = "GHG emissions") +
geom_text(aes(label = t_CO2e_per_ha), vjust = -0.5, size = 3) +
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Plotting GHG emission
on_farm_table %>%
ggplot2::ggplot(aes(x=sources_and_sinks, y=t_CO2e_per_ha))+
geom_bar(stat = "identity", width = 0.6)+
labs(x = "", y = "t CO2e/ha", title = "GHG emissions") +
geom_text(aes(label = t_CO2e_per_ha), vjust = -0.5, size = 3, angle = 45) +
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
on_farm_table %>%
ggplot2::ggplot(aes(x=sources_and_sinks, y=t_CO2e_per_ha))+
geom_bar(stat = "identity", width = 0.6)+
labs(x = "", y = "t CO2e/ha", title = "GHG emissions") +
geom_text(aes(label = t_CO2e_per_ha), vjust = -0.5, size = 3, angle = 45) +
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
ggsave(paste0(directoryPath, "/ghg_emission.png"), width = 150, height = 100, units = "mm")
# Plotting GHG emission
on_farm_table %>%
ggplot2::ggplot(aes(x=sources_and_sinks, y=t_CO2e_per_ha))+
geom_bar(stat = "identity", width = 0.6)+
labs(x = "", y = "t CO2e/ha", title = "GHG emissions") +
geom_text(aes(label = round(t_CO2e_per_ha, 2)), vjust = -0.5, size = 3, angle = 45) +
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
on_farm_table %>%
ggplot2::ggplot(aes(x=sources_and_sinks, y=t_CO2e_per_ha))+
geom_bar(stat = "identity", width = 0.6)+
labs(x = "", y = "t CO2e/ha", title = "GHG emissions") +
geom_text(aes(label = round(t_CO2e_per_ha, 2)), vjust = -0.5, size = 3, angle = 45) +
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
ggsave(paste0(directoryPath, "/ghg_emission.png"), width = 150, height = 100, units = "mm")
# Plotting land requirement
land_required %>%
group_by(feed, season_name) %>%
summarise(area_feed_total = sum(area_feed, na.rm = T)) %>%
ggplot2::ggplot(aes(x=feed, y=area_feed_total, fill=season_name))+
geom_bar(stat = "identity", width = 0.6)+
labs(x = "Feed Item", y = "Area (Ha)", fill = "Seasons", title = "Land Requirement and Feed Basket") +
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
###############################################################################################
## Soil impact
###############################################################################################
# Plotting N balance
nitrogen_balance %>%
group_by(feed) %>%
summarise(nbalance_kg_n_total = sum(nbalance_kg_n_total, na.rm = T)) %>%
ggplot2::ggplot(aes(x=feed, y=nbalance_kg_n_total))+
geom_bar(stat = "identity", width = 0.6)+
labs(x = "Feed Item", y = "Kg N", title = "Total Nitrogen Balance by Feed Item") +
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
###############################################################################################
## Soil impact
###############################################################################################
# Plotting N balance
nitrogen_balance %>%
group_by(feed) %>%
summarise(nbalance_kg_n_total = sum(nbalance_kg_n_total, na.rm = T)) %>%
ggplot2::ggplot(aes(x=feed, y=nbalance_kg_n_total))+
geom_bar(stat = "identity", width = 0.6)+
labs(x = "Feed Item", y = "Kg N", title = "Total Nitrogen Balance by Feed Item") +
geom_text(aes(label = round(nbalance_kg_n_total, 2)), vjust = -0.5, size = 3, angle = 45) +
theme_bw()+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
library(cleaned)
rm(list = ls())
# Desktop
# args = c("C:/Users/soloo/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/gui_test/Ramin/output.json",
# "C:/Users/Soloo/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/cleaned_tool/cleaned/inst/extdata/ghg_parameters.json",
# "C:/Users/Soloo/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/cleaned_tool/cleaned/inst/extdata/stock_change_parameters.json",
# "C:/Users/Soloo/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/cleaned_tool/cleaned/inst/extdata/energy_parameters.json",
# "C:/Users/soloo/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/outputs/output250723.json")
# Laptop
args = c("C:/Users/SOloo.CGIARAD/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/gui_test/Ramin/output.json",
"C:/Users/SOloo.CGIARAD/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/cleaned_tool/cleaned/inst/extdata/ghg_parameters.json",
"C:/Users/SOloo.CGIARAD/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/cleaned_tool/cleaned/inst/extdata/stock_change_parameters.json",
"C:/Users/SOloo.CGIARAD/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/cleaned_tool/cleaned/inst/extdata/energy_parameters.json",
"C:/Users/SOloo.CGIARAD/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/outputs/output250723.json")
para <- fromJSON(args[1], flatten = TRUE)
ghg_ipcc_data <- fromJSON(args[2], flatten = TRUE)
stock_change_para <- fromJSON(args[3], flatten = TRUE)
energy_parameters <- fromJSON(args[4], flatten = TRUE)
filePath <- args[5]
feed_basket_quality <- feed_quality(para)
energy_required <- energy_requirement(para, feed_basket_quality,energy_parameters)
land_required <- land_requirement(feed_basket_quality, energy_required, para)
soil_erosion <- soil_health(para, land_required)
water_required <- water_requirement(para,land_required)
nitrogen_balance <- n_balance(para, land_required, soil_erosion)
livestock_productivity <- land_productivity(para)
View(livestock_productivity)
library(cleaned)
args <- list("C:/Users/soloo/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/outputs/test060923/outFile.json",
"C:/Users/soloo/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/outputs/test060923/test1.json",
"C:/Users/soloo/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/outputs/test060923/test2.json")
outFile <- args[[1]]
oDir <- "C:/Users/soloo/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/outputs/test060923/plots/"
clean_plotting(args[1], args[2])
outFile <- jsonlite::fromJSON(outFile, flatten = TRUE)
library(tidyverse)
library(jsonlite)
outFile <- jsonlite::fromJSON(outFile, flatten = TRUE)
rm(list = ls())
args <- list("C:/Users/SOloo.CGIARAD/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/outputs/test060923/outFile.json",
"C:/Users/SOloo.CGIARAD/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/outputs/test060923/test1.json",
"C:/Users/SOloo.CGIARAD/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/outputs/test060923/test2.json")
outFile <- args[[1]]
oDir <- "C:/Users/SOloo.CGIARAD/OneDrive - CGIAR/Documents/ILRI work/Projects/CLEANED-XtRa/outputs/test060923/plots/"
outFile <- jsonlite::fromJSON(outFile, flatten = TRUE)
outFile$scenario <- as.factor(outFile$scenario)
oDir <- oDir
if (!file.exists(oDir)) {dir.create(oDir, recursive=T)}
for(i in 2:ncol(outFile)){
datos <- outFile %>% select(1, all_of(i))
# titles
tt <- colnames(datos[2])
if(tt == "total_milk_produced_kg_fpcm_per_year"){
title = "Total milk FPCM (kg/yr)"
}else if(tt == "total_meat_produced_kg_per_year"){
title = "Total meat (kg/yr)"
}else if(tt == "total_protein_produced_kg_per_year"){
title = "Total protein (kg/yr)"
}else if (tt == "total_tlu"){
title = "Tropical Livestock Unit"
}else if (tt == "total_land_requirement_ha"){
title = "Land required (ha/yr)"
}else if (tt == "total_land_requirement_ha_per_kg_fpcm"){
title = "Land required (ha/kg FPCM)"
}else if (tt == "total_land_requirement_ha_per_kg_meat"){
title = "Land required (ha/kg meat)"
}else if (tt == "total_land_requirement_ha_per_kg_protein"){
title = "Land required (ha/kg protein)"
}else if (tt == "total_land_requirement_ha_per_tlu"){
title = "Land required (ha/TLU)"
}else if (tt == "total_n_balance_kg_n_per_year"){
title = "N balance (kg N/yr)"
}else if (tt == "percent_area_mining"){
title = "Soil mining (%)"
}else if (tt == "percent_area_leaching"){
title = "Soil leaching (%)"
}else if (tt == "n_balance_kg_n_per_ha_per_year"){
title = "N balance (kg N/ha/yr)"
}else if (tt == "n_balance_kg_n_per_kg_fpcm"){
title = "N balance (kg N/kg FPCM)"
}else if (tt == "n_balance_kg_n_per_kg_meat"){
title = "N balance (kg N/kg meat)"
}else if (tt == "n_balance_kg_n_per_kg_protein"){
title = "N balance (kg N/kg protein)"
}else if (tt == "erosion_t_soil_year"){
title = "Erosion (t soil/yr)"
}else if (tt == "erosion_t_soil_per_ha_per_year"){
title = "Erosion (t soil/ha/yr)"
}else if (tt == "erosion_t_soil_per_kg_fpcm"){
title = "Erosion (t soil/kg FPCM)"
}else if (tt == "erosion_t_soil_per_kg_meat"){
title = "Erosion (t soil/kg meat)"
}else if (tt == "erosion_t_soil_per_kg_protein"){
title = "Erosion (t soil/kg protein)"
}else if (tt == "ghg_emission_t_co2_eq_per_year"){
title = "GHG (t CO2eq/yr)"
}else if (tt == "ghg_emission_t_co2_eq_per_ha_per_year"){
title = "GHG (t CO2eq/ha/yr)"
}else if (tt == "ghg_emission_t_co2_eq_per_kg_fpcm"){
title = "GHG (CO2eq/kg FPCM)"
}else if (tt == "ghg_emission_t_co2_eq_per_kg_meat"){
title = "GHG (CO2eq/kg meat)"
}else if (tt == "ghg_emission_t_co2_eq_per_kg_protein"){
title = "GHG (CO2eq/kg protein)"
}else if (tt == "percent_precipitation_used_for_feed_production"){
title = "% Precipitation use for feed production"
}else if (tt == "total_water_use_m3"){
title = "Water use (m3/yr)"
}else if (tt == "total_water_use_m3_per_ha"){
title = "Water use (m3/ha)"
}else if (tt == "total_water_use_m3_per_kg_fpcm"){
title = "Water use (m3/kg FPCM)"
}else if (tt == "total_water_use_m3_per_kg_meat"){
title = "Water use (m3/kg meat)"
}else if (tt == "total_water_use_m3_per_kg_protein"){
title = "Water (m3/kg protein)"
}else if (tt == "carbon_stock_change_t_co2eq_per_year"){
title = "Carbon stock changes (t CO2eq/yr)"
}else if (tt == "carbon_stock_change_t_co2eq_per_ha_per_year"){
title = "Carbon stock changes (t CO2eq/ha/yr)"
}else if (tt == "carbon_stock_change_t_co2eq_per_fpcm"){
title = "Carbon stock changes (t CO2eq/kg FPCM)"
}else if (tt == "carbon_stock_change_t_co2eq_per_meat"){
title = "Carbon stock changes (t CO2eq/kg meat)"
}else if (tt == "carbon_stock_change_t_co2eq_per_protein"){
title = "Carbon stock changes (t CO2eq/kg protein)"
}else if (tt == "total_milk_produced_energy_kcal_per_year"){
title = "Energy (kcal/kg FPCM)"
}else if (tt == "total_meat_produced_energy_kcal_per_year"){
title = "Energy (kcal/kg meat)"
}else if (tt == "total_milk_produced_ame_days_per_year"){
title = "AME (days/kg FPCM)"
}else if (tt == "total_meat_produced_ame_days_per_year"){
title = "AME (days/kg meat)"
}else if (tt == "total_carbon_balance_per_fpcm"){
title = "Carbon balance (t CO2eq/kg FPCM)"
}else if (tt == "total_carbon_balance_per_meat"){
title = "Carbon balance (t CO2eq/kg meat)"
}else if (tt == "total_carbon_balance_per_protein"){
title = "Carbon balance (t CO2eq/kg protein)"
}else{
NA
}
ggplot(datos, aes_string("scenario", y = tt, fill = "scenario")) +
geom_bar(stat = "identity",
position = "dodge",
width = 0.2) +
geom_text(aes(label = round(datos[,2],2)), position = position_dodge(width = 0.2), vjust = -0.5, size = 2) +
labs(x = "", y = tt, title = title, fill = "Scenario") +
theme_bw()
ggsave(paste0(oDir, tt, ".png"), width = 150, height = 100, units = "mm")
}
library(cleaned)
library(cleaned)
install.packages("roxygen2")
library(roxygen2)
library(cleaned)