forked from sguo28/codes-energy-policy
/
.Rhistory
512 lines (512 loc) · 25.4 KB
/
.Rhistory
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
ggsave("Figures/suits_among_groups.pdf",width = 8.7/2, height = 8.7/3 ,dpi = 300)
summarytotalrebate <- read_csv("Data/summarytotalrebate.csv")
ggplot(summarytotalrebate,aes(x=year,y=total_rebate,group=income_group,fill=income_group))+
geom_bar(stat="identity", position=position_dodge(),alpha=0.7)+
# scale_fill_manual(values = brewer.pal(n = 4, name = "Set1"))+
ggtitle("Total rebate comparison between income groups by year")+
scale_x_continuous(breaks=seq(2011, 2018, 1))+
theme_bw()+
scale_fill_tron()+
theme(legend.position="bottom")+
ggsave("Figures/total_rebate.pdf",width = 8.7, height = 8.7/2, dpi = 300)
lisaplotting <- read_csv("Data/lisaplotting.csv")
lisaplotting %>%ggplot(aes(x= MORAN_STD,y=MORAN_LAG))+
geom_point(alpha = 0.3)+
geom_abline(intercept = 0, slope = 0.618,color = "red", linetype = "dashed",size = 1)+
scale_color_futurama() +
xlab("std of the variable")+
ylab("lag of the variable")+
ggtitle("Moran'I scatter plot")+
geom_hline(yintercept = 0)+
geom_vline(xintercept = 0)+
geom_text(x=10, y=10, label="I",size=4)+
geom_text(x=10, y=-10, label="IV",size=4)+
geom_text(x=-10, y=10, label="II",size=4)+
geom_text(x=-10, y=-10, label="III",size=4)+
coord_fixed()+
xlim(-10,10)+
ylim(-10,10)+
theme_bw()+
ggsave("Figures/moranscatterplot.pdf",width = 8.7/2,height = 8.7/2, dpi = 300)
save.image("~/GitHub/codes-energy-policy/.RData")
library(readxl)
# Data Cleaning
library(tidyverse)
library(readr)
library(readxl)
library(dplyr)
library(ggplot2)
library(grid)
library(zoo)
library(scales)
library(ggpubr)
library(REAT)
library(hrbrthemes)
library(viridis)
library(dygraphs)
library(xts) # To make the convertion data-frame / xts format
library(RColorBrewer)
library(ggsci)
# select the attributes we desired
completeFun <- function(data, desiredCols) {
completeVec <- complete.cases(data[, desiredCols])
return(data[completeVec, ])
}
# insert preprocessed data
dat<-read_csv("Data/dat1.csv", col_names = TRUE)
# density of income-group and DAC
sample_dat<-dat
sample_dat$`low.income`<-sample_dat$`Low-Income Communities`
ggdensity(sample_dat, x = "num_tot",
add = "mean", rug = TRUE,
color = "low.income", fill = "low.income",
palette = c("#0073C2FF", "#FC4E07"),xlim=c(0,400),xlab = "PEV numbers")+
ggsave("Figures/density_low_income.pdf",width = 8.7/2 ,height = 8.7/3, dpi = 300)
ggdensity(sample_dat, x = "num_tot",
add = "mean", rug = TRUE,
color = "DAC", fill = "DAC",
palette = c("#0073C2FF", "#FC4E07"),xlim=c(0,400),xlab = "PEV numbers")+
ggsave("Figures/density_dac.pdf",width = 8.7/2,height = 8.7/3, dpi = 300)
# distribution of capita rebate ordered by CES and income percentile before and after income-cap.
completeFun(dat,c("CES 3.0 Percentile","rebate_capita_average","rebate_capita_average_before","rebate_capita_average_after")) %>%filter(rebate_capita_average < 100)%>%ggplot()+
geom_smooth(aes(x=`CES 3.0 Percentile`,y=`rebate_capita_average_before`,color="before income cap",linetype="before income cap"),alpha = 0.25)+
geom_smooth(aes(x=`CES 3.0 Percentile`,y=`rebate_capita_average_after`,color="after income cap",linetype="after income cap"),alpha = 0.25)+
xlab("CES Percentile (High value means disadvantaged)")+
ylab("Total rebate")+
labs(color = NULL,linetype = NULL)+
scale_color_aaas()+
theme_bw()+
theme(legend.position="bottom")+
geom_vline(xintercept = 75,linetype="dashed")+
geom_text(x=75, y=5, label="DAC line",angle = 0,size=4)+
ggtitle("Total rebate ordered by CES percentile")+
ggsave("Figures/ces_capita.pdf",width = 8.7/2, height = 8.7/3, dpi = 300)
income_rebate_capita<-completeFun(dat,c("income_md","rebate_capita_average","rebate_capita_average_before","rebate_capita_average_after")) %>%filter(rebate_capita_average < 100)
income_rebate_capita%>%ggplot()+
geom_smooth(aes(x=`income_md`,y=`rebate_tot_before`,color=as.factor("before income cap"),linetype=as.factor("before income cap")),alpha = 0.25)+
geom_smooth(aes(x=`income_md`,y=`rebate_tot_after`,color=as.factor("after income cap"),linetype=as.factor("after income cap")),alpha = 0.25)+
xlab("Median income")+
ylab("Total rebate")+
labs(color =NULL,linetype= NULL)+
ggtitle("Total rebate ordered by median income")+
scale_color_aaas()+
theme_bw()+
theme(legend.position="bottom")+
ggsave("Figures/income_capita.tiff",width = 8.7/2, height = 8.7/3, dpi = 300)
income_rebate_capita_stacked<-rbind(income_rebate_capita,income_rebate_capita)
ggdensity(income_rebate_capita, x = "income_md",
add = "mean", rug = TRUE,
color = "DAC", fill = "DAC",
palette = c("#0073C2FF", "#FC4E07"),xlim=c(0,400),xlab = "PEV numbers")+
ggsave("Figures/density_dac.pdf",width = 8.7/2,height = 8.7/3, dpi = 300)
# income_rebate_capita_stacked<-rbind(income_rebate_capita,income_rebate_capita)
# ggdensity(income_rebate_capita, x = "income_md",
# add = "mean", rug = TRUE,
# color = "DAC", fill = "DAC",
# palette = c("#0073C2FF", "#FC4E07"),xlim=c(0,400),xlab = "PEV numbers")+
# ggsave("Figures/density_dac.pdf",width = 8.7/2,height = 8.7/3, dpi = 300)
# distribution of capita rebate ordered by CES and income percentile before and after income-cap.
completeFun(dat,c("CES 3.0 Percentile","rebate_capita_average","rebate_capita_average_before","rebate_capita_average_after")) %>%filter(rebate_capita_average < 100)%>%ggplot()+
geom_smooth(aes(x=`CES 3.0 Percentile`,y=`rebate_capita_average_before`,color="before income cap",linetype="before income cap"),alpha = 0.25)+
geom_smooth(aes(x=`CES 3.0 Percentile`,y=`rebate_capita_average_after`,color="after income cap",linetype="after income cap"),alpha = 0.25)+
xlab("CES Percentile (High value means disadvantaged)")+
ylab("Total rebate")+
labs(color = NULL,linetype = NULL)+
scale_color_aaas()+
theme_bw()+
theme(legend.position="bottom")+
geom_vline(xintercept = 75,linetype="dashed")+
geom_text(x=75, y=5, label="DAC line",angle = 0,size=4)+
ggtitle("Total rebate ordered by CES percentile")+
ggsave("Figures/ces_capita.pdf",width = 8.7/2, height = 8.7/3, dpi = 300)
income_rebate_capita<-completeFun(dat,c("income_md","rebate_capita_average","rebate_capita_average_before","rebate_capita_average_after")) %>%filter(rebate_capita_average < 100)
income_rebate_capita%>%ggplot()+
geom_smooth(aes(x=`income_md`,y=`rebate_tot_before`,color=as.factor("before income cap"),linetype=as.factor("before income cap")),alpha = 0.25)+
geom_smooth(aes(x=`income_md`,y=`rebate_tot_after`,color=as.factor("after income cap"),linetype=as.factor("after income cap")),alpha = 0.25)+
xlab("Median income")+
ylab("Total rebate")+
labs(color =NULL,linetype= NULL)+
ggtitle("Total rebate ordered by median income")+
scale_color_aaas()+
theme_bw()+
theme(legend.position="bottom")+
ggsave("Figures/income_capita.tiff",width = 8.7/2, height = 8.7/3, dpi = 300)
# income_rebate_capita_stacked<-rbind(income_rebate_capita,income_rebate_capita)
# ggdensity(income_rebate_capita, x = "income_md",
# add = "mean", rug = TRUE,
# color = "DAC", fill = "DAC",
# palette = c("#0073C2FF", "#FC4E07"),xlim=c(0,400),xlab = "PEV numbers")+
# ggsave("Figures/density_dac.pdf",width = 8.7/2,height = 8.7/3, dpi = 300)
# Suits Lorenz Curve
dat4fun2 <-completeFun(dat,c("CES 3.0 Percentile","rebate_tot_before","rebate_tot_after")) %>% arrange(1-`CES 3.0 Percentile`) %>% mutate(y.before=cumsum(rebate_tot_before)/sum(rebate_tot_before),y.after = cumsum(rebate_tot_after)/sum(rebate_tot_after))%>%
mutate(order = seq(0,1,1/(nrow(.)-1)))
ggplot(dat4fun2)+
geom_line(aes(x=order,y=`y.before`,color="before income cap"))+
geom_line(aes(x=order,y=`y.after`,color="after income cap"))+
geom_text(x=0.75, y=.25, label="DAC line",angle = 0,size=4)+
scale_color_aaas()+
geom_abline(intercept=0,slope=1,linetype="dashed")+
xlab("Cumulative of census tracts by decreasing CES3.0 score")+
ylab("Cumulative share of total rebate") +
ggtitle("Suits - total rebate vs. CES score")+
geom_vline(xintercept = 0.25,linetype="dashed")+
coord_fixed()+
labs(color = "")+
theme_bw()+
theme(legend.position="bottom")+
ggsave("Figures/lorenz_dac.pdf",width = 8.7/2,height = 8.7/2, dpi = 300)
# dat4fun2$y.after[5135]
# dat4fun2$y.before[5135]
dat4fun3 <-completeFun(dat,c("income_md","rebate_tot_before","rebate_tot_after")) %>% arrange(income_md) %>% mutate(y.before=cumsum(rebate_tot_before)/sum(rebate_tot_before),y.after = cumsum(rebate_tot_after)/sum(rebate_tot_after))%>%
mutate(order = seq(0,1,1/(nrow(.)-1)))
ggplot(dat4fun3)+
geom_line(aes(x=order,y=`y.before`,color="before income cap"))+
geom_line(aes(x=order,y=`y.after`,color="after income cap"))+
geom_text(x=0.90, y=.25, label="top 10%",angle = 0,size=4)+
scale_color_aaas()+
geom_abline(intercept=0,slope=1,linetype="dashed")+
xlab("Cumulative of census tracts by increasing income")+
ylab("Cumulative share of total rebate") +
ggtitle("Suits - total rebate vs. median income")+
geom_vline(xintercept = 0.90,linetype="dashed")+
coord_fixed()+
labs(color = "")+
theme_bw()+
theme(legend.position="bottom")+
ggsave("Figures/lorenz_income.pdf",width = 8.7/2,height = 8.7/2, dpi = 300)
# lorenz curve in overall level
# number
completeFun(dat,c("rebate_tot","num_tot"))%>%arrange(`num_tot`)%>% mutate(y=cumsum(rebate_tot)/sum(rebate_tot))%>% ggplot(aes(x=seq(0,1,1/(nrow(.)-1)),y=y))+
geom_line() +
geom_abline(intercept=0,slope=1,linetype="dashed")+
xlab("By increasing PEV buyers")+
ylab("Cumulative share of total rebate") +
ggtitle("Gini: PEV buyers")+
coord_fixed()+
theme_bw()+
ggsave("Figures/lorenz_gini.pdf",width = 8.7/3,height = 8.7/3, dpi = 300)
# income
completeFun(dat,c("rebate_tot","income_md","num_tot"))%>%arrange(income_md)%>% mutate(y=cumsum(rebate_tot)/sum(rebate_tot),y1=cumsum(income_md)/sum(income_md))%>%
ggplot(aes(x=seq(0,1,1/(length(y) -1))))+
geom_line(aes(y=y))+
geom_abline(intercept=0,slope=1,linetype="dashed")+
xlab("By increasing median income")+
ylab("Cumulative share of total rebate") +
ggtitle("Suits: median income")+
coord_fixed()+
theme_bw()+
ggsave("Figures/lorenz_suits_income.pdf",width = 8.7/3,height = 8.7/3, dpi = 300)
# ces3.0
completeFun(dat,c("rebate_tot","num_tot","CES 3.0 Score"))%>%arrange(100-`CES 3.0 Score` )%>% mutate(y=cumsum(rebate_tot)/sum(rebate_tot))%>% mutate(y=cumsum(rebate_tot)/sum(rebate_tot),y1=cumsum(income_md)/sum(income_md))%>%
ggplot(aes(x=seq(0,1,1/(length(y) -1))))+
geom_line(aes(y=y))+
geom_abline(intercept=0,slope=1,linetype="dashed")+
xlab("By decreasing CES3.0 score")+
ylab("Cumulative share of total rebate") +
ggtitle("Suits: CES3.0 score")+
coord_fixed()+
theme_bw()+
ggsave("Figures/lorenz_suits_ces.pdf",width = 8.7/3,height = 8.7/3, dpi = 300)
adoptionev <- read_csv("Data/adoptionev.csv") # data from https://autoalliance.org/energy-environment/advanced-technology-vehicle-sales-dashboard/, BEV/PHEV sales per quarter
rebate_adoption_rate<-data.frame(`adoption.rate_bev`=adoptionev[1:33,]$adoption_rate, "adoption.rate_phev"=adoptionev[34:66,]$adoption_rate)
pt<-ts(rebate_adoption_rate, frequency = 4, start = c(2011, 1))
a <- data.frame(Time=c(time(pt)),rebate_adoption_rate)
a[1:4,3]<-NA # omit 0
a$Time<-yearqtr(a$Time)
a[21,2]<-1.00 # the raw sales record is slight low than CVRP record (101.38987%), we make a modification here.
ggplot(a)+ # the 34th one is overall average value, so omit that.( 0.55994735 , 0.434460486)
geom_line(aes(x=Time,y=adoption.rate_phev, color = "PHEV", linetype = "PHEV")) +
geom_line(aes(x=Time,y=adoption.rate_bev,color = "BEV", linetype = "BEV")) +
geom_point(aes(x=Time,y=adoption.rate_phev, color = "PHEV", shape = "PHEV")) +
geom_point(aes(x=Time,y=adoption.rate_bev,color = "BEV", shape = "BEV")) +
# geom_rect(aes(xmin=2016.2383, xmax=2016.8356, ymin=-Inf, ymax=Inf),fill='#FF3300',alpha = .02)+
geom_vline(xintercept = 2016,linetype=4, colour="grey50" )+
# geom_vline(xintercept = 2016.8356,linetype=4, colour="grey50" )+
scale_color_manual(values = c("PHEV"="#0073C2FF","BEV"="#FC4E07"))+
geom_text(x=2017.5, y=1.0, label="after income cap",size=5)+
geom_text(x=2013.5, y=1.0, label="before income cap ",size=5)+
geom_text(x=2016, y=.25, label="3/29/2016",angle = 0,size=5)+
xlab('year - quarter') +
ylab('CVRP / PEV registration')+
ylim(0,1)+
labs(color = NULL, shape = NULL, linetype = NULL )+
theme_bw() +
scale_x_continuous(breaks=seq(2011, 2019, 0.25),labels=as.character(adoptionev[1:33,]$Time))+
theme(legend.position="bottom")+
ggtitle("The percentage of CVRP in total PEV registration (2011 Q1 - 2019 Q1)")+ theme(axis.text.x = element_text(angle=90, hjust=1, vjust=.5))+
ggsave("Figures/adoptionev.pdf",width = 8.7, height = 8.7/2.5, dpi = 300)
# moran's I line graph
moran1 <- read_csv("Data/moran.csv") # export from Geoda, moran'I for each year
ggplot(moran1)+ # the 34th one is overall average value, so omit that.( 0.55994735 , 0.434460486)
geom_line(aes(x=year,y=uni_total_rebate, color = "uni_rebate",linetype = "uni_rebate")) +
geom_line(aes(x=year,y=bi_tot_rebate_income,color = "bi_rebate_income", linetype = "bi_rebate_income")) +
geom_line(aes(x=year,y=bi_tot_rebate_ces,color = "bi_rebate_ces", linetype = "bi_rebate_ces")) +
geom_point(aes(x=year,y=uni_total_rebate, color = "uni_rebate",shape = "uni_rebate")) +
geom_point(aes(x=year,y=bi_tot_rebate_income,color = "bi_rebate_income", shape = "bi_rebate_income")) +
geom_point(aes(x=year,y=bi_tot_rebate_ces,color = "bi_rebate_ces", shape = "bi_rebate_ces")) +
xlab('year') +
ylab("Moran's I Index")+
labs(color = NULL, shape = NULL, linetype = NULL )+
scale_color_aaas() +
theme_bw() +
theme(legend.position="bottom")+
ggtitle("Moran'I index")+
scale_x_continuous(breaks=seq(2011, 2019, 1))+
ggsave("Figures/moran.pdf",width = 8.7/2, height = 8.7/3 ,dpi = 300)
# plot lorenz curves for each year: PEV num, income, and DAC.
plot_list = list()
for (i in 1:9) {
dat2<-na.omit(as.matrix(dat[,c(2*i+2,3*i+21,55,3*i+23)]))
dat.income <- dat2[order(dat2[,1]),]# income
dat.dac <- dat2[order(dat2[,3], decreasing = TRUE),]# dac
dat.num <- dat2[order(dat2[,4]),] # num_pev_buyer
# overall
dat.dac
y.income <- cumsum(dat.income[,2])/sum(dat.income[,2])
y.dac <- cumsum(dat.dac[,2])/sum(dat.dac[,2])
y.num <- cumsum(dat.num[,2])/sum(dat.num[,2])
order <- seq(0,1,1/(nrow(dat2)-1))
plot_list[[i]] = as.data.frame(cbind(y.num,y.income,y.dac,order))
}
p1<-ggplot(NULL)+
geom_line(data=plot_list[[1]],aes(x=order,y=y.num,col=as.factor(2010)))+
geom_line(data=plot_list[[2]],aes(x=order,y=y.num,col=as.factor(2011)))+
geom_line(data=plot_list[[3]],aes(x=order,y=y.num,col=as.factor(2012)))+
geom_line(data=plot_list[[4]],aes(x=order,y=y.num,col=as.factor(2013)))+
geom_line(data=plot_list[[5]],aes(x=order,y=y.num,col=as.factor(2014)))+
geom_line(data=plot_list[[6]],aes(x=order,y=y.num,col=as.factor(2015)))+
geom_line(data=plot_list[[7]],aes(x=order,y=y.num,col=as.factor(2016)))+
geom_line(data=plot_list[[8]],aes(x=order,y=y.num,col=as.factor(2017)))+
geom_line(data=plot_list[[9]],aes(x=order,y=y.num,col=as.factor(2018)))+
labs(color = "Year")+
scale_color_manual(values = brewer.pal(n = 9, name = "Oranges"))+
geom_abline(intercept=0,slope=1,linetype="dashed")+
xlab("Cumulative share of census tract")+
coord_fixed()+
ylab("Cumulative share of total rebate") +
ggtitle("Gini: increasing order of PEV buyers")+
theme_bw()+
ggsave("Figures/suits-rebate-year.pdf",width = 8.7/2,height = 8.7/2, dpi = 300)
p2<-ggplot(NULL)+
geom_line(data=plot_list[[1]],aes(x=order,y=y.income,col=as.factor(2010)))+
geom_line(data=plot_list[[2]],aes(x=order,y=y.income,col=as.factor(2011)))+
geom_line(data=plot_list[[3]],aes(x=order,y=y.income,col=as.factor(2012)))+
geom_line(data=plot_list[[4]],aes(x=order,y=y.income,col=as.factor(2013)))+
geom_line(data=plot_list[[5]],aes(x=order,y=y.income,col=as.factor(2014)))+
geom_line(data=plot_list[[6]],aes(x=order,y=y.income,col=as.factor(2015)))+
geom_line(data=plot_list[[7]],aes(x=order,y=y.income,col=as.factor(2016)))+
geom_line(data=plot_list[[8]],aes(x=order,y=y.income,col=as.factor(2017)))+
geom_line(data=plot_list[[9]],aes(x=order,y=y.income,col=as.factor(2018)))+
labs(color = "Year")+
scale_color_manual(values = brewer.pal(n = 9, name = "Oranges"))+
geom_abline(intercept=0,slope=1,linetype="dashed")+
xlab("Cumulative share of census tract")+
ylab("Cumulative share of total rebate") +
coord_fixed()+
ggtitle("Suits: increasing order of median income")+
theme_bw()+
ggsave("Figures/suits-rebate-income-year.pdf",width = 8.7/2,height = 8.7/2, dpi = 300)
p3<-ggplot(NULL)+
geom_line(data=plot_list[[1]],aes(x=order,y=y.dac,col=as.factor(2010)))+
geom_line(data=plot_list[[2]],aes(x=order,y=y.dac,col=as.factor(2011)))+
geom_line(data=plot_list[[3]],aes(x=order,y=y.dac,col=as.factor(2012)))+
geom_line(data=plot_list[[4]],aes(x=order,y=y.dac,col=as.factor(2013)))+
geom_line(data=plot_list[[5]],aes(x=order,y=y.dac,col=as.factor(2014)))+
geom_line(data=plot_list[[6]],aes(x=order,y=y.dac,col=as.factor(2015)))+
geom_line(data=plot_list[[7]],aes(x=order,y=y.dac,col=as.factor(2016)))+
geom_line(data=plot_list[[8]],aes(x=order,y=y.dac,col=as.factor(2017)))+
geom_line(data=plot_list[[9]],aes(x=order,y=y.dac,col=as.factor(2018)))+
geom_line(size = 1) +
labs(color = "Year")+
# scale_color_viridis_d()+
scale_color_manual(values = brewer.pal(n = 9, name = "Oranges"))+
geom_abline(intercept=0,slope=1,linetype="dashed")+
xlab("Cumulative share of census tract")+
ylab("Cumulative share of total rebate") +
ggtitle("Suits: decreasing order of DAC level")+
coord_fixed()+
theme_bw()+
theme(legend.position= "none")+
ggsave("Figures/suits-rebate-dac-year.pdf",width = 8.7/2,height = 8.7/2, dpi = 300)
p3
# get data for income groups, four specific groups
gini4use<-function(y){
l<-c()
for (i in 1:(length(y)-1)) {
l[i]=1/2*(y[i]+y[i+1])/(length(y)-1)
}
return(1-2*sum(l))
}
theil_index<-function(y1){
l<-c()
y<-y1
for (i in 1:(as.numeric(length(y)))-1){
l[i]=(y[i]/mean(y))*log(y[i]/mean(y))
}
l<-na.omit(l)
return(mean(l))
}
rbt1 = rbt2 = rbt3 = rbt4<-c()
theil = theil1 = theil2 = theil3 = theil4<-c()
gini.group1 = gini.group2 =gini.group3 =gini.group4 <-c()
spec.group.rebate1 = spec.group.rebate2 =spec.group.rebate3 =spec.group.rebate4 <-c()
gini_num = suits_income = suits_dac<-c()
for (i in 1:9) {
dat2<-na.omit(as.data.frame(dat[,c(2*i+2,3*i+21,55,3*i+23,58:61)]))
dat.income <- dat2[order(dat2[,1]),]# income
dat.dac <- dat2[order(dat2[,3], decreasing = TRUE),]# dac
dat.num <- dat2[order(dat2[,4]),] # num_pev_buyer
# overall
y.income <- cumsum(dat.income[,2])/sum(dat.income[,2])
y.dac <- cumsum(dat.dac[,2])/sum(dat.dac[,2])
y.num <- cumsum(dat.num[,2])/sum(dat.num[,2])
order <- seq(0,1,1/(nrow(dat2)-1))
gini_num[i]<-gini4use(y.num)
suits_income[i]<-gini4use(y.income)
suits_dac[i]<-gini4use(y.dac)
# four income categories for bar charts
income1<-dat.income[which(dat.income[,1]<50000),]
income2<-dat.income[which(100000>dat.income[,1],dat.income[,1]>50000),]
income3<-dat.income[which(150000>dat.income[,1],dat.income[,1]>100000),]
income4<-dat.income[which(dat.income[,1]>150000),]
rbt1[i] <- sum(income1[,2])
rbt2[i] <- sum(income2[,2])
rbt3[i] <- sum(income3[,2])
rbt4[i] <- sum(income4[,2])
### four specific groups, note that ordered by income here, actually for Suits Calculation, but can be changed to PEV buyers/DAC
group1<-dat.income[which(dat.num[,5]=="Yes"),]
group2<-dat.income[which(dat.num[,6]=="Yes"),]
group3<-dat.income[which(dat.num[,7]=="Yes"),]
group4<-dat.income[which(dat.num[,8]=="Yes"),]
# group1<-dat.num[which(dat.num[,5]=="Yes"),]
# group2<-dat.num[which(dat.num[,6]=="Yes"),]
# group3<-dat.num[which(dat.num[,7]=="Yes"),]
# group4<-dat.num[which(dat.num[,8]=="Yes"),]
# group1<-dat.num[which(dat.dac[,5]=="Yes"),]
# group2<-dat.num[which(dat.dac[,6]=="Yes"),]
# group3<-dat.num[which(dat.dac[,7]=="Yes"),]
# group4<-dat.num[which(dat.dac[,8]=="Yes"),]
# total rebate
spec.group.rebate1[i] <- sum(group1[,2])
spec.group.rebate2[i] <- sum(group2[,2])
spec.group.rebate3[i] <- sum(group3[,2])
spec.group.rebate4[i] <- sum(group4[,2])
# for gini/suits/theil calculation
y1 <- cumsum(group1[,2])/spec.group.rebate1[i]
y2 <- cumsum(group2[,2])/spec.group.rebate2[i]
y3 <- cumsum(group3[,2])/spec.group.rebate3[i]
y4 <- cumsum(group4[,2])/spec.group.rebate4[i]
# theil for specific groups; I compared REAT::theil with function what i coded, the results are almost same.
theil1[i] <- theil_index(group1[,2])
theil2[i] <- theil_index(group2[,2])
theil3[i] <- theil_index(group3[,2])
theil4[i] <- theil_index(group4[,2])
# gini for specific groups
gini.group1[i] <- gini4use(y1)
gini.group2[i] <- gini4use(y2)
gini.group3[i] <- gini4use(y3)
gini.group4[i] <- gini4use(y4)
}
# suits among groups rebate vs. income
ggplot()+
geom_line(aes(x=seq(2010,2018,1),y=gini.group1,color="DAC",linetype ="DAC") )+
geom_line(aes(x=seq(2010,2018,1),y=gini.group2,color="Low-Income",linetype ="Low-Income"))+
geom_line(aes(x=seq(2010,2018,1),y=gini.group3,color="Partial Buffered",linetype ="Partial Buffered"))+
geom_line(aes(x=seq(2010,2018,1),y=gini.group4,color="Wholy Buffered",linetype ="Wholy Buffered"))+
geom_point(aes(x=seq(2010,2018,1),y=gini.group1,color="DAC",shape ="DAC") )+
geom_point(aes(x=seq(2010,2018,1),y=gini.group2,color="Low-Income",shape ="Low-Income"))+
geom_point(aes(x=seq(2010,2018,1),y=gini.group3,color="Partial Buffered",shape ="Partial Buffered"))+
geom_point(aes(x=seq(2010,2018,1),y=gini.group4,color="Wholy Buffered",shape ="Wholy Buffered"))+
# scale_color_manual(values = brewer.pal(n = 4, name = "Dark2"))+
scale_color_futurama() +
xlab("year")+
ylab("Suits Index for specific groups")+
ggtitle("Suits Index in low-income and disadvantaged communities by years")+
labs(color = NULL,linetype=NULL,shape = NULL)+
theme_bw()+
scale_x_continuous(breaks=seq(2010, 2018, 1))+
theme(legend.position="bottom")+ ylim(0,1)+
ggsave("Figures/adoptionev.pdf",width = 8.7, dpi = 300)
ggplot()+
geom_line(aes(x=seq(2010,2018,1),y=gini_num ,color="Gini",linetype="Gini"))+
geom_line(aes(x=seq(2010,2018,1),y=suits_income,color="Suits-income",linetype = "Suits-income" ))+
geom_line(aes(x=seq(2010,2018,1),y=suits_dac,color="Suits-DAC", linetype = "Suits-DAC"))+
geom_point(aes(x=seq(2010,2018,1),y=gini_num ,color="Gini", shape = "Gini"))+
geom_point(aes(x=seq(2010,2018,1),y=suits_income,color="Suits-income",shape = "Suits-income"))+
geom_point(aes(x=seq(2010,2018,1),y=suits_dac,color="Suits-DAC",shape = "Suits-DAC"))+
scale_color_futurama() +
xlab("year")+
ylab("Index")+scale_x_continuous(breaks=seq(2010, 2018, 1))+
ggtitle("Gini, Suits:income and Suits:DAC by year")+
labs(color = NULL,linetype=NULL,shape = NULL)+
ylim(0,1)+
theme_bw()+
theme(legend.position="bottom")+
ggsave("Figures/index-compare.pdf",width = 8.7/2, height = 8.7/3 ,dpi = 300)
# suits among groups rebate vs. income
ggplot()+
geom_line(aes(x=seq(2010,2018,1),y=gini.group1,color="DAC",linetype ="DAC") )+
geom_line(aes(x=seq(2010,2018,1),y=gini.group2,color="Low-Income",linetype ="Low-Income"))+
geom_line(aes(x=seq(2010,2018,1),y=gini.group3,color="Partial Buffered",linetype ="Partial Buffered"))+
geom_line(aes(x=seq(2010,2018,1),y=gini.group4,color="Wholy Buffered",linetype ="Wholy Buffered"))+
geom_point(aes(x=seq(2010,2018,1),y=gini.group1,color="DAC",shape ="DAC") )+
geom_point(aes(x=seq(2010,2018,1),y=gini.group2,color="Low-Income",shape ="Low-Income"))+
geom_point(aes(x=seq(2010,2018,1),y=gini.group3,color="Partial Buffered",shape ="Partial Buffered"))+
geom_point(aes(x=seq(2010,2018,1),y=gini.group4,color="Wholy Buffered",shape ="Wholy Buffered"))+
# scale_color_manual(values = brewer.pal(n = 4, name = "Dark2"))+
scale_color_futurama() +
xlab("year")+
ylab("Suits Index for specific groups")+
ggtitle("Suits Index in low-income and disadvantaged communities by years")+
labs(color = NULL,linetype=NULL,shape = NULL)+
theme_bw()+
scale_x_continuous(breaks=seq(2010, 2018, 1))+
theme(legend.position="bottom")+ ylim(0,1)+
ggsave("Figures/suits_among_groups.pdf",width = 8.7/2, height = 8.7/3 ,dpi = 300)
summarytotalrebate <- read_csv("Data/summarytotalrebate.csv")
summarytotalrebate$income_group <- factor(summarytotalrebate$income_group,levels = c("low_income","moderate_low","moderate_high","high_income"))
ggplot(summarytotalrebate,aes(x=year,y=total_rebate,group=income_group,fill=income_group))+
geom_bar(stat="identity", position=position_dodge(),alpha=0.75)+
# scale_fill_manual(values = brewer.pal(n = 4, name = "Set1"))+
ggtitle("Total rebate comparison between income groups by year")+
scale_x_continuous(breaks=seq(2011, 2018, 1))+
ylab("Total rebate ($)")+
xlab("Year")+
theme_bw()+
scale_fill_d3()+
labs(fill="Income group")+
theme(legend.position="bottom")+
ggsave("Figures/total_rebate.pdf",width = 8.7, height = 8.7/2.5, dpi = 300)
summarytotalrebate <- read_csv("Data/summarytotalrebate.csv")
summarytotalrebate$income_group <- factor(summarytotalrebate$income_group,levels = c("low_income","moderate_low","moderate_high","high_income"))
ggplot(summarytotalrebate,aes(x=year,y=total_rebate,group=income_group,fill=income_group))+
geom_bar(stat="identity", position=position_dodge(),alpha=0.75)+
# scale_fill_manual(values = brewer.pal(n = 4, name = "Set1"))+
ggtitle("Total rebate comparison between income groups by year")+
scale_x_continuous(breaks=seq(2011, 2018, 1))+
ylab("Total rebate ($)")+
xlab("Year")+
theme_bw()+
scale_fill_d3()+
labs(fill="Income group")+
theme(legend.position="bottom")+
ggsave("Figures/total_rebate.pdf",width = 8.7, height = 8.7/2.5, dpi = 300)
lisaplotting <- read_csv("Data/lisaplotting.csv")
lisaplotting %>%ggplot(aes(x= MORAN_STD,y=MORAN_LAG))+
geom_point(alpha = 0.3)+
geom_abline(intercept = 0, slope = 0.618,color = "red", linetype = "dashed",size = 1)+
scale_color_futurama() +
xlab("std of the variable")+
ylab("lag of the variable")+
ggtitle("Moran'I scatter plot")+
geom_hline(yintercept = 0)+
geom_vline(xintercept = 0)+
geom_text(x=10, y=10, label="I",size=4)+
geom_text(x=10, y=-10, label="IV",size=4)+
geom_text(x=-10, y=10, label="II",size=4)+
geom_text(x=-10, y=-10, label="III",size=4)+
coord_fixed()+
xlim(-10,10)+
ylim(-10,10)+
theme_bw()+
ggsave("Figures/moranscatterplot.pdf",width = 8.7/2,height = 8.7/2, dpi = 300)