# Section 3.2 script

# adopter-level income data is proprietary and cannot be shared
# this script includes code to replicate figures 5-7

source("prep.R")

# set working directory to the data repositor
### setwd("YOUR WD")

# Figure 5
f5<-read.csv("Data/figure5.csv")
pfgr<-function(ttl,yl,mv,p20v,p80v,ym) {
  a<-f5 %>% mutate(md=mv,lo=p20v,hi=p80v)
  b<-base(a)+ggtitle(ttl)+coord_cartesian(xlim=c(0.5,3.5),ylim=c(0,ym))+
    geom_segment(aes(x=x,xend=x,y=lo,yend=hi,color=xpos))+
    geom_point(aes(x=x,y=md,color=xpos,shape=smpl),fill="white",size=6)+
    scale_color_manual(values=c("tan1","navy","darkcyan"),guide="none")+
    scale_shape_manual(values=c(21,16),name="Sample:",labels=c("Full","High confidence"))+ylab(yl)+
    scale_x_continuous(breaks=c(1:3),labels=c("TPO","Loan","Customer\nFinanced"))+
    theme(axis.title.x=element_blank(),legend.position="bottom")
  return(b)
}
ggsave("Figures/figure5.jpg",
       plot=plot_grid(pfgr("Adopter Income","$1000",f5$mdInc,f5$p20Inc,f5$p80Inc,250)+
                        theme(axis.text.x=element_blank(),legend.position="none")+
                        ant("p20",0.63,67,4,'gray50')+ant("p50",0.63,122,4,'gray50')+ant("p80",0.63,195,4,'gray50'),
                      pfgr("Adopter Relative Income","    %  ",f5$mdRel,f5$p20Rel,f5$p80Rel,240),nrow=2,rel_heights=c(60,75)),width=4.5,height=8.5)

# Figure 6
f6<-read.csv("Data/figure6.csv")
ggsave("Figures/figure6.jpg",
       base(f6)+coord_cartesian(ylim=c(100,180))+
         geom_ribbon(aes(x=year,ymin=lo,ymax=hi,fill=fnc),alpha=0.6)+
         geom_line(aes(year,rel,color=fnc))+
         geom_line(aes(year,relH,color=fnc),linetype='dashed')+
         scale_color_manual(values=c('darkcyan','navy','tan1'),guide="none")+
         scale_fill_manual(values=c('darkcyan','navy','tan1'),guide="none")+
         ant("Customer financed",2021,170,5,'darkcyan')+ant("Loans",2019,146,5,'navy')+
         ant("TPO",2023,140,5,'tan1')+
         
         geom_segment(x=2017,xend=2017.8,y=110,yend=110,color='gray50')+ant("Full sample",2018.7,110,4,'gray50')+
         geom_segment(x=2017,xend=2017.8,y=105,yend=105,color='gray50',linetype="dashed")+ant("High confidence",2019,105,4,'gray50')+
         ylab("Adopter Relative\nIncome (%)")+
         theme(axis.title.x=element_blank()),width=6,height=4)

# Figure 7
f7<-read.csv("Data/figure7.csv")
bfgr<-function(ss,ttl,xl) {
  base(filter(f7,smpl==ss))+ggtitle(ttl)+
    coord_cartesian(xlim=c(0.5,xl))+
    geom_col(aes(bin,pct,fill=fnc),position='stack')+
    scale_fill_manual(values=c("darkcyan","navy","tan1"),guide="none")+
    scale_y_continuous(breaks=seq(0,80,20))+
    geom_text(aes(bin,ly,label=label),color='white',size=4)+
    ylab("Share of\nAdopters\n(%)")+
    scale_x_continuous(breaks=c(1:4),
                       labels=c("<50%","50-\n100%","100-\n150%",">150%"))+
    theme(axis.title.x=element_blank())
}
ggsave("Figures/figure7.jpg",
       plot=ggdraw()+draw_plot(plot_grid(bfgr("F","Full Sample",4.5),
                                         bfgr("H","High Confidence",4.5)+noY,nrow=1,rel_widths=c(68,50)),0,0.12,0.88,0.88)+
         draw_text("Adopter Income Bracket",0.5,0.1,14,color='gray50')+
         draw_text("(% of County Median Income)",0.5,0.05,14,color='gray50')+
         draw_text("Customer",0.86,0.88,11,color='darkcyan',hjust=0)+draw_text("financed",0.86,0.84,11,color='darkcyan',hjust=0)+
         draw_text("Loans",0.86,0.7,11,color='navy',hjust=0)+draw_text("TPO",0.86,0.45,11,color='tan1',hjust=0),width=8,height=4)

