library(ggplot2)
library(tidyr)
library(plyr)
library(reshape2)
library(grid)
library(gridExtra)
library(ggpubr)
library(tidyverse)

AllData <- read.csv("~/AllData.csv")
AllData$WOA <- ifelse(AllData$Advice==AllData$Init, NA, (AllData$Fin - AllData$Init)/(AllData$Advice - AllData$Init))
AllData$WOA_Wins <- ifelse(AllData$WOA<=-1,-1,ifelse(AllData$WOA>=1,1,AllData$WOA))
AllData$IOA_MS_Wins <- ifelse(AllData$IOA_MS<=-1,-1,ifelse(AllData$IOA_MS>=1,1,AllData$IOA_MS))
AllData$IOA_CC_Wins <- ifelse(AllData$IOA_CC<=-1,-1,ifelse(AllData$IOA_CC>=1,1,AllData$IOA_CC))
AllData$IOA_TOT_Wins <- ifelse(AllData$IOA_TOT<=-1,-1,ifelse(AllData$IOA_TOT>=1,1,AllData$IOA_TOT))
AllData$RankLab2 <- ifelse(AllData$RankLab=="1. Advisor Rank 1-40","(a) High-Quality Advisors\n(Ranked 1-40 on Previous Task)",ifelse(AllData$RankLab=="3. Advisor Rank 81-120","(b) Middling-Quality Advisors\n(Ranked 81-120 on Previous Task)",ifelse(AllData$RankLab=="5. Advisor Rank 161-200","(c) Low-Quality Advisors\n(Ranked 161-200 on Previous Task)",AllData$RankLab)))
AllData$Study2 <- AllData$Study
AllData$Study <- ifelse(AllData$Study2=="AgeProb","\n1a. Ages,\nProbability Within C\n",ifelse(AllData$Study2=="AgeMiss","\n1b. Ages,\nEstimated Miss\n",ifelse(AllData$Study2=="AgeQuant","\n1c. Ages,\nPercentiles\n",ifelse(AllData$Study2=="FoodProb","\n2a. Calories,\nProbability Within C\n",ifelse(AllData$Study2=="FoodMiss","\n2b. Calories,\nEstimated Miss\n",ifelse(AllData$Study2=="FoodQuant","\n2c. Calories,\nPercentiles\n",AllData$Study2))))))

# REMOVE THE TWO WORST OUTLIERS
AllData <- AllData %>% filter(OC_Change <5000 & OC_Change > -5000 & ExpImprove <5000 & ExpImprove >-5000) 

#################################################
# IOA and WOA aggregated graphs, Figures 3 and 4
#################################################
{ # plot IOA and WOA together for Ages
IOAWOAages <- ggplot(data=subset(AllData,StimType=="Age" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_TOT_Wins, linetype = "\nIOA\n(Influence\nOf Advice)\n", color = "\nIOA\n(Influence\nOf Advice)\n"), se = F, method = "loess") + geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n(Weight\nOn Advice)\n", color = "\nWOA\n(Weight\nOn Advice)\n"), se = F, method = "loess") + geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="Experiments\n1a, 1b, 1c. \n(Ages)",values=c("\nIOA\n(Influence\nOf Advice)\n"="dashed","\nWOA\n(Weight\nOn Advice)\n"="dotted","\nWOA\n(Weight\nOn Advice)\n"="solid")) + scale_color_manual(name="Experiments\n1a, 1b, 1c. \n(Ages)",values=c("\nIOA\n(Influence\nOf Advice)\n"="gray50","\nWOA\n(Weight\nOn Advice)\n"="gray30","\nWOA\n(Weight\nOn Advice)\n"="black")) + coord_cartesian(xlim=c(0,25),ylim=c(-1,1)) + labs(y="Advice Taking", x="Distance Between Advice and Initial Estimate (in Years)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 

IOAWOAfood <- ggplot(data=subset(AllData,StimType=="Food" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_TOT_Wins, linetype = "\nIOA\n(Influence\nOf Advice)\n", color = "\nIOA\n(Influence\nOf Advice)\n"), se = F, method = "loess") + geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n(Weight\nOn Advice)\n", color = "\nWOA\n(Weight\nOn Advice)\n"), se = F, method = "loess") + geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="Experiments\n2a, 2b, 2c. \n(Calories)",values=c("\nIOA\n(Influence\nOf Advice)\n"="dashed","\nWOA\n(Weight\nOn Advice)\n"="dotted","\nWOA\n(Weight\nOn Advice)\n"="solid")) + scale_color_manual(name="Experiments\n2a, 2b, 2c. \n(Calories)",values=c("\nIOA\n(Influence\nOf Advice)\n"="gray50","\nWOA\n(Weight\nOn Advice)\n"="gray30","\nWOA\n(Weight\nOn Advice)\n"="black")) + coord_cartesian(xlim=c(0,1000),ylim=c(-1,1)) + labs(y="Advice Taking", x="Distance Between Advice and Initial Estimate (in Calories)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 

grid.arrange(IOAWOAages, IOAWOAfood, nrow=2) # Figure 3, export as 9.5 x 4.5


MSCCages <- ggplot(data=subset(AllData,StimType=="Age" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_MS_Wins, linetype = "\nOpinion\nShift\n", color = "\nOpinion\nShift\n"), se = F, method = "loess") + geom_smooth(aes(y = IOA_CC_Wins, linetype = "\nConfidence\nChange\n", color = "\nConfidence\nChange\n"), se = F, method = "loess") + geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="Experiments\n1a, 1b, 1c. \n(Ages)",values=c("\nOpinion\nShift\n"="dashed","\nConfidence\nChange\n"="dotted","\nWOA\n"="solid")) + scale_color_manual(name="Experiments\n1a, 1b, 1c. \n(Ages)", values=c("\nOpinion\nShift\n"="gray50","\nConfidence\nChange\n"="gray30","\nWOA\n"="black")) + coord_cartesian(xlim=c(0,25),ylim=c(-1,1)) + labs(y="Contribution of Subcomponent\nto Total Influence of Advice", x="Distance Between Advice and Initial Estimate (in Years)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 

MSCCfood <- ggplot(data=subset(AllData,StimType=="Food" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_MS_Wins, linetype = "\nOpinion\nShift\n", color = "\nOpinion\nShift\n"), se = F, method = "loess") + geom_smooth(aes(y = IOA_CC_Wins, linetype = "\nConfidence\nChange\n", color = "\nConfidence\nChange\n"), se = F, method = "loess") + geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="Experiments\n2a, 2b, 2c. \n(Calories)",values=c("\nOpinion\nShift\n"="dashed","\nConfidence\nChange\n"="dotted","\nWOA\n"="solid")) + scale_color_manual(name="Experiments\n2a, 2b, 2c. \n(Calories)",values=c("\nOpinion\nShift\n"="gray50","\nConfidence\nChange\n"="gray30","\nWOA\n"="black")) + coord_cartesian(xlim=c(0,1000),ylim=c(-1,1)) + labs(y="Contribution of Subcomponent\nto Total Influence of Advice", x="Distance Between Advice and Initial Estimate (in Calories)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 

grid.arrange(MSCCages, MSCCfood, nrow=2) # Figure 4, export as 10 x 5

}

#######################################################
# IOA and WOA broken down by study, Figure 3 Supplement
#######################################################
{ # plot IOA and WOA together for Ages
  IOAWOAagesProb <- ggplot(data=subset(AllData,StimType=="Age" & ElicitType=="Prob" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_TOT_Wins, linetype = "\nIOA\n(Influence\nOf Advice)\n", color = "\nIOA\n(Influence\nOf Advice)\n"), se = F, method = "loess") + geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n(Weight\nOn Advice)\n", color = "\nWOA\n(Weight\nOn Advice)\n"), se = F, method = "loess") + geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="1a. Ages,\nProbability\nWithin C",values=c("\nIOA\n(Influence\nOf Advice)\n"="dashed","\nWOA\n(Weight\nOn Advice)\n"="dotted","\nWOA\n(Weight\nOn Advice)\n"="solid")) + scale_color_manual(name="1a. Ages,\nProbability\nWithin C",values=c("\nIOA\n(Influence\nOf Advice)\n"="gray50","\nWOA\n(Weight\nOn Advice)\n"="gray30","\nWOA\n(Weight\nOn Advice)\n"="black")) + coord_cartesian(xlim=c(0,25),ylim=c(-1,1)) + labs(y="Advice Taking", x="Distance Between Advice and Initial Estimate (in Years)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 

  IOAWOAagesMiss <- ggplot(data=subset(AllData,StimType=="Age" & ElicitType=="Miss" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_TOT_Wins, linetype = "\nIOA\n(Influence\nOf Advice)\n", color = "\nIOA\n(Influence\nOf Advice)\n"), se = F, method = "loess") + geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n(Weight\nOn Advice)\n", color = "\nWOA\n(Weight\nOn Advice)\n"), se = F, method = "loess") + geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="1b. Ages,\nEstimated\nMiss",values=c("\nIOA\n(Influence\nOf Advice)\n"="dashed","\nWOA\n(Weight\nOn Advice)\n"="dotted","\nWOA\n(Weight\nOn Advice)\n"="solid")) + scale_color_manual(name="1b. Ages,\nEstimated\nMiss",values=c("\nIOA\n(Influence\nOf Advice)\n"="gray50","\nWOA\n(Weight\nOn Advice)\n"="gray30","\nWOA\n(Weight\nOn Advice)\n"="black")) + coord_cartesian(xlim=c(0,25),ylim=c(-1,1)) + labs(y="Advice Taking", x="Distance Between Advice and Initial Estimate (in Years)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 
  
  IOAWOAagesQuant <- ggplot(data=subset(AllData,StimType=="Age" & ElicitType=="Quant" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_TOT_Wins, linetype = "\nIOA\n(Influence\nOf Advice)\n", color = "\nIOA\n(Influence\nOf Advice)\n"), se = F, method = "loess") + geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n(Weight\nOn Advice)\n", color = "\nWOA\n(Weight\nOn Advice)\n"), se = F, method = "loess") + geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="1c. Ages,\nPercentiles",values=c("\nIOA\n(Influence\nOf Advice)\n"="dashed","\nWOA\n(Weight\nOn Advice)\n"="dotted","\nWOA\n(Weight\nOn Advice)\n"="solid")) + scale_color_manual(name="1c. Ages,\nPercentiles",values=c("\nIOA\n(Influence\nOf Advice)\n"="gray50","\nWOA\n(Weight\nOn Advice)\n"="gray30","\nWOA\n(Weight\nOn Advice)\n"="black")) + coord_cartesian(xlim=c(0,25),ylim=c(-1,1)) + labs(y="Advice Taking", x="Distance Between Advice and Initial Estimate (in Years)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 
  
  IOAWOAfoodProb <- ggplot(data=subset(AllData,StimType=="Food" & ElicitType=="Prob" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_TOT_Wins, linetype = "\nIOA\n(Influence\nOf Advice)\n", color = "\nIOA\n(Influence\nOf Advice)\n"), se = F, method = "loess") + geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n(Weight\nOn Advice)\n", color = "\nWOA\n(Weight\nOn Advice)\n"), se = F, method = "loess") + geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="2a. Calories,\nProbability\nWithin C",values=c("\nIOA\n(Influence\nOf Advice)\n"="dashed","\nWOA\n(Weight\nOn Advice)\n"="dotted","\nWOA\n(Weight\nOn Advice)\n"="solid")) + scale_color_manual(name="2a. Calories,\nProbability\nWithin C",values=c("\nIOA\n(Influence\nOf Advice)\n"="gray50","\nWOA\n(Weight\nOn Advice)\n"="gray30","\nWOA\n(Weight\nOn Advice)\n"="black")) + coord_cartesian(xlim=c(0,1000),ylim=c(-1,1)) + labs(y="Advice Taking", x="Distance Between Advice and Initial Estimate (in Calories)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 
  
  IOAWOAfoodMiss <- ggplot(data=subset(AllData,StimType=="Food" & ElicitType=="Miss" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_TOT_Wins, linetype = "\nIOA\n(Influence\nOf Advice)\n", color = "\nIOA\n(Influence\nOf Advice)\n"), se = F, method = "loess") + geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n(Weight\nOn Advice)\n", color = "\nWOA\n(Weight\nOn Advice)\n"), se = F, method = "loess") + geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="2b. Calories,\nEstimated\nMiss",values=c("\nIOA\n(Influence\nOf Advice)\n"="dashed","\nWOA\n(Weight\nOn Advice)\n"="dotted","\nWOA\n(Weight\nOn Advice)\n"="solid")) + scale_color_manual(name="2b. Calories,\nEstimated\nMiss",values=c("\nIOA\n(Influence\nOf Advice)\n"="gray50","\nWOA\n(Weight\nOn Advice)\n"="gray30","\nWOA\n(Weight\nOn Advice)\n"="black")) + coord_cartesian(xlim=c(0,1000),ylim=c(-1,1)) + labs(y="Advice Taking", x="Distance Between Advice and Initial Estimate (in Calories)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 
  
  IOAWOAfoodQuant <- ggplot(data=subset(AllData,StimType=="Food" & ElicitType=="Quant" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_TOT_Wins, linetype = "\nIOA\n(Influence\nOf Advice)\n", color = "\nIOA\n(Influence\nOf Advice)\n"), se = F, method = "loess") + geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n(Weight\nOn Advice)\n", color = "\nWOA\n(Weight\nOn Advice)\n"), se = F, method = "loess") + geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="2c. Calories,\nPercentiles",values=c("\nIOA\n(Influence\nOf Advice)\n"="dashed","\nWOA\n(Weight\nOn Advice)\n"="dotted","\nWOA\n(Weight\nOn Advice)\n"="solid")) + scale_color_manual(name="2c. Calories,\nPercentiles",values=c("\nIOA\n(Influence\nOf Advice)\n"="gray50","\nWOA\n(Weight\nOn Advice)\n"="gray30","\nWOA\n(Weight\nOn Advice)\n"="black")) + coord_cartesian(xlim=c(0,1000),ylim=c(-1,1)) + labs(y="Advice Taking", x="Distance Between Advice and Initial Estimate (in Calories)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 
  
  
grid.arrange(IOAWOAagesProb, IOAWOAagesMiss, IOAWOAagesQuant, IOAWOAfoodProb, IOAWOAfoodMiss, IOAWOAfoodQuant, nrow=6) # export as 10 x 16

  
}

###############################################################
# IOA Opinion Shift and Confidence Changes, Figure 4 Supplement
###############################################################
{
{
MSCCAgeProb <- ggplot(data=subset(AllData,StimType=="Age" & ElicitType=="Prob" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_MS_Wins, linetype = "\nOpinion\nShift\n", color = "\nOpinion\nShift\n"), se = F, method = "loess") + geom_smooth(aes(y = IOA_CC_Wins, linetype = "\nConfidence\nChange\n", color = "\nConfidence\nChange\n"), se = F, method = "loess") + # geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n", color = "\nWOA\n"), se = F, method = "loess") + 
geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="1a. Ages,\nProbability\nWithin C",values=c("\nOpinion\nShift\n"="dashed","\nConfidence\nChange\n"="dotted","\nWOA\n"="solid")) + scale_color_manual(name="1a. Ages,\nProbability\nWithin C",values=c("\nOpinion\nShift\n"="gray50","\nConfidence\nChange\n"="gray30","\nWOA\n"="black")) + coord_cartesian(xlim=c(0,25),ylim=c(-1,1)) + labs(y="Contribution of Subcomponent\nto Total Influence of Advice", x="Distance Between Advice and Initial Estimate (in Years)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 
MSCCAgeMiss <- ggplot(data=subset(AllData,StimType=="Age" & ElicitType=="Miss" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_MS_Wins, linetype = "\nOpinion\nShift\n", color = "\nOpinion\nShift\n"), se = F, method = "loess") + geom_smooth(aes(y = IOA_CC_Wins, linetype = "\nConfidence\nChange\n", color = "\nConfidence\nChange\n"), se = F, method = "loess") + #geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n", color = "\nWOA\n"), se = F, method = "loess") + 
geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="1b. Ages,\nEstimated\nMiss",values=c("\nOpinion\nShift\n"="dashed","\nConfidence\nChange\n"="dotted","\nWOA\n"="solid")) + scale_color_manual(name="1b. Ages,\nEstimated\nMiss",values=c("\nOpinion\nShift\n"="gray50","\nConfidence\nChange\n"="gray30","\nWOA\n"="black")) + coord_cartesian(xlim=c(0,25),ylim=c(-1,1)) + labs(y="Contribution of Subcomponent\nto Total Influence of Advice", x="Distance Between Advice and Initial Estimate (in Years)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 
MSCCAgeQuant <- ggplot(data=subset(AllData,StimType=="Age" & ElicitType=="Quant" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_MS_Wins, linetype = "\nOpinion\nShift\n", color = "\nOpinion\nShift\n"), se = F, method = "loess") + geom_smooth(aes(y = IOA_CC_Wins, linetype = "\nConfidence\nChange\n", color = "\nConfidence\nChange\n"), se = F, method = "loess") + #geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n", color = "\nWOA\n"), se = F, method = "loess") + 
geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="1c. Ages,\nPercentiles",values=c("\nOpinion\nShift\n"="dashed","\nConfidence\nChange\n"="dotted","\nWOA\n"="solid")) + scale_color_manual(name="1c. Ages,\nPercentiles",values=c("\nOpinion\nShift\n"="gray50","\nConfidence\nChange\n"="gray30","\nWOA\n"="black")) + coord_cartesian(xlim=c(0,25),ylim=c(-1,1)) + labs(y="Contribution of Subcomponent\nto Total Influence of Advice", x="Distance Between Advice and Initial Estimate (in Years)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2)
}
grid.arrange(MSCCAgeProb, MSCCAgeMiss, MSCCAgeQuant, nrow=3) # export as 10 x 8

{
MSCCFoodProb <- ggplot(data=subset(AllData,StimType=="Food" & ElicitType=="Prob" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_MS_Wins, linetype = "\nOpinion\nShift\n", color = "\nOpinion\nShift\n"), se = F, method = "loess") + geom_smooth(aes(y = IOA_CC_Wins, linetype = "\nConfidence\nChange\n", color = "\nConfidence\nChange\n"), se = F, method = "loess") + #geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n", color = "\nWOA\n"), se = F, method = "loess") + 
geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="2a. Calories,\nProbability\nWithin C",values=c("\nOpinion\nShift\n"="dashed","\nConfidence\nChange\n"="dotted","\nWOA\n"="solid")) + scale_color_manual(name="2a. Calories,\nProbability\nWithin C",values=c("\nOpinion\nShift\n"="gray50","\nConfidence\nChange\n"="gray30","\nWOA\n"="black")) + coord_cartesian(xlim=c(0,1000),ylim=c(-1,1)) + labs(y="Contribution of Subcomponent\nto Total Influence of Advice", x="Distance Between Advice and Initial Estimate (in Calories)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 
MSCCFoodMiss <- ggplot(data=subset(AllData,StimType=="Food" & ElicitType=="Miss" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_MS_Wins, linetype = "\nOpinion\nShift\n", color = "\nOpinion\nShift\n"), se = F, method = "loess") + geom_smooth(aes(y = IOA_CC_Wins, linetype = "\nConfidence\nChange\n", color = "\nConfidence\nChange\n"), se = F, method = "loess") + #geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n", color = "\nWOA\n"), se = F, method = "loess") + 
geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="2b. Calories,\nEstimated\nMiss",values=c("\nOpinion\nShift\n"="dashed","\nConfidence\nChange\n"="dotted","\nWOA\n"="solid")) + scale_color_manual(name="2b. Calories,\nEstimated\nMiss",values=c("\nOpinion\nShift\n"="gray50","\nConfidence\nChange\n"="gray30","\nWOA\n"="black")) + coord_cartesian(xlim=c(0,1000),ylim=c(-1,1)) + labs(y="Contribution of Subcomponent\nto Total Influence of Advice", x="Distance Between Advice and Initial Estimate (in Calories)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) 
MSCCFoodQuant <- ggplot(data=subset(AllData,StimType=="Food" & ElicitType=="Quant" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = IOA_MS_Wins, linetype = "\nOpinion\nShift\n", color = "\nOpinion\nShift\n"), se = F, method = "loess") + geom_smooth(aes(y = IOA_CC_Wins, linetype = "\nConfidence\nChange\n", color = "\nConfidence\nChange\n"), se = F, method = "loess") + #geom_smooth(aes(y = WOA_Wins, linetype = "\nWOA\n", color = "\nWOA\n"), se = F, method = "loess") + 
geom_hline(yintercept = 0, size = .5) + theme_bw(base_size=12) + scale_linetype_manual(name="2c. Calories,\nPercentiles",values=c("\nOpinion\nShift\n"="dashed","\nConfidence\nChange\n"="dotted","\nWOA\n"="solid")) + scale_color_manual(name="2c. Calories,\nPercentiles",values=c("\nOpinion\nShift\n"="gray50","\nConfidence\nChange\n"="gray30","\nWOA\n"="black")) + coord_cartesian(xlim=c(0,1000),ylim=c(-1,1)) + labs(y="Contribution of Subcomponent\nto Total Influence of Advice", x="Distance Between Advice and Initial Estimate (in Calories)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2)
}
grid.arrange(MSCCFoodProb, MSCCFoodMiss, MSCCFoodQuant, nrow=3) # export as 10 x 8

grid.arrange(MSCCAgeProb, MSCCAgeMiss, MSCCAgeQuant, MSCCFoodProb, MSCCFoodMiss, MSCCFoodQuant, nrow=6) # Figure 4 Full Version For Supplement, export as 10 x 16
}

######################################################################
### Bar charts SPOTLIGHT splits pooled across studies, 2x2x2, Figure 5 
######################################################################
{
  ### SPOTLIGHT splits for Ages to make 2x2 bar plot
  # set the data split at DistIA<=3 and DistIA>=10
  # set the data split at Rank<=40 and Rank>160
  AllData$DistRankCrossAgeSP = ifelse(AllData$DistIA<=3 & AllData$Rank<=40,"(a) High-quality advisor,\nhigh agreement",ifelse(AllData$DistIA>=10 & AllData$Rank<=40,"(b) High-quality advisor,\nlow agreement",ifelse(AllData$DistIA<=3 & AllData$Rank>160,"(c) Low-quality advisor,\nhigh agreement",ifelse(AllData$DistIA>=10 & AllData$Rank>160,"(d) Low-quality advisor,\nlow agreement",NA))))
  # AGES STUDY
  # Take means of initial and final AD and EAD within the four groups
  AgeBarDataSP <-ddply(subset(AllData,StimType=="Age" & is.na(DistRankCrossAgeSP)==FALSE), c("DistRankCrossAgeSP"), summarise, v1AD_I = mean(AD_I), v2EAD_I = mean(EAD_I), v3AD_F = mean(AD_F),  v4EAD_F = mean(EAD_F))
  AgeBarDataSP1 <- melt(AgeBarDataSP, id.vars = c("DistRankCrossAgeSP"))
  AgeBarDataSP1$measure = ifelse(AgeBarDataSP1$variable == "v1AD_I" | AgeBarDataSP1$variable == "v3AD_F","\nAccuracy\n(Absolute Error)\n","\nConfidence\n(Expected Absolute Error)\n")
  AgeBarDataSP1$type = ifelse(AgeBarDataSP1$variable == "v1AD_I" | AgeBarDataSP1$variable == "v2EAD_I","1. Initial","2. Final")
  AgeBarsSP <- ggplot(AgeBarDataSP1) + geom_bar(aes(x=type, y=value, fill=measure), stat="identity", position="dodge", width = 0.7) + facet_wrap(~DistRankCrossAgeSP) + theme_bw() + scale_fill_grey(name="Experiments\n1a, 1b, 1c. \n(Ages)") + theme(axis.title.x = element_blank(), panel.grid.major.x = element_blank(), legend.position="left") + scale_x_discrete(labels=c("1. Initial"="Initial","2. Final"="Final")) + scale_y_continuous(limits=c(0,10),breaks=c(0,2,4,6,8,10)) + ylab("Years")
  AgeBarsSP # export as 8 x 3
  
  ### SPOTLIGHT splits for Calories to make 2x2 bar plot
  {
    # set the data split at DistIA<=150 and DistIA>=500
    # set the data split at Rank<=40 and Rank>160
    AllData$DistRankCrossFoodSP = ifelse(AllData$DistIA<=150 & AllData$Rank<=40,"(a) High-quality advisor,\nhigh agreement",ifelse(AllData$DistIA>=500 & AllData$Rank<=40,"(b) High-quality advisor,\nlow agreement",ifelse(AllData$DistIA<=150 & AllData$Rank>160,"(c) Low-quality advisor,\nhigh agreement",ifelse(AllData$DistIA>=500 & AllData$Rank>160,"(d) Low-quality advisor,\nlow agreement",NA))))
    # CALORIES STUDIES
    # Take means of initial and final AD and EAD within the four groups
    FoodBarDataSP <-ddply(subset(AllData,StimType=="Food" & is.na(DistRankCrossFoodSP)==FALSE), c("DistRankCrossFoodSP"), summarise, v1AD_I = mean(AD_I), v2EAD_I = mean(EAD_I), v3AD_F = mean(AD_F),  v4EAD_F = mean(EAD_F))
    FoodBarDataSP1 <- melt(FoodBarDataSP, id.vars = c("DistRankCrossFoodSP"))
    FoodBarDataSP1$measure = ifelse(FoodBarDataSP1$variable == "v1AD_I" | FoodBarDataSP1$variable == "v3AD_F","\nAccuracy\n(Absolute Error)\n","\nConfidence\n(Expected Absolute Error)\n")
    FoodBarDataSP1$type = ifelse(FoodBarDataSP1$variable == "v1AD_I" | FoodBarDataSP1$variable == "v2EAD_I","1. Initial","2. Final")
    FoodBarsSP <- ggplot(FoodBarDataSP1) + geom_bar(aes(x=type, y=value, fill=measure), stat="identity", position="dodge", width = 0.7) + facet_wrap(~DistRankCrossFoodSP) + theme_bw() + scale_fill_grey(name="Experiments\n2a, 2b, 2c. \n(Calories)") + theme(axis.title.x = element_blank(), panel.grid.major.x = element_blank(), legend.position="left") +scale_x_discrete(labels=c("1. Initial"="Initial","2. Final"="Final")) + scale_y_continuous(limits=c(0,560),breaks=c(0,100,200,300,400,500)) + ylab("Calories")
    FoodBarsSP # export as 8 x 3 
  }
}
wSP = grid.rect(gp=gpar(col="white")) # empty white space for plot below
grid.arrange(AgeBarsSP, wSP, FoodBarsSP,  nrow=3, heights=c(1, .2, 1)) 
grid.text((paste("(Note: Lower values\ncorrespond to\ngreater accuracy or\ngreater confidence)")), x = unit(.02, "npc"), y = unit(.43, "npc"), just = c("left", "bottom"), gp = gpar(fontface = "bold.italic", fontsize = 12, lineheight=0.9)) # Figure 5, export as 9 x 7

#######################################################################
### Bar charts broken down by experiment, 2x2x2x3, Figure 5 Supplement
#######################################################################
{
### SPOTLIGHT splits for Ages to make 2x2 bar plot
{ 
# set the data split at DistIA<=3 and DistIA>=10
# set the data split at Rank<=40 and Rank>160
  AllData$DistRankCrossAgeSP = ifelse(AllData$DistIA<=3 & AllData$Rank<=40,"(a) High-quality advisor,\nhigh agreement",ifelse(AllData$DistIA>=10 & AllData$Rank<=40,"(b) High-quality advisor,\nlow agreement",ifelse(AllData$DistIA<=3 & AllData$Rank>160,"(c) Low-quality advisor,\nhigh agreement",ifelse(AllData$DistIA>=10 & AllData$Rank>160,"(d) Low-quality advisor,\nlow agreement",NA))))
  # AGE PROBABILITY STUDY
  # Take means of initial and final AD and EAD within the four groups
  AgeProbBarDataSP <-ddply(subset(AllData,StimType=="Age" & Study2=="AgeProb" & is.na(DistRankCrossAgeSP)==FALSE), c("DistRankCrossAgeSP","Study"), summarise, v1AD_I = mean(AD_I), v2EAD_I = mean(EAD_I), v3AD_F = mean(AD_F),  v4EAD_F = mean(EAD_F))
  AgeProbBarDataSP1 <- melt(AgeProbBarDataSP, id.vars = c("DistRankCrossAgeSP","Study"))
  AgeProbBarDataSP1$measure = ifelse(AgeProbBarDataSP1$variable == "v1AD_I" | AgeProbBarDataSP1$variable == "v3AD_F","\nMean Absolute\nError\n(MAE)\n","\nMean Expected\nAbsolute Error\n(MEAE)\n")
  AgeProbBarDataSP1$type = ifelse(AgeProbBarDataSP1$variable == "v1AD_I" | AgeProbBarDataSP1$variable == "v2EAD_I","1. Initial","2. Final")
  AgeProbBarsSP <- ggplot(AgeProbBarDataSP1) + geom_bar(aes(x=type, y=value, fill=measure), stat="identity", position="dodge", width = 0.7) +facet_wrap(~DistRankCrossAgeSP) + theme_bw() + scale_fill_grey() + theme(axis.title.x = element_blank(), legend.position="right", legend.title = element_blank()) + scale_x_discrete(labels=c("1. Initial"="Initial","2. Final"="Final")) + scale_y_continuous(limits=c(0,10.5),breaks=c(0,2,4,6,8,10)) + ggtitle("1a. Ages, Probability Within C")+theme(plot.title = element_text(hjust = 0.5, size = 16))+ ylab("Years")
  # AGE MISS STUDY
  # Take means of initial and final AD and EAD within the four groups
  AgeMissBarDataSP <- ddply(subset(AllData,StimType=="Age" & Study2=="AgeMiss" & is.na(DistRankCrossAgeSP)==FALSE), c("DistRankCrossAgeSP","Study"), summarise, v1AD_I = mean(AD_I), v2EAD_I = mean(EAD_I), v3AD_F = mean(AD_F),  v4EAD_F = mean(EAD_F))
  AgeMissBarDataSP1 <- melt(AgeMissBarDataSP, id.vars = c("DistRankCrossAgeSP","Study"))
  AgeMissBarDataSP1$measure = ifelse(AgeMissBarDataSP1$variable == "v1AD_I" | AgeMissBarDataSP1$variable == "v3AD_F","\nMean Absolute\nError\n(MAE)\n","\nMean Expected\nAbsolute Error\n(MEAE)\n")
  AgeMissBarDataSP1$type = ifelse(AgeMissBarDataSP1$variable == "v1AD_I" | AgeMissBarDataSP1$variable == "v2EAD_I","1. Initial","2. Final")
  AgeMissBarsSP <- ggplot(AgeMissBarDataSP1) + geom_bar(aes(x=type, y=value, fill=measure), stat="identity", position="dodge", width = 0.7) + facet_wrap(~DistRankCrossAgeSP) + theme_bw() + scale_fill_grey() + theme(axis.title.x = element_blank(), legend.position="right", legend.title = element_blank()) + scale_x_discrete(labels=c("1. Initial"="Initial","2. Final"="Final")) + scale_y_continuous(limits=c(0,10.5),breaks=c(0,2,4,6,8,10)) + ggtitle("1b. Ages, Estimated Miss")+theme(plot.title = element_text(hjust = 0.5, size = 16))+ ylab("Years")
  # AGE QUANTILE STUDY
  # Take means of initial and final AD and EAD within the four groups
  AgeQuantBarDataSP <-ddply(subset(AllData,StimType=="Age" & Study2=="AgeQuant" & is.na(DistRankCrossAgeSP)==FALSE), c("DistRankCrossAgeSP","Study"), summarise, v1AD_I = mean(AD_I), v2EAD_I = mean(EAD_I), v3AD_F = mean(AD_F),  v4EAD_F = mean(EAD_F))
  AgeQuantBarDataSP1 <- melt(AgeQuantBarDataSP, id.vars = c("DistRankCrossAgeSP","Study"))
  AgeQuantBarDataSP1$measure = ifelse(AgeQuantBarDataSP1$variable == "v1AD_I" | AgeQuantBarDataSP1$variable == "v3AD_F","\nMean Absolute\nError\n(MAE)\n","\nMean Expected\nAbsolute Error\n(MEAE)\n")
  AgeQuantBarDataSP1$type = ifelse(AgeQuantBarDataSP1$variable == "v1AD_I" | AgeQuantBarDataSP1$variable == "v2EAD_I","1. Initial","2. Final")
  AgeQuantBarsSP <- ggplot(AgeQuantBarDataSP1) + geom_bar(aes(x=type, y=value, fill=measure), stat="identity", position="dodge", width = 0.7) + facet_wrap(~DistRankCrossAgeSP) + theme_bw() + scale_fill_grey() + theme(axis.title.x = element_blank(), legend.position="right", legend.title = element_blank()) + scale_x_discrete(labels=c("1. Initial"="Initial","2. Final"="Final")) + scale_y_continuous(limits=c(0,10.5),breaks=c(0,2,4,6,8,10)) + ggtitle("1c. Ages, Percentiles")+theme(plot.title = element_text(hjust = 0.5, size = 16))+ ylab("Years")
}
grid.arrange(AgeProbBarsSP, AgeMissBarsSP, AgeQuantBarsSP, nrow=3) # export as 6.5 x 10 
### SPOTLIGHT splits for Calories to make 2x2 bar plot
{
  # set the data split at DistIA<=150 and DistIA>=500
  # set the data split at Rank<=40 and Rank>160
  AllData$DistRankCrossFoodSP = ifelse(AllData$DistIA<=150 & AllData$Rank<=40,"(a) High-quality advisor,\nhigh agreement",ifelse(AllData$DistIA>=500 & AllData$Rank<=40,"(b) High-quality advisor,\nlow agreement",ifelse(AllData$DistIA<=150 & AllData$Rank>160,"(c) Low-quality advisor,\nhigh agreement",ifelse(AllData$DistIA>=500 & AllData$Rank>160,"(d) Low-quality advisor,\nlow agreement",NA))))
  # CALORIES PROB STUDY
  # Take means of initial and final AD and EAD within the four groups
  FoodProbBarDataSP <-ddply(subset(AllData,StimType=="Food" & Study2=="FoodProb" & is.na(DistRankCrossFoodSP)==FALSE), c("DistRankCrossFoodSP","Study"), summarise, v1AD_I = mean(AD_I), v2EAD_I = mean(EAD_I), v3AD_F = mean(AD_F),  v4EAD_F = mean(EAD_F))
  FoodProbBarDataSP1 <- melt(FoodProbBarDataSP, id.vars = c("DistRankCrossFoodSP","Study"))
  FoodProbBarDataSP1$measure = ifelse(FoodProbBarDataSP1$variable == "v1AD_I" | FoodProbBarDataSP1$variable == "v3AD_F","\nMean Absolute\nError\n(MAE)\n","\nMean Expected\nAbsolute Error\n(MEAE)\n")
  FoodProbBarDataSP1$type = ifelse(FoodProbBarDataSP1$variable == "v1AD_I" | FoodProbBarDataSP1$variable == "v2EAD_I","1. Initial","2. Final")
  FoodProbBarsSP <- ggplot(FoodProbBarDataSP1) + geom_bar(aes(x=type, y=value, fill=measure), stat="identity", position="dodge", width = 0.7)+facet_wrap(~DistRankCrossFoodSP) + theme_bw() + scale_fill_grey() + theme(axis.title.x = element_blank(), legend.position="right", legend.title = element_blank()) +scale_x_discrete(labels=c("1. Initial"="Initial","2. Final"="Final")) + ggtitle("2a. Calories, Probability Within C")+theme(plot.title = element_text(hjust = 0.5, size = 16)) + scale_y_continuous(limits=c(0,560),breaks=c(0,100,200,300,400,500)) + ylab("Calories")
  # CALORIES MISS STUDY
  # Take means of initial and final AD and EAD within the four groups
  FoodMissBarDataSP <-ddply(subset(AllData,StimType=="Food" & Study2=="FoodMiss" & is.na(DistRankCrossFoodSP)==FALSE), c("DistRankCrossFoodSP","Study"), summarise, v1AD_I = mean(AD_I), v2EAD_I = mean(EAD_I), v3AD_F = mean(AD_F),  v4EAD_F = mean(EAD_F))
  FoodMissBarDataSP1 <- melt(FoodMissBarDataSP, id.vars = c("DistRankCrossFoodSP","Study"))
  FoodMissBarDataSP1$measure = ifelse(FoodMissBarDataSP1$variable == "v1AD_I" | FoodMissBarDataSP1$variable == "v3AD_F","\nMean Absolute\nError\n(MAE)\n","\nMean Expected\nAbsolute Error\n(MEAE)\n")
  FoodMissBarDataSP1$type = ifelse(FoodMissBarDataSP1$variable == "v1AD_I" | FoodMissBarDataSP1$variable == "v2EAD_I","1. Initial","2. Final")
  FoodMissBarsSP <- ggplot(FoodMissBarDataSP1) + geom_bar(aes(x=type, y=value, fill=measure), stat="identity", position="dodge", width = 0.7) + facet_wrap(~DistRankCrossFoodSP) + theme_bw() + scale_fill_grey() + theme(axis.title.x = element_blank(), legend.position="right", legend.title = element_blank()) +scale_x_discrete(labels=c("1. Initial"="Initial","2. Final"="Final")) + ggtitle("2b. Calories, Expected Miss")+theme(plot.title = element_text(hjust = 0.5, size = 16)) + scale_y_continuous(limits=c(0,560),breaks=c(0,100,200,300,400,500))+ ylab("Calories")
  # CALORIES QUANTILE STUDY
  # Take means of initial and final AD and EAD within the four groups
  FoodQuantBarDataSP <-ddply(subset(AllData,StimType=="Food" & Study2=="FoodQuant" & is.na(DistRankCrossFoodSP)==FALSE), c("DistRankCrossFoodSP","Study"), summarise, v1AD_I = mean(AD_I), v2EAD_I = mean(EAD_I), v3AD_F = mean(AD_F),  v4EAD_F = mean(EAD_F))
  FoodQuantBarDataSP1 <- melt(FoodQuantBarDataSP, id.vars = c("DistRankCrossFoodSP","Study"))
  FoodQuantBarDataSP1$measure = ifelse(FoodQuantBarDataSP1$variable == "v1AD_I" | FoodQuantBarDataSP1$variable == "v3AD_F","\nMean Absolute\nError\n(MAE)\n","\nMean Expected\nAbsolute Error\n(MEAE)\n")
  FoodQuantBarDataSP1$type = ifelse(FoodQuantBarDataSP1$variable == "v1AD_I" | FoodQuantBarDataSP1$variable == "v2EAD_I","1. Initial","2. Final")
  FoodQuantBarsSP <- ggplot(FoodQuantBarDataSP1) + geom_bar(aes(x=type, y=value, fill=measure), stat="identity", position="dodge", width = 0.7) + facet_wrap(~DistRankCrossFoodSP) + theme_bw() + scale_fill_grey() + theme(axis.title.x = element_blank(), legend.position="right", legend.title = element_blank()) + scale_x_discrete(labels=c("1. Initial"="Initial","2. Final"="Final")) + ggtitle("2c. Calories, Percentiles")+theme(plot.title = element_text(hjust = 0.5, size = 16)) + scale_y_continuous(limits=c(0,560), breaks=c(0,100,200,300,400,500))+ ylab("Calories")
}
grid.arrange(FoodProbBarsSP, FoodMissBarsSP, FoodQuantBarsSP, nrow=3) # export as 6.5 x 10 
{
  # FIRST RUN THE TWO CHUNKS OF CODE ABOVE TO GENERATE THE INDIVIDUAL PANEL PLOTS FOR EACH STUDY
AgeProbBarDataSP1$measureAlt = ifelse(AgeProbBarDataSP1$variable == "v1AD_I" | AgeProbBarDataSP1$variable== "v3AD_F","\nAccuracy\n(Absolute Error)\n","\nConfidence\n(Expected Absolute Error)\n")
AgeProbBarsSPalt <- ggplot(AgeProbBarDataSP1) + geom_bar(aes(x=type, y=value, fill=measureAlt), stat="identity", position="dodge", width = 0.7) +facet_wrap(~DistRankCrossAgeSP) + theme_bw() + scale_fill_grey() + theme(axis.title.x = element_blank(), legend.position="bottom", legend.title = element_blank()) + scale_x_discrete(labels=c("1. Initial"="Initial","2. Final"="Final")) + scale_y_continuous(limits=c(0,10.5),breaks=c(0,2,4,6,8,10)) + ggtitle("1a. Ages, Probability Within C") + theme(plot.title = element_text(hjust = 0.5, size = 16), legend.text = element_text(hjust = 0.5, size = 11)) + ylab("Years")

grid_arrange_shared_legend <- function(...) {
  plots <- list(...)
  g <- ggplotGrob(plots[[1]] + theme(legend.position="bottom"))$grobs
  legend <- g[[which(sapply(g, function(x) x$name) == "guide-box")]]
  lheight <- sum(legend$height)
  grid.arrange(
    do.call(arrangeGrob, lapply(plots, function(x)
      x + theme(legend.position="none"))),
    legend,
    ncol = 1,
    heights = unit.c(unit(1, "npc") - lheight, lheight))
}
}

grid_arrange_shared_legend(AgeProbBarsSPalt + theme(legend.position = 'none'), FoodProbBarsSP + theme(legend.position = 'none'), AgeMissBarsSP + theme(legend.position = 'none'), FoodMissBarsSP + theme(legend.position = 'none'), AgeQuantBarsSP + theme(legend.position = 'none'), FoodQuantBarsSP + theme(legend.position = 'none')) 
grid.text((paste("(Note: Lower values correspond\nto greater accuracy or\ngreater confidence)")), x = unit(.73, "npc"), y = unit(.02, "npc"), just = c("left", "bottom"), gp = gpar(fontface = "bold.italic", fontsize = 11, lineheight=0.8)) # Figure 5S, export as 9.5 x 10 
}

##########################################################
# Accuracy of Initial, Final, Advice, Averaging, Figure 6
##########################################################
{
  AllData$InitACC <- abs(AllData$Init-AllData$Truth)
  AllData$FinACC <- abs(AllData$Fin-AllData$Truth)
  AllData$AdviceACC <- abs(AllData$Advice-AllData$Truth)
  AllData$AvgACC <- abs(0.5*AllData$Init+0.5*AllData$Advice-AllData$Truth)
  # plot Accuracy of Initial, Final, Advice, Averaging for Ages
  IFAAages <- ggplot(data=subset(AllData,StimType=="Age" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = InitACC, linetype = "Initial", color = "Initial"), se = F, method = "loess") + geom_smooth(aes(y = FinACC, linetype = "Final", color = "Final"), se = F, method = "loess") + geom_smooth(aes(y = AdviceACC, linetype = "Advice", color = "Advice"), se = F, method = "loess") + geom_smooth(aes(y = AvgACC, linetype = "Averaging", color = "Averaging"), se = F, method = "loess") + theme_bw(base_size=12) + scale_linetype_manual(name="Experiments\n1a, 1b, 1c. \n(Ages)",values=c("Initial"="dashed","Advice"="dotted","Final"="solid","Averaging"="dotdash")) + scale_color_manual(name="Experiments\n1a, 1b, 1c. \n(Ages)",values=c("Initial"="gray50","Final"="black","Advice"="gray60","Averaging"="gray30")) + coord_cartesian(xlim=c(0,25),ylim=c(0,20)) + labs(y="Accuracy (Absolute Error)", x="Distance Between Advice and Initial Estimate (in Years)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) + theme(legend.key.width = unit(4, "line"))
  
  IFAAfoods <- ggplot(data=subset(AllData,StimType=="Food" & RankCat %in% c(1,3,5)), aes(x=DistIA)) + geom_smooth(aes(y = InitACC, linetype = "Initial", color = "Initial"), se = F, method = "loess") + geom_smooth(aes(y = FinACC, linetype = "Final", color = "Final"), se = F, method = "loess") + geom_smooth(aes(y = AdviceACC, linetype = "Advice", color = "Advice"), se = F, method = "loess") + geom_smooth(aes(y = AvgACC, linetype = "Averaging", color = "Averaging"), se = F, method = "loess") + theme_bw(base_size=12) + scale_linetype_manual(name="Experiments\n2a, 2b, 2c. \n(Calories)",values=c("Initial"="dashed","Advice"="dotted","Final"="solid","Averaging"="dotdash")) + scale_color_manual(name="Experiments\n2a, 2b, 2c. \n(Calories)",values=c("Initial"="gray50","Final"="black","Advice"="gray60","Averaging"="gray30")) + coord_cartesian(xlim=c(0,1000),ylim=c(0,800)) + labs(y="Accuracy (Absolute Error)", x="Distance Between Advice and Initial Estimate (in Calories)") + theme(legend.position="left", panel.grid.minor = element_blank()) + facet_grid( ~ RankLab2) + theme(legend.key.width = unit(4, "line"))
  
  grid.arrange(IFAAages, IFAAfoods, nrow=2) # Figure6, export as 9.5 x 4.5
  
}


dev.off()

##########################################################
# to create Figure 1 and Figure 2
##########################################################
mat <- matrix(c(1:2), byrow = TRUE, ncol = 2, nrow = 1)
mat
layout(mat, widths = c(1,1), heights = c(1,1))
layout.show(max(mat))

# IOA illustration panel 1
par(mar=c(4, 2, 1, 1), mgp=c(1.2, 0.6, 0), tcl=-0.4)
mean1 = 400
sd1 = 70
x1 <- seq(-5, 5,length=100)*sd1 + mean1
hx1 <- dnorm(x1,mean1,sd1)
plot(NULL, bty = "n", xlim = c(100,800), ylim = c(0,.009), type="l", xlab="", ylab = "", xaxt = "none", yaxt = "none", lwd = 2, cex.lab = 1.4)
axis(1, seq(0,1000,50),las=1, labels = FALSE) 
title(xlab="\nx\n(a) initial opinion and advice\n", mgp=c(3.25,1,0), cex.lab = 1.4)
mean1 = 400
sd1 = 70
x1 <- seq(-5, 5,length=100)*sd1 + mean1
hx1 <- dnorm(x1,mean1,sd1)
lines(x1, hx1, lwd=3, col = "black", lty = 1)
abline(v = 700, lwd=2, lty = 2)
abline(v = 400, lwd=2, lty = 1)
arrows(x0 = 405, y0 = .0072, x1 = 695, y1 = .0072, length = .1, code = 3, lwd=2)
arrows(x0 = 405, y0 = .0028, x1 = 475, y1 = .0028, length = .12, code = 3, lwd=2 )
text(356, .0088, bquote(mu[italic("initial")]), cex = 1.4)
text(740, .0088, "advice", cex = 1.2)
text(550, .0076, bquote(Delta[italic("initial")]), cex = 1.4)
text(446, .0022, bquote(sigma[italic(initial)]), cex = 1.3)

# IOA illustration panel 2
mean2 = 550
sd2 = 50
x2 <- seq(-5, 5,length=100)*sd2 + mean2
hx2 <- dnorm(x2,mean2,sd2)
plot(NULL, bty = "n", xlim = c(100,800), ylim = c(0,.009), type="l", xlab="", ylab = "", xaxt = "none", yaxt = "none", lwd = 3, cex.lab = 1.4)
axis(1, seq(0,1000,50),las=1, labels = FALSE) 
title(xlab="\nx\n(b) final opinion added\n", mgp=c(3.25,1,0), cex.lab = 1.4)
lines(x1, hx1, lwd=3, col = "black", lty = 1)
lines(x2, hx2, lwd=3, col = "black", lty = 4)
abline(v = 700, lwd=2, lty = 2)
abline(v = 550, lwd=2, lty = 4)
arrows(x0 = 555, y0 = .0070, x1 = 695, y1 = .0070, length = .12, code = 3, lwd=2 )
arrows(x0 = 555, y0 = .0042, x1 = 602, y1 = .0042, length = .10, code = 3, lwd=2 )
text(580, 0.0036, bquote(sigma[italic("final")]), cex = 1.34)
text(625, 0.0076, bquote(Delta[italic("final")]), cex = 1.4)
text(510, .0088, bquote(mu[italic("final")]), cex = 1.4)
text(740, .0088, "advice", cex = 1.2)
# export as 13 x 4 pdf, Figure 1

mat <- matrix(c(1:3), byrow = TRUE, ncol = 3, nrow = 1)
mat
layout(mat, widths = c(1,1), heights = c(1,1))
layout.show(max(mat))

# EXAMPLE 1
par(mar=c(4, 2, 1, 1), mgp=c(1.2, 0.6, 0), tcl=-0.4)
mean1 = 400
sd1 = 70
mean2 = 550
sd2 = 60
x1 <- seq(-5, 5,length=100)*sd1 + mean1
hx1 <- dnorm(x1,mean1,sd1)
x2 <- seq(-5, 5,length=100)*sd2 + mean2
hx2 <- dnorm(x2,mean2,sd2)
plot(NULL, bty = "n", xlim = c(200,900), ylim = c(0,.009), type="l", xlab="", ylab = "", xaxt = "none", yaxt = "none", lwd = 2, cex.lab = 1.4)
axis(1, seq(0,1000,50),las=1, labels = FALSE)
title(xlab="\nx\n(a) opinion shift (OS) dominates\n", mgp=c(3.25,1,0), cex.lab = 1.4)
lines(x1, hx1, lwd=3, col = "black", lty = 1)
lines(x2, hx2, lwd=3, col = "black", lty = 4)
abline(v = 700, lty = 2, lwd=2)
text(589, .0091, "advice = 700", cex = 1.3)
par(lheight=0.2)
text(276, .0052, bquote(atop(atop(mu[italic("initial")]~"= 400", sigma[italic("initial")]~"= 70"))), cex = 2)
text(526, .0069, bquote(atop(atop(mu[italic("final")]~"= 550", sigma[italic("final")]~" = 60"))), cex = 2)
par(lheight = 1)
lines <- "WOA: 0.500 \n   IOA: 0.724 \n    OS: 0.711 \n   CC: 0.014"
text(830, .0075, lines, cex = 1.2)


# EXAMPLE 2
par(mar=c(4, 2, 1, 1), mgp=c(1.2, 0.6, 0), tcl=-0.4)
mean1 = 650
sd1 = 70
mean2 = 675
sd2 = 60
x1 <- seq(-5, 5,length=100)*sd1 + mean1
hx1 <- dnorm(x1,mean1,sd1)
x2 <- seq(-5, 5,length=100)*sd2 + mean2
hx2 <- dnorm(x2,mean2,sd2)
plot(NULL, bty = "n", xlim = c(200,900), ylim = c(0,.009), type="l", xlab="",
     ylab = "", xaxt = "none", yaxt = "none", lwd = 2, cex.lab = 1.2)
axis(1, seq(0,1000,50),las=1, labels = FALSE)
title(xlab="\nx\n(b) OS and CC both contribute\n", mgp=c(3.25,1,0), cex.lab = 1.4)
lines(x1, hx1, lwd=3, col = "black", lty = 1)
lines(x2, hx2, lwd=3, col = "black", lty = 4)
abline(v = 700, lty = 2, lwd=2)
text(589, .0091, "advice = 700", cex = 1.3)
par(lheight=0.2)
text(470, .0032, bquote(atop(atop(mu[italic("initial")]~"= 650", sigma[italic("initial")]~" = 70"))), cex = 2)
text(550, .0060, bquote(atop(atop(mu[italic("final")]~"= 675", sigma[italic("final")]~" = 60"))), cex = 2)
par(lheight = 1)
lines <- "WOA: 0.500 \n   IOA: 0.429 \n    OS: 0.253 \n   CC: 0.176"
text(830, .0075, lines, cex = 1.2)


# EXAMPLE 3
par(mar=c(4, 2, 1, 1), mgp=c(1.2, 0.6, 0), tcl=-0.4)
mean1 = 700
sd1 = 70
mean2 = 700
sd2 = 60
x1 <- seq(-5, 5,length=100)*sd1 + mean1
hx1 <- dnorm(x1,mean1,sd1)
x2 <- seq(-5, 5,length=100)*sd2 + mean2
hx2 <- dnorm(x2,mean2,sd2)
plot(NULL, bty = "n", xlim = c(200,900), ylim = c(0,.009), type="l", xlab="",
     ylab = "", xaxt = "none", yaxt = "none", lwd = 2, cex.lab = 1.2)
axis(1, seq(0,1000,50),las=1, labels = FALSE)
title(xlab="\nx\n(c) confidence change (CC) dominates\n", mgp=c(3.25,1,0), cex.lab = 1.4)
lines(x1, hx1, lwd=3, col = "black", lty = 1)
lines(x2, hx2, lwd=3, col = "black", lty = 4)
abline(v = 700, lty = 2, lwd=2)
text(589, .0091, "advice = 700", cex = 1.3)
par(lheight=0.2)
text(490, .0018, bquote(atop(atop(mu[italic("initial")]~"= 700", sigma[italic("initial")]~" = 70"))), cex = 2)
text(568, .0058, bquote(atop(atop(mu[italic("final")]~"= 700", sigma[italic("final")]~" = 60"))), cex = 2)
par(lheight = 1)
lines <- "  WOA: undef.   \n IOA: 0.265 \n  OS: 0.000 \n CC: 0.265"
text(830, .0075, lines, cex = 1.2)
# export as 9.7 x 3.5 pdf, Figure 2
