Skip to content

Commit

Permalink
Improve TM source data to fix a FBS sugar crop issues with large resi…
Browse files Browse the repository at this point in the history
…dual term in 2016.

A few generate improvements also added.
Added svg in treemap
adding discussions of potential discrepancy in food calorie and macronutrient aggregation
  • Loading branch information
realxinzhao committed Dec 22, 2024
1 parent 5b3f8ba commit d93bbbb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
9 changes: 9 additions & 0 deletions R/xfaostat_L101_RawDataPreProc6_TM.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ module_xfaostat_L101_RawDataPreProc6_TM <- function(command, ...) {

}

# 12/20/2024 fix Raw cane or beet sugar (centrifugal only); item code 162 issue
# in iso == "moz" area code = 144 in 2016
# the export value was way too large and likely a unit mistake
# we scale it done by 1000 when the export value > 10^6

TM_bilateral_wide %>%
mutate(`2016` = if_else(item_code == 162 & source_code == 144 & `2016` > 10^6, `2016`/1000, `2016` )) ->
TM_bilateral_wide


return_data(MODULE_OUTPUTS)

Expand Down
37 changes: 34 additions & 3 deletions R/xfaostat_L107_FoodBalanceSheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,9 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {



#Section6 Connect food items and macronutrient rates ----
#Section3 Connect food items and macronutrient rates ----

# 6.1 Separate FAO food items into GCAM food items and NEC for macronutrient ----
# 3.1 Separate FAO food items into GCAM food items and NEC for macronutrient ----
# GCAM included most of the food items
# All food item with available macronutrient info from FAOSTAT are included

Expand Down Expand Up @@ -724,7 +724,7 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
SUA_Items_Food


# 6.2 Get macronutrient values ----
# 3.2 Get macronutrient values ----

### a. Get world average macronutrient ----
# For filling in missing values
Expand Down Expand Up @@ -781,7 +781,38 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
select(-macronutrient)->
L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus

# Potential discrepancy in food calorie and macronutrient aggregation (due to data quality)
# when the processing use of primary is zero while there is indeed secondary output and food consumption
# food in secondary products won't be converted to primary due to zero extraction rates.
# fortunately, there are only a few cases in small areas/sectors.
# One example is "Other citrus and products" in PCe in "bra"

L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus %>%
group_by(area_code, year, APE_comm_Agg, element) %>%
summarize(value = sum(value), .groups = "drop") %>%
# anti join ones with positive food mass in PCe
anti_join(
L107.Traceable_FBS_PCe_2010Plus %>%
filter(element == "Food", value >0) %>%
spread(element, value) %>% rename(area_code = region_ID),
by = c("area_code", "year", "APE_comm_Agg")
) %>%
# check remaining positive
filter(value > 0) %>%
distinct(area_code, year, APE_comm_Agg) %>%
mutate(ZeroFood = 0) ->
FBS_FOOD_SCALER

# After checks above, there were 30 obs; we change the calorie and macronutrient to zero in FBS
L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus %>%
left_join(FBS_FOOD_SCALER,
by = c("area_code", "year", "APE_comm_Agg")) %>%
mutate(value = if_else(is.na(ZeroFood), value, value *0)) %>%
select(-ZeroFood) ->
L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus


## Done Section3 ----
#****************************----
# Produce outputs ----

Expand Down
20 changes: 16 additions & 4 deletions R/xfaostat_visualization_funcs.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ chord_wrapper <- function(.DF,
#' @param .FigTitleSize Size of title font
#' @param .LastLabelCol Color of the last label
#' @param .SaveScaler A scaler controling the size (dpi = 300)
#' @param .SVG logical if true export SVG otherwise export PNG
#' @importFrom grDevices dev.off png
#' @description
#' See https://cran.r-project.org/web/packages/treemap/index.html for the details of the treemap function
Expand All @@ -99,18 +100,29 @@ treemap_wrapper <- function(.DF,
.LastLabelCol = "orange",
.SaveDir = "../man/figures",
.SaveName,
.SaveScaler = 1){
.SaveScaler = 1,
.SVG = F,
.SVG_w = 6,
.SVG_h = 8){

assertthat::assert_that(is.data.frame(.DF))
assertthat::assert_that(is.character(.SaveName))
assertthat::assert_that(is.character(.FigTitle))
#assertthat::assert_that(is.character(.FigTitle))
assertthat::assert_that(.Depth%%1==0)
assertthat::assert_that(is.numeric(.SaveScaler))
assertthat::assert_that(is.logical(.SVG))


# treemap save to a path
png(filename= file.path(.SaveDir, paste0(.SaveName,".png")), res = 300,
width= 2500 * .SaveScaler, height= 1800 * .SaveScaler )

if (.SVG == TRUE) {
svg(filename= file.path(.SaveDir, paste0(.SaveName,".svg")), width = .SVG_w, height = .SVG_h) }
else{
png(filename= file.path(.SaveDir, paste0(.SaveName,".png")), res = 300,
width= 2500 * .SaveScaler, height= 1800 * .SaveScaler )
}


# treemap
treemap(.DF,
index= names(.DF)[1:.Depth],
Expand Down

0 comments on commit d93bbbb

Please sign in to comment.