From c7190dbba759d0af4b62836ee92ec2ae514c5832 Mon Sep 17 00:00:00 2001
From: Peter Alexander <peter@blackhillock.co.uk>
Date: Sat, 19 Jan 2019 16:39:36 +0000
Subject: [PATCH] Marginal gains plot format changes

---
 PLUM/SetupData/UclProjections.R        |  2 +-
 altLU/MarginalGains.R                  | 31 +++++++++++++++++------
 altLU/{ForAlmut.R => RegionalLosses.R} |  0
 altLU/plotMarginalGains.R              | 34 +++++++++++++-------------
 4 files changed, 41 insertions(+), 26 deletions(-)
 rename altLU/{ForAlmut.R => RegionalLosses.R} (100%)

diff --git a/PLUM/SetupData/UclProjections.R b/PLUM/SetupData/UclProjections.R
index d49ffaa..a2c4348 100644
--- a/PLUM/SetupData/UclProjections.R
+++ b/PLUM/SetupData/UclProjections.R
@@ -20,7 +20,7 @@ baseConsump = baseConsump[, list(Country, Item, baseCpc=food/population, baseFoo
 
 dt = merge(consumpChange, baseConsump, by=c("Country", "Item"))[, list(Country, Year, Item, SspModel=Model, Scenario, population, cpcAdj, baseCpc, baseFood, cpc=baseCpc*cpcAdj, food=baseCpc*cpcAdj*population)]
 
-write.table(dt, "~/Download/foodItemProjections.csv", row.names=FALSE, sep=",")
+write.table(dt[, list(Country, Year, Item, SspModel, Scenario, foodPerCapita=cpc, food)], "~/Downloads/foodItemProjections.csv", row.names=FALSE, sep=",")
 
 
 # FIGURE PLOT
diff --git a/altLU/MarginalGains.R b/altLU/MarginalGains.R
index e894f58..3a629bd 100644
--- a/altLU/MarginalGains.R
+++ b/altLU/MarginalGains.R
@@ -1,3 +1,5 @@
+require(ggplot2)
+
 ##MARGINAL GAINS##
 regional = merge(con_prod, fread(paste0(data_dir_root, "/HALF/gustavsson_country_regions.csv")), by=c('Country'))
 colsToSum=c('feed', 'food', 'used', 'feed_area', 'food_area', 'animal_a_feed', 'animal_a_pasture') 
@@ -192,27 +194,40 @@ calcMarginGains = function(cp, rp, act, aYear=2013) {
 	
 	
 	if (FALSE) {
-		dt = merge(regionalRes[, list(chg=sum(saved_area)), by="Region"], adjusted_diet2[Item == "Wheat", list(Region, population)], by="Region")
+		dt = merge(regionalRes[, list(chg=sum(saved_area)), by=c("Region", "variable")], adjusted_diet2[Item == "Wheat", list(Region, population)], by="Region")
 		org_area= adjusted_diet2[, list(food=sum(crop_food_req* population* quantity_person_year, na.rm=TRUE),
 						feed=sum(feed_req* population* quantity_person_year, na.rm=TRUE),
 						pasture=sum(pasture_req* population* quantity_person_year, na.rm=TRUE)), by=Region]
-		dt=merge(dt, org_area[, list(Region, base_food_area=food+feed+ pasture)], by="Region")
-		dt2 = melt(dt[, list(Region, base_perperson=base_food_area/population, updated_perperson=base_food_area/population-chg/population)], id.vars="Region")
-		levels(dt2$variable) = c("2013 baseline", "After marginal gains")
+		org_area_melt = melt(org_area, id.vars="Region", value.name="base")
+		
+		org_area_melt$variable = factor(org_area_melt$variable, levels=c("pasture", "feed", "food"))
+		levels(org_area_melt$variable) = c("Pasture", "Cropland for feed", "Cropland for food")
 		
-		ggplot(updateRegionNames(dt2), aes(x=variable, y=value, fill=variable)) + geom_bar(stat="identity") + 
+		dt=merge(dt, org_area_melt, by=c("Region", "variable"))
+		dt2 = melt(dt[, list(Region, variable, base_perperson=base/population, updated_perperson=(base-chg)/population)], 
+				id.vars=c("Region", "variable"), variable.name="dataType")
+		levels(dt2$dataType) = c("Before", "After")
+		dt2$variable = factor(dt2$variable, levels=c("Pasture", "Cropland for feed", "Cropland for food"))
+		
+		ggplot(updateRegionNames(dt2), aes(x=dataType, y=value, fill=variable, alpha=dataType)) + geom_bar(stat="identity") + 
 				facet_grid(. ~ Region) +
 				theme_minimal() + 
 				labs(x="", y="Food area (ha/person)") +
+				scale_fill_manual(values=c("#238b45","#cc4c02", "#fec44f")) + 
+				scale_alpha_manual(values=c(0.5, 0.9), guide = FALSE) +
 				theme(text = element_text(size=12),
 						legend.position="bottom",
+						axis.text.x = element_text(size=10),
 						legend.title=element_blank(),,
-						axis.text.x = element_blank(),
 						panel.grid.minor.y = element_blank(), 
 						panel.grid.major.x = element_blank(), 
 						panel.grid.minor.x = element_blank(), 
 						panel.background = element_rect(colour = "black", size =.4)) +
-				ggsave(file=file.path("/Users/peteralexander/Documents/LURG/Papers/Marginal gains/Figures", "area_per_person.pdf"),width=10,height=5.5)
+				ggsave(file=file.path("/Users/peteralexander/Documents/LURG/Papers/Marginal gains/Figures", "area_per_person.pdf"),width=9,height=5.5)
+		
+		dt3 = merge(dt2[dataType == "Before", list(before=sum(value)), by=list(Region)],  dt2[dataType == "After", list(after=sum(value)), by=list(Region)], by="Region")
+		dt3[, diff:=before-after]
+		dt3[, pc:=diff/before]
 	}
 	
 	list(regionalRes=regionalRes, worldRes=worldRes)
@@ -320,7 +335,7 @@ ggplot(marginalGainsByRate, aes(x=rate*100, y=agri_t)) + geom_line() +
 marginalGainsUniformAdjust = function(act, lower=0, upper=2, fileName) {
 	marginalGainsSamples= data.table()
 	
-	for (i in seq(1, 400)) {
+	for (i in seq(1, 1000)) {
 		actionsDtC = copy(act)
 		print (i)
 		for (chgType in actionsDtB$changeType) {
diff --git a/altLU/ForAlmut.R b/altLU/RegionalLosses.R
similarity index 100%
rename from altLU/ForAlmut.R
rename to altLU/RegionalLosses.R
diff --git a/altLU/plotMarginalGains.R b/altLU/plotMarginalGains.R
index a5668b8..237c30f 100644
--- a/altLU/plotMarginalGains.R
+++ b/altLU/plotMarginalGains.R
@@ -18,7 +18,7 @@ dtToPlot = updateRegionNames(dtToPlot)
 dtToPlot$typesToPlot = factor(dtToPlot$typesToPlot, levels=rev(c("Consumer change: Cropland for food", "Consumer change: Cropland for feed", "Consumer change: Pasture", 
 				"Supply change: Cropland for food", "Supply change: Cropland for feed", "Supply change: Pasture")))
 
-ggplot(dtToPlot, aes(x=Region, y= saved_area, fill= typesToPlot)) + geom_bar(stat="identity") + 
+ggplot(dtToPlot, aes(x=Region, y= saved_area, fill= typesToPlot)) + geom_bar(stat="identity", alpha=0.9) + 
 		labs(x="", y="Reduction in land for food (Mha)") +
 		scale_fill_manual(values =c("#43a2ca", "#7bccc4", "#bae4bc", "#e34a33","#fc8d59",  "#fdcc8a"), guide = guide_legend(ncol = 2)) +
 		theme_minimal() + 
@@ -30,7 +30,7 @@ ggplot(dtToPlot, aes(x=Region, y= saved_area, fill= typesToPlot)) + geom_bar(sta
 				panel.grid.major.x = element_blank(), 
 				panel.grid.minor.x = element_blank(), 
 				panel.background = element_rect(colour = "black", size =.4)) +
-		ggsave(file=file.path("/Users/peteralexander/Documents/LURG/Papers/Marginal gains/Figures", "regional_area_chg.pdf"),width=9,height=6)
+		ggsave(file=file.path("/Users/peteralexander/Documents/LURG/Papers/Marginal gains/Figures", "regional_area_chg.pdf"),width=9,height=5.8)
 
 
 marginalGainByGroup[rateAdj==0, rateType:=factor("2013 baseline")]
@@ -60,25 +60,24 @@ plotDecomposition = function(mgByGroup) {
 	
 	# crop / pasture percentage change in area ### CHANGE COLOURS, pink, purple, blue??
 	mg_chg2<-mg_chg[, list(Region, variable, baseVal, actionGroup, rateAdj, decompVal)]
-	levels(mg_chg2$variable)<- c("Pasture", "Cropland for feed", "Cropland for food")
+	levels(mg_chg2$variable)<- c("Pasture", "Cropland\nfor feed", "Cropland\nfor food")
 	mg_chg2$variable <- factor(mg_chg2$variable, levels=rev(levels(mg_chg2$variable)))
 	
-	mg_chg3 = rbind(mg_chg2, mg_chg2[, list(variable="All agricultural land", decompVal=sum(decompVal), baseVal=sum(baseVal)), by=list(Region, rateAdj, actionGroup)])
+	mg_chg3 = rbind(mg_chg2, mg_chg2[, list(variable="Agricultural\nland", decompVal=sum(decompVal), baseVal=sum(baseVal)), by=list(Region, rateAdj, actionGroup)])
 	mg_chg3[, decompPercent := decompVal/baseVal * 100]
 	
-	ggplot(mg_chg3[rateAdj==1], aes(x= variable, y= decompPercent, fill= actionGroup)) + geom_bar(stat="identity") + 
-			labs(x="", y="Percentage reduction in HALF index") +
-			scale_fill_manual(values =c("#377eb8","#4daf4a",  "#e41a1c")) +
+	ggplot(mg_chg3[rateAdj==1], aes(x= variable, y= decompPercent, fill= actionGroup)) + geom_bar(stat="identity", alpha=0.9) + 
+			labs(x="", y="Percentage reduction in land for food") +
+			scale_fill_manual(values =c("#377eb8","#4daf4a",  "#e41a1c"), guide = guide_legend(ncol = 2)) +
 			theme_minimal() + 
-			theme(text = element_text(size=10),
+			theme(legend.position="bottom",
 					legend.title=element_blank(),
-					legend.text=element_text(size=10),
 					axis.text.x = element_text(size=10),
 					panel.grid.minor.y = element_blank(), 
 					panel.grid.major.x = element_blank(), 
 					panel.grid.minor.x = element_blank(), 
 					panel.background = element_rect(colour = "black", size =.4)) +
-			ggsave(file=file.path("/Users/peteralexander/Documents/LURG/Papers/Marginal gains/Figures", "mg_area_chg_pc.pdf"),width=8,height=5)	
+			ggsave(file=file.path("/Users/peteralexander/Documents/LURG/Papers/Marginal gains/Figures", "mg_area_chg_pc.pdf"),width=5,height=5)	
 }
 plotDecomposition(marginalGainByGroup)
 
@@ -88,9 +87,10 @@ dtToPlot[, variable:=factor(variable, levels=c('pasture_t', 'feed_t', 'food_crop
 dtToPlot = dtToPlot[order(variable)]
 historicalCrop = marginalGainByGroup[actionGroup=="All" & rateAdj==0, feed_t+ food_crop_t]
 historicalAgri = marginalGainByGroup[actionGroup=="All" & rateAdj==0,  pasture_t + feed_t + food_crop_t]
+level(dtToPlot$rateType) = c("2013\nBaseline", "Low", "Moderate", "High")
 
 ggplot() + 
-		geom_bar(data = dtToPlot, aes(x=rateType, y=value, fill=variable), stat="identity") +
+		geom_bar(data = dtToPlot, aes(x=rateType, y=value, fill=variable), alpha=0.9, stat="identity") +
 		geom_line(data = data.table(y=c(historicalCrop, historicalCrop), x=c(0.5, length(unique(dtToPlot$rateType))+0.5)), 
 				aes(x=x, y=y, linetype="Cropland area in 2013 ")) +
 		geom_line(data = data.table(y=c(historicalAgri, historicalAgri), x=c(0.5, length(unique(dtToPlot$rateType))+0.5)), 
@@ -98,17 +98,17 @@ ggplot() +
 		labs(x="", y="Percentage global land for food production (%)") +
 		scale_y_continuous(sec.axis = sec_axis(~.*13000/100, name = "Land area for food production (Mha)")) + 
 		theme_minimal() + 
-		scale_linetype_manual(values = c("dashed", "dotted")) + 
-		scale_fill_manual(values=c("mediumseagreen","chocolate1", "khaki1"), labels = c("Pasture", "Cropland for feed", "Cropland for food")) + 
+		scale_linetype_manual(values = c("dashed", "dotted"), guide = guide_legend(nrow = 2)) + 
+		scale_fill_manual(values=c("#238b45","#cc4c02", "#fec44f"), labels = c("Pasture", "Cropland for feed", "Cropland for food"), guide = guide_legend(ncol = 2)) + 
 		scale_colour_manual(values = c("grey", "black"), guide = FALSE) + 
-		theme(text = element_text(size=10),
+		theme(legend.position="bottom",
 				legend.title=element_blank(),
 				axis.text.x = element_text(size=10),
 				panel.grid.minor.y = element_blank(), 
 				panel.grid.major.x = element_blank(), 
 				panel.grid.minor.x = element_blank(), 
 				panel.background = element_rect(colour = "black", size =.4)) + 
-		ggsave(file=file.path("/Users/peteralexander/Documents/LURG/Papers/Marginal gains/Figures", "mg_area.pdf"),width=8,height=5)
+		ggsave(file=file.path("/Users/peteralexander/Documents/LURG/Papers/Marginal gains/Figures", "mg_area.pdf"),width=5,height=5)
 
 ########
 
@@ -143,11 +143,11 @@ amg_chg4 = merge(amg_chg2, actionsDt[, list(ID, changeType, actionGroup)], by='c
 amg_chg4[, displayName:=paste0(ID, '. ', changeType)]
 amg_chg4$displayName <- factor(amg_chg4$displayName, levels = unique(amg_chg4[order(ID, decreasing=TRUE)]$displayName))
 
-ggplot(amg_chg4, aes(x=displayName, y=percred, fill=colr)) + geom_bar(stat="identity", width=0.5) + 
+ggplot(amg_chg4, aes(x=displayName, y=percred, fill=colr)) + geom_bar(stat="identity", width=0.5, alpha=0.9) + 
 		facet_grid(actionGroup~variable, space="free_y", scale="free_y") +
 		coord_flip()+
 		scale_fill_manual(values=c("firebrick1","royalblue1"))+
-		labs(x="", y="Reduce in land area (%)") +
+		labs(x="", y="Reduction in land area (%)") +
 		theme_minimal() + 
 		theme(strip.text.x = element_text(size = 9))+
 		theme(strip.text.y = element_text(size = 10))+
-- 
GitLab