for (i in unique(dat.other$panel_num)) {
tmp.dat <-
rbind(dat.low,dat.other  %>% filter(panel_num %in% i) %>% dplyr::select(-panel_num)) %>%
mutate(Retailer_Department_Aisle = paste0(Retailer,Department,Aisle)) %>%
transform(Retailer_Department_Aisle = factor(Retailer_Department_Aisle, levels = .$Retailer_Department_Aisle)) %>%
mutate(panel_num = i) %>%
mutate(position_x = 1:nrow(.))
out.dat <- rbind(out.dat, tmp.dat)
plot.list.tot[[i]] <-
ggplot(tmp.dat, aes(x = Retailer_Department_Aisle, y = Tot_env_100g_scaled, colour = Retailer)) +
theme_classic() +
# geom_point(alpha = .5, colour = tmp.dat$colour) +
geom_point(alpha = .75, colour = tmp.dat$colour) +
# geom_errorbar(aes(ymin = (Tot_env_100g_scaled-Tot_env_100g_scaled_se), ymax = (Tot_env_100g_scaled+Tot_env_100g_scaled_se)), colour = tmp.dat$colour, alpha = .5) +
geom_errorbar(aes(ymin = (Tot_env_100g_scaled-Tot_env_100g_scaled_se), ymax = (Tot_env_100g_scaled+Tot_env_100g_scaled_se)), alpha = .75, colour = tmp.dat$colour) +
# geom_point(aes(y = overlap),shape = 8, colour = 'black') +
# geom_point(aes(x = Department_Aisle_Shelf, y = scaled_tot_env), colour = 'blue') +
# geom_errorbar(aes(ymin = scaled_tot_env_lower, ymax = scaled_tot_env_upper), colour = 'blue') +
# scale_x_discrete(labels = substr(tmp.dat$Aisle,1,10)) +
scale_x_discrete(labels = substr(tmp.dat$Aisle,1,20)) +
# scale_colour_manual(values = unique(tmp.dat$colour)) +
# scale_colour_manual(values = sort(unique(dat.other$colour[dat.other$Retailer %in% tmp.dat$Retailer]))) +
# scale_y_continuous(trans = 'log10') +
theme(legend.position = NULL) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .5, size = 6)) +
labs(x = NULL,y = 'Environmental Impact Score', colour = NULL) +
geom_vline(xintercept = 10, linetype = 2) +
theme(legend.position = 'bottom')
}
ggplot(out.dat, aes(x = position_x, y = Tot_env_100g_scaled, colour = Retailer)) +
theme_classic() +
# geom_point(alpha = .5, colour = tmp.dat$colour) +
geom_point(alpha = .75, colour = tmp.dat$colour) +
# geom_errorbar(aes(ymin = (Tot_env_100g_scaled-Tot_env_100g_scaled_se), ymax = (Tot_env_100g_scaled+Tot_env_100g_scaled_se)), colour = tmp.dat$colour, alpha = .5) +
geom_errorbar(aes(ymin = (Tot_env_100g_scaled-Tot_env_100g_scaled_se), ymax = (Tot_env_100g_scaled+Tot_env_100g_scaled_se)), alpha = .75, colour = tmp.dat$colour) +
# geom_point(aes(y = overlap),shape = 8, colour = 'black') +
# geom_point(aes(x = Department_Aisle_Shelf, y = scaled_tot_env), colour = 'blue') +
# geom_errorbar(aes(ymin = scaled_tot_env_lower, ymax = scaled_tot_env_upper), colour = 'blue') +
# scale_x_discrete(labels = substr(tmp.dat$Aisle,1,10)) +
scale_x_continuous(breaks = out.dat$position_x,labels = substr(out.dat$Aisle,1,20)) +
# scale_colour_manual(values = unique(tmp.dat$colour)) +
# scale_y_continuous(trans = 'log10') +
theme(legend.position = NULL) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0, size = 6)) +
labs(x = NULL,y = 'Environmental Impact Score', colour = NULL) +
geom_vline(xintercept = 10, linetype = 2) +
theme(legend.position = 'bottom') +
facet_wrap(.~panel_num, nrow = 5, scales = 'free_y')
out.plot.tot <-
plot_grid(plot.list.tot[[1]],plot.list.tot[[2]],plot.list.tot[[3]],plot.list.tot[[4]], plot.list.tot[[5]], plot.list.tot[[6]], nrow = length(plot.list.tot), align = 'hv')
dev.off()
out.plot.tot
Sys.sleep(15)
ggsave(paste0(getwd(),'/Figure S6 Impacts by Aisle.pdf'),
width = 7.2, height = 15, units = 'in')
### Getting impacts for chocolate and coffee
setwd("/Volumes/Citadel/Oxford/Research Projects/Env and Health Snapshot of FoodDB/Runs 19April2021")
# monte carlo function for lca estimates
monte.carlo.lca <-
function(product.list) {
# setting for reproducability
set.seed(19)
# updating to work with code below
lca <- lca.dat %>% filter(!is.na(Weight))
# indicators to be used to update name later...
indicators <- names(lca)[!(names(lca) %in% c('Food_Category','Weight'))]
# impacts
names(lca)[!(names(lca) %in% c('Food_Category','Weight'))] <-
paste0('impact_',1:length(indicators))
# and adjusting units on lca estimates
lca <-
lca %>%
mutate(impact_1 = impact_1/10,
impact_2 = impact_2/10,
impact_3 = impact_3/10,
impact_4 = impact_4/10,
impact_5 = impact_5/10,
impact_6 = impact_6/10)
# Number of reps
n_rep = 1000
# Fifth and 95th percentiles
fifth.obs = round(n_rep * .05, digits = 0)
fifty.obs = round(n_rep * .50, digits = 0)
ninetyfifth.obs = round(n_rep * .95, digits = 0)
# tmp food df
# filtering to avoid processing power if doing this in parallel
tmp.food.df <-
food.df %>%
filter(product_name %in% product.list) %>%
mutate(amount = amount / 100) # adjusting to proportional composition
# Creating data frame
out.df <- matrix(nrow = length(product.list), ncol = 56)
out.df <- as.data.frame(out.df)
names(out.df) <- c('product_name','id',paste0('mean_',1:6),paste0('se_',1:6),paste0('lower_ci_',1:6),paste0('upper_ci_',1:6),paste0('min_',1:6),paste0('max_',1:6),paste0('lower_fifth_',1:6),paste0('upper_ninetyfifth_',1:6), paste0('median_',1:6))
out.df$product_name <- product.list
# LCA baseline for scaling impacts
# This isn't quite perfect, but it is close
lca.scaling <-
lca %>%
mutate(impact_1 = impact_1 * Weight/100,
impact_2 = impact_2 * Weight/100,
impact_3 = impact_3 * Weight/100,
impact_4 = impact_4 * Weight/100,
impact_5 = impact_5 * Weight/100,
impact_6 = impact_6 * Weight/100) %>%
group_by(Food_Category) %>%
dplyr::summarise(Weight = sum(Weight)/100,
impact_1 = sum(impact_1),
impact_2 = sum(impact_2),
impact_3 = sum(impact_3),
impact_4 = sum(impact_4),
impact_5 = sum(impact_5),
impact_6 = sum(impact_6)) %>%
mutate(impact_1 = impact_1 * (1/Weight),
impact_2 = impact_2 * (1/Weight),
impact_3 = impact_3 * (1/Weight),
impact_4 = impact_4 * (1/Weight),
impact_5 = impact_5 * (1/Weight),
impact_6 = impact_6 * (1/Weight))
# looping across foods
for(i in product.list) {
# selecting one producer from the category
tmp.lca.sample <-
lca %>%
filter(Food_Category %in% tmp.food.df$Food_Category[tmp.food.df$product_name %in% i]) %>%
group_by(Food_Category) %>%
sample_n(n_rep, weight = Weight, replace = TRUE) %>%
.[order(.$Food_Category),] %>%
as.data.frame(.) %>%
mutate(sample_index = rep(1:n_rep, length(unique(.$Food_Category))))
# Multiplying
tmp.food <-
left_join(tmp.food.df %>% filter(product_name %in% i), tmp.lca.sample) %>%
mutate(tmp.impact_1 = amount * impact_1,
tmp.impact_2 = amount * impact_2,
tmp.impact_3 = amount * impact_3,
tmp.impact_4 = amount * impact_4,
tmp.impact_5 = amount * impact_5,
tmp.impact_6 = amount * impact_6)
# getting mean and sd and ci
mean.se.ci <-
tmp.food %>%
group_by(product_name, id, sample_index) %>%
dplyr::summarise(tmp.impact_1 = sum(tmp.impact_1, na.rm = TRUE),
tmp.impact_2 = sum(tmp.impact_2, na.rm = TRUE),
tmp.impact_3 = sum(tmp.impact_3, na.rm = TRUE),
tmp.impact_4 = sum(tmp.impact_4, na.rm = TRUE),
tmp.impact_5 = sum(tmp.impact_5, na.rm = TRUE),
tmp.impact_6 = sum(tmp.impact_6, na.rm = TRUE)) %>%
as.data.frame(.) %>%
mutate(scaled_1 = tmp.impact_1/max(lca.scaling$impact_1), # scaling to get min and max
scaled_2 = tmp.impact_2/max(lca.scaling$impact_2),
scaled_3 = tmp.impact_3/max(lca.scaling$impact_3),
scaled_4 = tmp.impact_4/max(lca.scaling$impact_4),
scaled_5 = tmp.impact_5/max(lca.scaling$impact_5),
scaled_6 = tmp.impact_6/max(lca.scaling$impact_6)) %>%
mutate(tot_scaled = (scaled_1 + scaled_2 + scaled_3 + scaled_4) / 4)
# Ordering mean.se.ci first
mean.se.ci <- mean.se.ci[order(mean.se.ci$tot_scaled),]
# Getting min and max
# Df is sorted, so just need to take first and last row
min.food <-
mean.se.ci[1,] %>% dplyr::select(product_name, id, min.impact_1 = tmp.impact_1, min.impact_2 = tmp.impact_2, min.impact_3 = tmp.impact_3, min.impact_4 = tmp.impact_4, min.impact_5 = tmp.impact_5, min.impact_6 = tmp.impact_6)
max.food <-
mean.se.ci[n_rep,] %>% dplyr::select(product_name, id, max.impact_1 = tmp.impact_1, max.impact_2 = tmp.impact_2, max.impact_3 = tmp.impact_3, max.impact_4 = tmp.impact_4, max.impact_5 = tmp.impact_5, max.impact_6 = tmp.impact_6)
# 5th and 95th quantile
# Lower ninety fifth
lower.fifth.food <-
mean.se.ci[fifth.obs,] %>% # Quick er way of getting 5th% impact after sorting data frame
dplyr::select(product_name, id, lower_fifth.impact_1 = tmp.impact_1, lower_fifth.impact_2 = tmp.impact_2, lower_fifth.impact_3 = tmp.impact_3, lower_fifth.impact_4 = tmp.impact_4,
lower_fifth.impact_5 = tmp.impact_5, lower_fifth.impact_6 = tmp.impact_6)
upper.ninety.fifth.food <-
mean.se.ci[ninetyfifth.obs,] %>% # quick way of getting 95th % impact after sorting data frame
dplyr::select(product_name, id, upper_ninetyfifth.impact_1 = tmp.impact_1, upper_ninetyfifth.impact_2 = tmp.impact_2, upper_ninetyfifth.impact_3 = tmp.impact_3, upper_ninetyfifth.impact_4 = tmp.impact_4,
upper_ninetyfifth.impact_5 = tmp.impact_5, upper_ninetyfifth.impact_6 = tmp.impact_6)
median.food <-
mean.se.ci[fifty.obs,] %>% # quick way of getting 95th % impact after sorting data frame
dplyr::select(product_name, id, median.impact_1 = tmp.impact_1, median.impact_2 = tmp.impact_2, median.impact_3 = tmp.impact_3, median.impact_4 = tmp.impact_4,
median.impact_5 = tmp.impact_5, median.impact_6 = tmp.impact_6)
# And getting mean across all monte carlo runs
mean.se.ci <-
mean.se.ci %>%
group_by(product_name, id) %>%
dplyr::summarise(mean_1 = mean(tmp.impact_1, na.rm = TRUE),
mean_2 = mean(tmp.impact_2, na.rm = TRUE),
mean_3 = mean(tmp.impact_3, na.rm = TRUE),
mean_4 = mean(tmp.impact_4, na.rm = TRUE),
mean_5 = mean(tmp.impact_5, na.rm = TRUE),
mean_6 = mean(tmp.impact_6, na.rm = TRUE),
se_1 = sd(tmp.impact_1, na.rm = TRUE),
se_2 = sd(tmp.impact_2, na.rm = TRUE),
se_3 = sd(tmp.impact_3, na.rm = TRUE),
se_4 = sd(tmp.impact_4, na.rm = TRUE),
se_5 = sd(tmp.impact_5, na.rm = TRUE),
se_6 = sd(tmp.impact_6, na.rm = TRUE))
# getting se
mean.se.ci[,grepl('se_', names(mean.se.ci))] <-
mean.se.ci[,grepl('se_', names(mean.se.ci))] / sqrt(n_rep)
# adding confidence intervals
# lower ci
mean.se.ci[,paste0('lower_ci_',1:6)] <-
mean.se.ci[,grepl(paste0("mean_"),names(mean.se.ci))] -
qt(1 - (0.05 / 2), n_rep - 1) * mean.se.ci[,grepl(paste0("se_"), names(mean.se.ci))]
# upper ci
mean.se.ci[,paste0('upper_ci_',1:6)] <-
mean.se.ci[,grepl(paste0("mean_"),names(mean.se.ci))] +
qt(1 - (0.05 / 2), n_rep - 1) * mean.se.ci[,grepl(paste0("se_"), names(mean.se.ci))]
# Merging in min and max impact
merge.in <-
left_join(min.food,max.food) %>%
left_join(.,lower.fifth.food) %>%
left_join(.,upper.ninety.fifth.food) %>%
left_join(., median.food)
mean.se.ci <-
left_join(mean.se.ci,merge.in)
# And adding to data frame
out.df[out.df$product_name %in% i,] <- mean.se.ci
}
return.df <- out.df
# and updating names again
for(n in 1:length(indicators)) {
names(return.df)[grepl(paste0("_",n), names(return.df))] <-
gsub(paste0("_",n),paste0("_",indicators[n]),names(return.df)[grepl(paste0("_",n), names(return.df))])
}
return(return.df)
}
#
conversion.function <-
function(indicators,
percentiles) {
# ratios for conversion
ratios <- read.csv("/Volumes/Citadel/Oxford/Research Projects/Food Labeling - Christina/Dat/LCA_Ratios_Import.csv",
stringsAsFactors = FALSE) %>%
mutate(Data.S2.Name = ifelse(Data.S2.Name %in% 'Dark Chocolate (70% cocoa)','Dark Chocolate',Data.S2.Name)) %>%
mutate(ch4_in_kgco2eq = GHG.Emissions..kg.CO2eq..IPCC2013.incl.CC.feedbacks.,
n2o_in_kgco2eq = GHG.Emissions..kg.CO2eq..IPCC2013.incl.CC.feedbacks.,
co2_in_kgco2eq = GHG.Emissions..kg.CO2eq..IPCC2013.incl.CC.feedbacks.)
# data set to apend rows to
converted.df <- data.frame()
food.groups <-
sort(unique(ratios$Data.S2.Name))
# Looping through food groups
for(i in 1:length(food.groups)) {
# Temporary df for layout
tmp.df <-
lca.dat[lca.dat$Data.S2.Name %in% ratios$Original_Food[ratios$Data.S2.Name %in% food.groups[i]],]
if(nrow(tmp.df)>0) {
# Updating food group name
tmp.df$food.group <- food.groups[i]
tmp.df$Data.S2.Name <- food.groups[i]
# Dividing by ratios
for(j in 1:length(indicators)) {
tmp.ratio <- ratios[ratios$Data.S2.Name %in% food.groups[i],
names(ratios)[grep(indicators[j],names(ratios))]]
tmp.df[,names(tmp.df)[grep(indicators[j], names(tmp.df))]] <-
tmp.df[,names(tmp.df)[grep(indicators[j], names(tmp.df))]] / tmp.ratio
}
# Rbinding conversion data
converted.df <- rbind(converted.df, tmp.df)
}# End of if statement
} # End of loop for foods
return(converted.df)
}
# Managing lca dat
lca.dat <-
read.csv(paste0(getwd(),"/Data Inputs/jp_lca_dat.csv"),
stringsAsFactors = FALSE) %>%
mutate(Weight = as.numeric(gsub("%","",Weight)))
# Merging in disaggregated co2 estimates ----
# This is not pretty, but it works
lca.disaggregated <-
read.csv("/Volumes/Citadel/Oxford/Research Projects/EPIC Oxford Diet Impacts/Data Inputs/jp_lca_dat_disaggregated_ghgs.csv", stringsAsFactors = FALSE)
dat <- read.csv("/Volumes/Citadel/Oxford/Research Projects/EPIC Oxford Diet Impacts/Data Inputs/jp_lca_dat_disaggregated_ghgs.csv", stringsAsFactors = FALSE)
# columns that are n2o
# columns that are ch4
# columns that are co2
which.ch4 <- c(grep('CH4', names(dat), ignore.case = TRUE), grep('LUC.Burn', names(dat), ignore.case=TRUE))
which.n2o <- c(grep('n2o', names(dat), ignore.case = TRUE), grep('LUC.Burn', names(dat), ignore.case=TRUE))
which.co2 <-
c(grep('co2', names(dat), ignore.case = TRUE), grep('LUC.Burn', names(dat), ignore.case=TRUE), 7,8,9,10) %>% .[. != 6]
# Weighting
ch4.weights <- c()
for(i in which.ch4) {
counter = 1
if(i %in% which.n2o) {
counter = counter + 1
}
if(i %in% which.co2) {
counter = counter + 1
}
ch4.weights <- c(ch4.weights,counter)
}
n2o.weights <- c()
for(i in which.n2o) {
counter = 1
if(i %in% which.ch4) {
counter = counter + 1
}
if(i %in% which.co2) {
counter = counter + 1
}
n2o.weights <- c(n2o.weights,counter)
}
co2.weights <- c()
for(i in which.co2) {
counter = 1
if(i %in% which.ch4) {
counter = counter + 1
}
if(i %in% which.n2o) {
counter = counter + 1
}
co2.weights <- c(co2.weights,counter)
}
# Getting rid of non numeric values
for(i in unique(c(which.ch4,which.n2o,which.co2, grep('Acid.Pot.*so2|Eutr.Pot.*po4|Freshwtr.*Withdr|Wtr.Sc.*L', names(dat), ignore.case=TRUE)))) {
dat[,i] <- as.numeric(dat[,i])
}
# And summing by gasses, assuming an equal proportion of e.g. LUC emissions goes to ch4, n2o and co2
dat$ch4_in_kgco2eq <- base::rowSums(dat[,which.ch4] * 1/ch4.weights, dims = 1, na.rm = TRUE)
dat$n2o_in_kgco2eq <- base::rowSums(dat[,which.n2o] * 1/n2o.weights, dims = 1, na.rm = TRUE)
dat$co2_in_kgco2eq <- base::rowSums(dat[,which.co2] * 1/co2.weights, dims = 1, na.rm = TRUE)
# Merging in
lca.dat <-
left_join(lca.dat,
dat %>%
dplyr::select(Product_details = Product, Country, GHG.Emissions..kg.CO2eq..IPCC2013.incl.CC.feedbacks. = GHG.Emis...kg.CO2.eq.,
# Acidification..g.SO2eq. = Acid.Pot..kg.SO2.eq.,
Eutrophication..g.PO43.eq. = Eutr.Pot..kg.PO43..eq.,
Water.Use..L. = Freshwtr..Withdr...L.,
Scarcity.Weighted.Water.Use..L.eq. = Wtr.Sc...L.eq.,
ch4_in_kgco2eq, n2o_in_kgco2eq, co2_in_kgco2eq) %>%
# mutate(col_check = 'check') %>%
mutate(GHG.Emissions..kg.CO2eq..IPCC2013.incl.CC.feedbacks. = round(GHG.Emissions..kg.CO2eq..IPCC2013.incl.CC.feedbacks., digits = 2)) %>%
mutate(Eutrophication..g.PO43.eq. = round(Eutrophication..g.PO43.eq. * 1000, digits = 2)) %>%
mutate(Water.Use..L. = round(Water.Use..L., digits = 2)) %>%
mutate(Scarcity.Weighted.Water.Use..L.eq. = round(Scarcity.Weighted.Water.Use..L.eq., digits = 2)))
# Adding conversion estimates
lca.dat <-
rbind(lca.dat,
conversion.function(indicators = c('^Land.Use','GHG','Eutrophication','Scarcity','n2o_in_kgco2eq','ch4_in_kgco2eq','co2_in_kgco2eq')) %>% .[,names(lca.dat)] %>% mutate(Data.S2.Name = ifelse(Data.S2.Name %in% 'Dark Chocolate', 'Dark Chocolate (70% cocoa)', Data.S2.Name))) %>%
.[,c('Data.S2.Name','Weight','Land.Use..m2.year.','GHG.Emissions..kg.CO2eq..IPCC2013.incl.CC.feedbacks.',
'Eutrophication..g.PO43.eq.','Scarcity.Weighted.Water.Use..L.eq.', 'n2o_in_kgco2eq','ch4_in_kgco2eq','co2_in_kgco2eq')]
# Adding butter, misc oils, and pig meat
# These weightings recommended by Joseph Poore, folliwng methods in Poore and Nemecek 2018 Science
lca.dat <-
rbind(lca.dat,
lca.dat %>% filter(Data.S2.Name %in% 'Milk') %>% mutate(Data.S2.Name = 'Butter, Cream & Ghee'),
lca.dat %>% filter(Data.S2.Name %in% 'Rapeseed Oil') %>% mutate(Data.S2.Name = 'Oils Misc.'),
lca.dat %>% filter(Data.S2.Name %in% 'Pig Meat') %>% mutate(Data.S2.Name = 'Animal Fats')) %>%
rbind(.,
read.csv(paste0(getwd(),"/Data Inputs/lcadat 17october2019.csv"),
stringsAsFactors = FALSE) %>%
filter(Data.S2.Name %in% 'Tea') %>% mutate(Weight = 100) %>%
mutate(n2o_in_kgco2eq = GHG.Emissions..kg.CO2eq..IPCC2013.incl.CC.feedbacks. / 3,
ch4_in_kgco2eq = n2o_in_kgco2eq,
co2_in_kgco2eq = n2o_in_kgco2eq) %>%.[,names(lca.dat)])
# and updating names to merge with rest of script
names(lca.dat)[names(lca.dat) %in% 'Data.S2.Name'] <- 'Food_Category'
names(lca.dat)[names(lca.dat) %in% 'Land.Use..m2.year.'] <- 'Land'
names(lca.dat)[names(lca.dat) %in% 'GHG.Emissions..kg.CO2eq..IPCC2013.incl.CC.feedbacks.'] <- 'GHG'
names(lca.dat)[names(lca.dat) %in% 'Eutrophication..g.PO43.eq.'] <- 'Eut'
names(lca.dat)[names(lca.dat) %in% 'Scarcity.Weighted.Water.Use..L.eq.'] <- 'WatScar'
names(lca.dat)[names(lca.dat) %in% 'ch4_in_kgco2eq'] <- 'GHGs_CH4'
names(lca.dat)[names(lca.dat) %in% 'n2o_in_kgco2eq'] <- 'GHGs_N2O'
# and limiting lca dat to only necessary columns
lca.dat <-
lca.dat[,c('Food_Category','Weight','Land','GHG','Eut','WatScar','GHGs_CH4','GHGs_N2O')] %>%
mutate(Food_Category = ifelse(Food_Category %in% c('Fish (farmed)','Fish (wild caught)','Crustaceans (farmed)','Crustaceans (wild caught)'),
gsub(" \\(farmed\\)| \\(wild caught\\)","",Food_Category),
Food_Category)) %>%
rbind(., data.frame(Food_Category = 'Salt',Weight = 1, Land = 0, GHG = 0, Eut = 0, WatScar = 0, GHGs_CH4 = 0, GHGs_N2O = 0)) %>% # Adding data for salt
rbind(., data.frame(Food_Category = 'Water',Weight = 1, Land = 0, GHG = 0, Eut = 0, WatScar = 0, GHGs_CH4 = 0, GHGs_N2O = 0)) # Adding data for water
# getting data frame
food.df <-
data.frame(product_name = sort(unique(lca.dat$Food_Category)),
id = sort(unique(lca.dat$Food_Category)),
amount = 100,
Food_Category = sort(unique(lca.dat$Food_Category)))
source("/Volumes/Citadel/Oxford/Research Projects/EPIC Oxford Diet Impacts/Scripts/0.0 Functions Estimating Env and Nutritional Impacts 27April2021.R")
# And getting estimates
split.product.list <- food.df$product_name
# And getting estimates
split.product.list <- food.df$product_name[1]
out.df <- do.call(rbind, lapply(split.product.list, monte.carlo.lca))
View(out.df)
View(plot.dat.aisle)
###
# Fig 3
# Impacts by Aisle for Tesco
# libraries
library(dplyr)
library(plyr)
library(ggplot2)
library(readr)
library(cowplot)
# Working directory
setwd("/Volumes/Citadel/Clark_et_al_2022_PNAS_SM")
# data
plot.dat <- read_csv(paste0(getwd(),"/Managed_Data/Impacts by Product 18Feb2022 Log2.csv"))
# Aggregating
plot.dat.aisle <-
plot.dat %>%
unique(.) %>%
filter(!grepl('Halloween',Aisle, ignore.case = TRUE)) %>%
filter(!grepl('Christmas',Aisle, ignore.case = TRUE)) %>%
filter(!grepl('\\bAll\\b',Aisle,ignore.case = TRUE)) %>%
filter(!grepl('\\bAlcohol|Spirit|Beer|Cider|Cidre\\b',Aisle,ignore.case = TRUE)) %>%
filter(!grepl('\\bAlcohol|Spirit|Beer|Cider|Cidre\\b',Department,ignore.case = TRUE)) %>%
filter(!grepl('\\bAlcohol|Spirit|Beer|Cider|Cidre\\b',Shelf,ignore.case = TRUE)) %>%
mutate(Shelf = ifelse(Retailer %in% 'Cook','Cook',Shelf)) %>%
filter(!(Shelf %in% 'All')) %>%
filter(!(Shelf %in% 'NULL')) %>%
filter(Shelf != 'Water') %>%
dplyr::select(product_name, id, Retailer, Department, Aisle, Tot_env_100g_scaled, Tot_env_100g_scaled_lower_ci, Tot_env_100g_scaled_upper_ci, NutriScore_Scaled,
mean_GHG, mean_Eut, mean_Land, mean_WatScar) %>%
.[complete.cases(.),] %>%
unique(.) %>%
group_by(Retailer, Department, Aisle) %>%
dplyr::summarise(Tot_env_100g_scaled_se = sd(Tot_env_100g_scaled_lower_ci)/sqrt(n()),
Tot_env_100g_scaled = mean(Tot_env_100g_scaled),
mean_GHG = mean(mean_GHG),
mean_WatScar = mean(mean_WatScar),
mean_Eut = mean(mean_Eut),
mean_Land = mean(mean_Land),
se_nutriscore = sd(NutriScore_Scaled) / sqrt(n()),
mean_nutriscore = mean(NutriScore_Scaled)) %>%
as.data.frame(.) %>%
arrange(Tot_env_100g_scaled) %>%
filter(Aisle != 'Water') %>%
mutate(Retailer = ifelse(Retailer %in% 'Tesco_Ireland','Tesco Ireland',Retailer)) %>%
mutate(colour = ifelse(Retailer %in% 'Cook','#ce82ab',
ifelse(Retailer %in% 'Morissons','#e7a43f',
ifelse(Retailer %in% 'Ocado','#69b6e3',
ifelse(Retailer %in% 'Sainsbury','#32a27b',
ifelse(Retailer %in% 'Tesco','#eee45f',
ifelse(Retailer %in% 'Tesco Ireland','#2777b0','#d66839'))))))) %>%
filter(!(grepl('alcohol|beer|cider|wine|spirits|tobacco|smoking',Aisle,ignore.case=TRUE))) %>%
filter(!(Aisle %in% 'Bottled Water'))
# Ordering impacts by Aisle
dat.low <- plot.dat.aisle[1:10,]
dat.other <-
plot.dat.aisle[11:nrow(plot.dat.aisle),] %>%
mutate(panel_num = rank(Tot_env_100g_scaled, ties.method = 'first')) %>%
mutate(panel_num = ifelse(panel_num < nrow(.)/6,1,
ifelse(panel_num <= (nrow(.)/6)*2,2,
ifelse(panel_num <= (nrow(.)/6)*3,3,
ifelse(panel_num <= (nrow(.)/6)*4,4,
ifelse(panel_num <= (nrow(.)/6)*5,5,6))))))
# And plotting
plot.list.tot <- list()
out.dat <- data.frame()
for (i in unique(dat.other$panel_num)) {
tmp.dat <-
rbind(dat.low,dat.other  %>% filter(panel_num %in% i) %>% dplyr::select(-panel_num)) %>%
mutate(Retailer_Department_Aisle = paste0(Retailer,Department,Aisle)) %>%
transform(Retailer_Department_Aisle = factor(Retailer_Department_Aisle, levels = .$Retailer_Department_Aisle)) %>%
mutate(panel_num = i) %>%
mutate(position_x = 1:nrow(.))
out.dat <- rbind(out.dat, tmp.dat)
plot.list.tot[[i]] <-
ggplot(tmp.dat, aes(x = Retailer_Department_Aisle, y = Tot_env_100g_scaled, colour = Retailer)) +
theme_classic() +
# geom_point(alpha = .5, colour = tmp.dat$colour) +
geom_point(alpha = .75, colour = tmp.dat$colour) +
# geom_errorbar(aes(ymin = (Tot_env_100g_scaled-Tot_env_100g_scaled_se), ymax = (Tot_env_100g_scaled+Tot_env_100g_scaled_se)), colour = tmp.dat$colour, alpha = .5) +
geom_errorbar(aes(ymin = (Tot_env_100g_scaled-Tot_env_100g_scaled_se), ymax = (Tot_env_100g_scaled+Tot_env_100g_scaled_se)), alpha = .75, colour = tmp.dat$colour) +
# geom_point(aes(y = overlap),shape = 8, colour = 'black') +
# geom_point(aes(x = Department_Aisle_Shelf, y = scaled_tot_env), colour = 'blue') +
# geom_errorbar(aes(ymin = scaled_tot_env_lower, ymax = scaled_tot_env_upper), colour = 'blue') +
# scale_x_discrete(labels = substr(tmp.dat$Aisle,1,10)) +
scale_x_discrete(labels = substr(tmp.dat$Aisle,1,20)) +
# scale_colour_manual(values = unique(tmp.dat$colour)) +
# scale_colour_manual(values = sort(unique(dat.other$colour[dat.other$Retailer %in% tmp.dat$Retailer]))) +
# scale_y_continuous(trans = 'log10') +
theme(legend.position = NULL) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .5, size = 6)) +
labs(x = NULL,y = 'Environmental Impact Score', colour = NULL) +
geom_vline(xintercept = 10, linetype = 2) +
theme(legend.position = 'bottom')
}
ggplot(out.dat, aes(x = position_x, y = Tot_env_100g_scaled, colour = Retailer)) +
theme_classic() +
# geom_point(alpha = .5, colour = tmp.dat$colour) +
geom_point(alpha = .75, colour = tmp.dat$colour) +
# geom_errorbar(aes(ymin = (Tot_env_100g_scaled-Tot_env_100g_scaled_se), ymax = (Tot_env_100g_scaled+Tot_env_100g_scaled_se)), colour = tmp.dat$colour, alpha = .5) +
geom_errorbar(aes(ymin = (Tot_env_100g_scaled-Tot_env_100g_scaled_se), ymax = (Tot_env_100g_scaled+Tot_env_100g_scaled_se)), alpha = .75, colour = tmp.dat$colour) +
# geom_point(aes(y = overlap),shape = 8, colour = 'black') +
# geom_point(aes(x = Department_Aisle_Shelf, y = scaled_tot_env), colour = 'blue') +
# geom_errorbar(aes(ymin = scaled_tot_env_lower, ymax = scaled_tot_env_upper), colour = 'blue') +
# scale_x_discrete(labels = substr(tmp.dat$Aisle,1,10)) +
scale_x_continuous(breaks = out.dat$position_x,labels = substr(out.dat$Aisle,1,20)) +
# scale_colour_manual(values = unique(tmp.dat$colour)) +
# scale_y_continuous(trans = 'log10') +
theme(legend.position = NULL) +
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0, size = 6)) +
labs(x = NULL,y = 'Environmental Impact Score', colour = NULL) +
geom_vline(xintercept = 10, linetype = 2) +
theme(legend.position = 'bottom') +
facet_wrap(.~panel_num, nrow = 5, scales = 'free_y')
out.plot.tot <-
plot_grid(plot.list.tot[[1]],plot.list.tot[[2]],plot.list.tot[[3]],plot.list.tot[[4]], plot.list.tot[[5]], plot.list.tot[[6]], nrow = length(plot.list.tot), align = 'hv')
dev.off()
out.plot.tot
Sys.sleep(15)
ggsave(paste0(getwd(),'/Figure S6 Impacts by Aisle.pdf'),
width = 7.2, height = 15, units = 'in')
write.csv(plot.dat.aisle %>% dplyr::select(Retailer, Aisle, Tot_env_100g_scaled, Tot_env_100g_scaled_se, colour),
paste0(getwd(),'/Figure Data/Figure Data S6.csv'),
row.names = FALSE)
tmp <- read_csv('/Volumes/Citadel/Clark_et_al_2022_PNAS_SM/foodDB_dat/products anonymised.csv')
View(tmp)
