Skip to content

Commit d93bbbb

Browse files
committed
Improve TM source data to fix a FBS sugar crop issues with large residual term in 2016.
A few generate improvements also added. Added svg in treemap adding discussions of potential discrepancy in food calorie and macronutrient aggregation
1 parent 5b3f8ba commit d93bbbb

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

R/xfaostat_L101_RawDataPreProc6_TM.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ module_xfaostat_L101_RawDataPreProc6_TM <- function(command, ...) {
129129

130130
}
131131

132+
# 12/20/2024 fix Raw cane or beet sugar (centrifugal only); item code 162 issue
133+
# in iso == "moz" area code = 144 in 2016
134+
# the export value was way too large and likely a unit mistake
135+
# we scale it done by 1000 when the export value > 10^6
136+
137+
TM_bilateral_wide %>%
138+
mutate(`2016` = if_else(item_code == 162 & source_code == 144 & `2016` > 10^6, `2016`/1000, `2016` )) ->
139+
TM_bilateral_wide
140+
132141

133142
return_data(MODULE_OUTPUTS)
134143

R/xfaostat_L107_FoodBalanceSheet.R

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,9 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
686686

687687

688688

689-
#Section6 Connect food items and macronutrient rates ----
689+
#Section3 Connect food items and macronutrient rates ----
690690

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

@@ -724,7 +724,7 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
724724
SUA_Items_Food
725725

726726

727-
# 6.2 Get macronutrient values ----
727+
# 3.2 Get macronutrient values ----
728728

729729
### a. Get world average macronutrient ----
730730
# For filling in missing values
@@ -781,7 +781,38 @@ module_xfaostat_L107_FoodBalanceSheet <- function(command, ...) {
781781
select(-macronutrient)->
782782
L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus
783783

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

790+
L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus %>%
791+
group_by(area_code, year, APE_comm_Agg, element) %>%
792+
summarize(value = sum(value), .groups = "drop") %>%
793+
# anti join ones with positive food mass in PCe
794+
anti_join(
795+
L107.Traceable_FBS_PCe_2010Plus %>%
796+
filter(element == "Food", value >0) %>%
797+
spread(element, value) %>% rename(area_code = region_ID),
798+
by = c("area_code", "year", "APE_comm_Agg")
799+
) %>%
800+
# check remaining positive
801+
filter(value > 0) %>%
802+
distinct(area_code, year, APE_comm_Agg) %>%
803+
mutate(ZeroFood = 0) ->
804+
FBS_FOOD_SCALER
805+
806+
# After checks above, there were 30 obs; we change the calorie and macronutrient to zero in FBS
807+
L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus %>%
808+
left_join(FBS_FOOD_SCALER,
809+
by = c("area_code", "year", "APE_comm_Agg")) %>%
810+
mutate(value = if_else(is.na(ZeroFood), value, value *0)) %>%
811+
select(-ZeroFood) ->
812+
L107.Traceable_FBS_Food_Calorie_Macronutrient_2010Plus
813+
814+
815+
## Done Section3 ----
785816
#****************************----
786817
# Produce outputs ----
787818

R/xfaostat_visualization_funcs.R

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ chord_wrapper <- function(.DF,
8484
#' @param .FigTitleSize Size of title font
8585
#' @param .LastLabelCol Color of the last label
8686
#' @param .SaveScaler A scaler controling the size (dpi = 300)
87+
#' @param .SVG logical if true export SVG otherwise export PNG
8788
#' @importFrom grDevices dev.off png
8889
#' @description
8990
#' See https://cran.r-project.org/web/packages/treemap/index.html for the details of the treemap function
@@ -99,18 +100,29 @@ treemap_wrapper <- function(.DF,
99100
.LastLabelCol = "orange",
100101
.SaveDir = "../man/figures",
101102
.SaveName,
102-
.SaveScaler = 1){
103+
.SaveScaler = 1,
104+
.SVG = F,
105+
.SVG_w = 6,
106+
.SVG_h = 8){
103107

104108
assertthat::assert_that(is.data.frame(.DF))
105109
assertthat::assert_that(is.character(.SaveName))
106-
assertthat::assert_that(is.character(.FigTitle))
110+
#assertthat::assert_that(is.character(.FigTitle))
107111
assertthat::assert_that(.Depth%%1==0)
108112
assertthat::assert_that(is.numeric(.SaveScaler))
113+
assertthat::assert_that(is.logical(.SVG))
109114

110115

111116
# treemap save to a path
112-
png(filename= file.path(.SaveDir, paste0(.SaveName,".png")), res = 300,
113-
width= 2500 * .SaveScaler, height= 1800 * .SaveScaler )
117+
118+
if (.SVG == TRUE) {
119+
svg(filename= file.path(.SaveDir, paste0(.SaveName,".svg")), width = .SVG_w, height = .SVG_h) }
120+
else{
121+
png(filename= file.path(.SaveDir, paste0(.SaveName,".png")), res = 300,
122+
width= 2500 * .SaveScaler, height= 1800 * .SaveScaler )
123+
}
124+
125+
114126
# treemap
115127
treemap(.DF,
116128
index= names(.DF)[1:.Depth],

0 commit comments

Comments
 (0)