-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTane-P5.Rmd
410 lines (338 loc) · 13.2 KB
/
Tane-P5.Rmd
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
---
title: "P5"
author: "Tane Koh"
date: "2024-04-01"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(readr)
library(lubridate)
library(maps)
library(ggmap)
library(tmap)
library(tmaptools)
register_stadiamaps("8d3c6033-7875-4644-b843-3156014c7e0a", write = TRUE)
```
```{r}
data <- read_csv("camdens big dataset.csv")
cities <- read_csv("MA cities.csv")
```
```{r}
data <- data %>%
rename_at('Date In Service', ~'Date') %>%
rename_at('Total Cost with Design Fees', ~'Cost') %>%
rename_at('Total Grant', ~'Grant')
data$Date <- as.Date(data$Date, format = "%m/%d/%Y")
data$Cost <- as.numeric(gsub("[^0-9.]", "", data$Cost))
data$Grant <- as.numeric(gsub("[^0-9.]", "", data$Grant))
cities$name <- str_replace(cities$name, " town$", "")
cities$name <- str_replace(cities$name, " Town$", "")
data <- left_join(data,cities, by = c("City" = "name"))
```
```{r}
city_counts <- data %>%
group_by(City) %>%
summarise(count = n()) %>%
filter(count > 200)
merged_data <- left_join(city_counts, cities, by = c("City"="name"))
merged_data$ratio <- merged_data$count / merged_data$pop2024
map_MA = get_stadiamap(
bbox = c(left = -73.1387, bottom = 41.4696, right = -69.8813, top = 42.8599),
maptype = "stamen_toner_lite",
zoom = 9
)
ggmap(map_MA)+
geom_point(
data = merged_data,
aes(x = lng, y = lat),
alpha = .80,
size = merged_data$ratio*10
)+
scale_color_gradient(low = "yellow", high = "red")+
labs(title = "Solar Installations in MA between 2000 and 2024 per capita.",caption="Restricted to cities with over 200 installations.")
```
```{r}
just_residential <- data %>%
filter(`Facility Type` == "Commercial / Office")
city_counts <- data %>%
group_by(City) %>%
summarise(count = n())
merged_data <- left_join(city_counts, cities, by = c("City"="name"))
merged_data$ratio <- merged_data$count / merged_data$pop2024
map_MA = get_stadiamap(
bbox = c(left = -73.1387, bottom = 41.4696, right = -69.8813, top = 42.8599),
maptype = "stamen_toner_lite",
zoom = 9
)
ggmap(map_MA)+
geom_point(
data = merged_data,
aes(x = lng, y = lat),
alpha = .80,
size = merged_data$count/500
)+
scale_color_gradient(low = "yellow", high = "red")+
labs(title = "All Solar Installations in MA between 2000 and 2016.",caption="Not per capita.")
```
```{r}
cost <- data %>%
filter(`Facility Type` %in% c("Multi-family residential (4 or more dwelling units per building)", "Residential (3 or fewer dwelling units per building)")) %>%
filter(Cost > 1) %>%
filter(!is.na(`Capacity (DC, kW)`)) %>%
filter(!is.na(`Date`)) %>%
filter(!is.na(`Grant`)) %>%
filter(!is.na(`Cost`)) %>%
mutate(Cost_per_kW = `Cost` / `Capacity (DC, kW)`) %>%
mutate(Grant_per_kW = `Grant` / `Capacity (DC, kW)`)
average_cost_per_year <- cost %>%
group_by(Year = format(Date, "%Y")) %>%
summarise(average_Cost_per_kW = mean(Cost / `Capacity (DC, kW)`))
average_cost_per_year2 <- cost %>%
group_by(Year = format(Date, "%Y")) %>%
summarise(mean_cost = mean(Cost / `Estimated Annual Production (kWhr)`))
average_capacity <- cost %>%
group_by(Year = format(Date, "%Y")) %>%
summarise(capacity = mean(`Capacity (DC, kW)`))
average_cost_per_city <- cost %>%
group_by(City) %>%
summarise(average_Cost_per_kW = mean(Cost / `Capacity (DC, kW)`))
average_cost_per_city <- left_join(average_cost_per_city, cities, by = c("City"="name"))
ggplot(average_cost_per_year2,aes(x=Year,y=mean_cost,group=1))+
geom_line()+
geom_point()+
labs(title="Yearly Average Cost Per kWhr of Annual Energy Production",y="Average Cost Per kWhr of Annual Energy Production")
ggplot(average_cost_per_year,aes(x=Year,y=average_Cost_per_kW,group=1))+
geom_line()+
geom_point()+
labs(title="Yearly Average Cost Per KW",y="Average Cost Per kW")
ggplot(average_capacity,aes(x=Year,y=capacity,group=1))+
geom_line()+
geom_point()+
labs(title="Trends in Residential Solar Installations Over TIme",y="Average capcity of solar panels (kW)")
ggplot(cost,aes(x=Cost_per_kW))+
geom_density()+
xlim(0,10000)+
labs(title = "Distribution of Cost per kW",x = "Cost per kW ($)")
map_MA = get_stadiamap(
bbox = c(left = -73.1387, bottom = 41.4696, right = -69.8813, top = 42.8599),
maptype = "stamen_toner_lite",
zoom = 9
)
ggmap(map_MA)+
geom_point(
data = average_cost_per_city,
aes(x = lng, y = lat),
alpha = .80,
size = average_cost_per_city$average_Cost_per_kW/2000
)+
scale_color_gradient(low = "yellow", high = "red")+
labs(title = "Average Cost per kW of Solar Installations Across MA",caption="")
```
```{r}
Grant_per <- cost %>%
group_by(City) %>%
summarise(average_grant_per_kW = mean(Grant / `Capacity (DC, kW)`))
Grant_per <- left_join(Grant_per, cities, by = c("City"="name"))
Grants <- cost %>%
group_by(City) %>%
summarise(average_grant = mean(Grant))
Grants <- left_join(Grants, cities, by = c("City"="name"))
ggmap(map_MA)+
geom_point(
data = Grant_per,
aes(x = lng, y = lat),
alpha = .80,
size = Grant_per$average_grant_per_kW/500
)+
scale_color_gradient(low = "yellow", high = "red")+
labs(title = "Average Grant per kW of Capacity Across MA",caption="")
ggmap(map_MA)+
geom_point(
data = Grants,
aes(x = lng, y = lat),
alpha = .80,
size = Grants$average_grant/100000
)+
scale_color_gradient(low = "yellow", high = "red")+
labs(title = "Average Grant per kW of Capacity Across MA",caption="")
ggplot(cost,aes(x=Grant))+
geom_density()+
xlim(0,10000)+
labs(title = "Distribution of Value of Grants",x = "Value of Grant ($)")
ggplot(cost,aes(x=Grant_per_kW))+
geom_density()+
xlim(0,5000)+
labs(title = "Distribution of Value of Grants per kW",x = "Value of Grant per kW ($)")
average_grant_per_kW_per_year <- cost %>%
group_by(Year = format(Date, "%Y")) %>%
summarise(average_grant_per_kW = mean(Grant / `Capacity (DC, kW)`))
average_grant_year <- cost %>%
group_by(Year = format(Date, "%Y")) %>%
summarise(grant = mean(`Grant`))
total_grant_year <- cost %>%
group_by(Year = format(Date, "%Y")) %>%
summarise(grant = sum(`Grant`))
total_grant_city <- cost %>%
filter(`Facility Type` == "Residential (3 or fewer dwelling units per building)") %>%
group_by(City) %>%
summarise(grant = sum(`Grant`))
total_grant_city <- left_join(total_grant_city, cities, by = c("City"="name"))
ggplot(average_grant_per_kW_per_year,aes(x=Year,y=average_grant_per_kW,group=1))+
geom_line()+
geom_point()+
labs(title="Yearly Average Grant Per kW",y="Average Grant Per kW ($)")
ggplot(average_grant_year,aes(x=Year,y=grant,group=1))+
geom_line()+
geom_point()+
ylim(0,75000)+
labs(title="Yearly Average Grant",y="Average Grant ($)")
ggplot(total_grant_year,aes(x=Year,y=grant/1000000,group=1))+
geom_line()+
geom_point()+
ylim(0,40)+
labs(title="Total Grants Awarded By Year",y="Total Value of Grants Awarded ($M)")
ggmap(map_MA)+
geom_point(
data = total_grant_city,
aes(x = lng, y = lat),
alpha = .80,
size = total_grant_city$grant/(100*total_grant_city$pop2024)
)+
scale_color_gradient(low = "yellow", high = "red")+
labs(title = "Total Residential Grants Awarded by City per capita",caption="")
```
```{r}
lm_model <- lm(Grant ~ `Estimated Annual Production (kWhr)`, data = data)
# Create a scatter plot with regression line
ggplot(data, aes(y = Grant, x = `Estimated Annual Production (kWhr)`)) +
geom_point() + # Add points
geom_smooth(method = "lm", se = FALSE, color = "blue") + # Add linear regression line
xlim(0,750000)+
labs(x = "Estimated Annual Production (kWhr)", y = "Grant", title = "Linear Model: Estimated Annual Production vs. Grant") +
theme_minimal()
lm_model2 <- lm(`Grant` ~ pop2024, data = data)
# Create a scatter plot with regression line
ggplot(cost, aes(x = pop2024, y = `Grant`)) +
geom_point() + # Add points
geom_smooth(method = "lm", se = FALSE, color = "blue") + # Add linear regression line
ylim(0,750000)+
labs(x = "Population", y = "Grant", title = "Linear Model: Estimated Annual Production vs. Grant") +
theme_minimal()
lm_model3 <- lm(`Grant` ~ pop2024 + `Estimated Annual Production (kWhr)`, data = cost)
intercept <- coef(lm_model3)[1]
slope_production <- coef(lm_model3)[2]
slope_pop2024 <- coef(lm_model3)[3]
# Create the scatter plot with the linear regression line
ggplot(cost, aes(x = `Estimated Annual Production (kWhr)`, y = Grant)) +
geom_point() + # Add points
geom_smooth(method = "lm", se = FALSE, color = "blue", formula = y ~ x + pop2024) + # Add linear regression line
labs(x = "Estimated Annual Production", y = "Grant", title = "Linear Model: Grant ~ Estimated Annual Production + pop2024") +
theme_minimal() +
xlim(0,700000)+
annotate("text", x = max(data$`Estimated Annual Production (kWhr)`), y = max(data$Grant),
label = paste("Grant =", round(intercept, 2), "+", round(slope_production, 2), "* Production +", round(slope_pop2024, 2), "* pop2024"))
```
```{r}
# make visualization of newer implementations via color
City_color <- cost %>%
mutate(Year = lubridate::year(Date)) %>%
group_by(City) %>%
summarise(avg_year = mean(Year))
City_color <- left_join(City_color, cities, by = c("City"="name"))
ggmap(map_MA) +
geom_point(
data = City_color,
aes(x = lng, y = lat, color = avg_year),
alpha = 0.80,
size = 3
) +
scale_color_gradient(
low = "white",
high = "blue",
limits = range(City_color$avg_year),
breaks = pretty(range(City_color$avg_year), n = 5)
) +
labs(
title = "Average Year of Solar Project Installation",
caption = ""
) +
theme(legend.position = "bottom") +
guides(color = guide_colorbar(title = "Average Year"))
# visualization of price rate change specific to commercial/residential facet plot?
cost2 <- cost %>%
mutate(`Facility Type` = case_when(
`Facility Type` %in% c("Multi-family residential (4 or more dwelling units per building)",
"Residential (3 or fewer dwelling units per building)") ~ "Residential",
`Facility Type` %in% c("Industrial", "Commercial / Office", "Restaurant / Food Service", "Retail") ~ "Industrial",
`Facility Type` %in% c("College / University", "Community Solar", "Federal",
"Hospital / Health Care", "Municipal - K-12 School",
"Municipal / Government / Public", "School (K-12)", "State",
"State - College/University") ~ "Government",
`Facility Type` %in% c("Other", "Agricultural", "Religious", "Mixed use (commercial & residential)") ~ "Other"
))
facet_data <- cost2 %>%
mutate(Year = lubridate::year(Date)) %>%
group_by(`Facility Type`, Year) %>%
summarise(average_cost = mean(Cost / `Estimated Annual Production (kWhr)`))
ggplot(facet_data, aes(x = Year, y = average_cost, color = `Facility Type`)) +
geom_line() +
labs(x = "Year", y = "Average Cost per Annual Production", title = "Cost per Annual Production by Facility Type") +
theme_minimal()+
ylim(0,12)
facet_grant <- cost2 %>%
mutate(Year = lubridate::year(Date)) %>%
group_by(`Facility Type`, Year) %>%
summarise(avg_grant = mean(Grant / `Estimated Annual Production (kWhr)`))
ggplot(facet_grant, aes(x = Year, y = avg_grant, color = `Facility Type`)) +
geom_line() +
labs(x = "Year", y = "Average Grant per Annual Production", title = "Grant per Annual Production by Facility Type") +
theme_minimal()+
ylim(0,7)
## Grants awarded by city per capita but limited to residential
```
```{r}
cost$Year <- format(cost$Date, "%Y")
# Aggregate data by year to calculate average Capacity and Production
aggregate_cost <- cost %>%
group_by(Year) %>%
summarise(avg_capacity = mean(`Capacity (DC, kW)`),
avg_production = mean(`Estimated Annual Production (kWhr)`))
ggplot(aggregate_cost, aes(x = Year)) +
geom_line(aes(y = avg_capacity*1000, color = "Capacity"), group = 1) +
geom_line(aes(y = avg_production, color = "Production"), group = 1) +
labs(title = "Capacity and Production of Residential Solar Installations Over Time",
x = "Date",
y = "Average Estimated Annual Production (kWhr)",
color = "Variable") +
scale_color_manual(values = c("Capacity" = "blue", "Production" = "red")) +
theme_minimal() +
# Adding a secondary y-axis for Production
scale_y_continuous(
sec.axis = sec_axis(~./1000, name = "Average Capacity (kW)")
)
```
```{r}
aggr_money <- cost %>%
group_by(Year) %>%
summarise(cost = mean(Cost/`Estimated Annual Production (kWhr)`),
grant = mean(Grant/`Estimated Annual Production (kWhr)`))
ggplot(aggr_money, aes(x = Year)) +
geom_line(aes(y = cost, color = "Average Cost of Installation"), group = 1) +
geom_line(aes(y = grant*2, color = "Average Awarded Grant Value"), group = 1) +
labs(title = "Cost of Residential Solar Installations and Grant Values per kWhr over time",
x = "Date",
y = "Cost ($/kWhr)",
color = "Variable") +
scale_color_manual(values = c("Average Cost of Installation" = "blue", "Average Awarded Grant Value" = "red")) +
# Adding a secondary y-axis for Production
scale_y_continuous(
sec.axis = sec_axis(~./2, name = "Grant ($/kWhr)")
)
```
```{r}
# average per year amount grant vs number of grants
grant_per <- cost %>%
```
# number per funding type faceted by year