-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtahMunicipalIndustrialUse_InitAnalysis.R
403 lines (298 loc) · 15.7 KB
/
UtahMunicipalIndustrialUse_InitAnalysis.R
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
# UtahMunicipalIndustiralUse_InitAnalysis.r
#
# Initial analysis of Utah's Municipal and Industrial Water Use Data
# 2018-2015
#
# Downloaded from https://dwre-utahdnr.opendata.arcgis.com/pages/municipal-and-industrial-data
#
# David E. Rosenberg
# November 28, 2020
# Updated November 12, 2021
# Utah State University
rm(list = ls()) #Clear history
# Load required libraies
if (!require(tidyverse)) {
install.packages("tidyverse", repos="http://cran.r-project.org")
library(tidyverse)
}
if (!require(readxl)) {
install.packages("readxl", repos="http://cran.r-project.org")
library(readxl)
}
if (!require(RColorBrewer)) {
install.packages("RColorBrewer",repos="http://cran.r-project.org")
library(RColorBrewer) #
}
if (!require(dplyr)) {
install.packages("dplyr",repos="http://cran.r-project.org")
library(dplyr) #
}
if (!require(expss)) {
install.packages("expss",repos="http://cran.r-project.org")
library(expss) #
}
if (!require(reshape)) {
install.packages("reshape", repos="http://cran.r-project.org")
library(reshape)
}
if (!require(reshape2)) {
install.packages("reshape2", repos="http://cran.r-project.org")
library(reshape2)
}
if (!require(pracma)) {
install.packages("pracma", repos="http://cran.r-project.org")
library(pracma)
}
if (!require(lubridate)) {
install.packages("lubridate", repos="http://cran.r-project.org")
library(lubridate)
}
if (!require(directlabels)) {
install.packages("directlabels", repo="http://cran.r-project.org")
library(directlabels)
}
#if (!require(plyr)) {
# install.packages("plyr", repo="http://cran.r-project.org")
# library(plyr)
#}
if (!require(ggrepel)) {
devtools::install_github("slowkow/ggrepel")
library(ggrepel)
}
library(dygraphs)
library(xts) # To make the convertion data-frame / xts format
library(tidyverse)
library(lubridate)
### 0. Definitions
sFileBase <- "_Municipal_and_Industrial_Water_Use_Databases.csv"
#Define the River locations to use
dfFileYears <- data.frame(year = seq(2015,2018, by=1))
dfFileYears$filename <- paste0(dfFileYears$year,sFileBase,"")
### 1. Read IN the data files. Rbind so long list
for(i in 1:nrow(dfFileYears)){
# i <- 1
print(dfFileYears$year[i])
# Read in the historical Powell data
dfTempFile <- read.csv(file=dfFileYears$filename[i],
header=TRUE,
stringsAsFactors=FALSE,
sep=",")
dfTempFile$year <- dfFileYears$year[i] #factor(dfLocations$rivermile[i], levels = dfLocations$rivermile)
#Rename the first column because they differ amoung files
cColNames <- colnames(dfTempFile)
cColNames[1] <- "FID"
colnames(dfTempFile) <- cColNames
if(dfTempFile$year[i] %in% c(2018)) { #We need to adjust some other columns
cColNamesTemp <- cColNames
cColNamesTemp[c(12, 25,26,27,28)] <- c("TotPotaGPCD", "TotalSup", "WellsSup","SpringsSup","SurfaceSup")
colnames(dfTempFile) <- cColNamesTemp
#Add blank columns for these fields
cColsAdd <- c("ComSecGPCD", "IndSecGPCD", "InsSecGPCD", "InsSecoUse", "ResSecGPCD", "ResSecoUse", "ComSecoUse", "IndSecoUse", "IndSecoUse")
for (i in (1:length(cColsAdd))) {
dfTempFile[[cColsAdd[i]]] = 0
}
}
if(dfTempFile$year[i] %in% c(2019)) { #We need to adjust some other columns
cColNamesTemp <- cColNames
cColNamesTemp[c(12, 25,26,27,28)] <- c("TotPotaGPCD", "TotalSup", "WellsSup","SpringsSup","SurfaceSup")
colnames(dfTempFile) <- cColNamesTemp
#Add blank columns for these fields
cColsAdd <- c("ComSecGPCD", "IndSecGPCD", "InsSecGPCD", "InsSecoUse", "ResSecGPCD", "ResSecoUse", "ComSecoUse", "IndSecoUse", "IndSecoUse")
for (i in (1:length(cColsAdd))) {
dfTempFile[[cColsAdd[i]]] = 0
}
}
if(i==1){ #first year, creat new data frame
dfAllYears <- dfTempFile
} else { #subsequent year, bind to existing records
#bind the latest record to the existing records
dfAllYears <- rbind(dfAllYears,dfTempFile)
}
}
#Calculate population from per capita daily use and annual volume
dGalPerAcreFeet <- 325851
dfAllYears$Population <- dfAllYears$TotalUse * dGalPerAcreFeet / (dfAllYears$TotalGPCD * 365)
#Calculate average percapita use statewide
dfAvgGPCD <- dfAllYears %>% group_by(year) %>% filter(is.na(Population) == FALSE, is.infinite(Population) == FALSE) %>% summarize(TotalUse = sum(TotalUse),
Population=sum(Population),
AvgGPCD = sum(TotalUse)*dGalPerAcreFeet/(365*sum(Population)),
UsePerPersonPerYear = sum(TotalUse)/sum(Population))
#Grab color palettes
palBlues <- brewer.pal(9, "Blues")
palReds <- brewer.pal(9, "Reds")
palBlueFunc <- colorRampPalette(c(palBlues[3],palBlues[9]))
###### 2. Plot Total Use vs. Population. Sloped line for avg gpcd
#Plot hourly - too much data
library(scales)
ggplot(dfAllYears %>% filter(year == 2018), aes(x = Population/100000, y= TotalUse/10000, color=BASIN)) +
geom_point(size=2) +
#Sloped line for statewide average per capacita per day
geom_abline(aes(intercept = 0, slope = as.numeric(10*dfAvgGPCD %>% filter(year == 2018) %>% select(UsePerPersonPerYear))), color="black", size = 2, show.legend = NA) +
#scale_colour_manual(name="Lines", labels=c(paste(round(dfAvgGPCD %>% filter(year==2015) %>% select(AvgGPCD)), "gpcd")), values=c("Black")) +
facet_wrap(~BASIN) +
#scale_color_manual(values = palBlues[3:8], breaks = c(194,149,76,61,30,0)) + #c(0,30,61,76,149,194)) +
#scale_x_continuous(labels = numeric() ) +
labs(x="Population (100,000)", y="Total Use (10,000 acre-feet per year)", color = "County") +
theme(text = element_text(size=20), legend.title=element_blank(), legend.text=element_text(size=18),
legend.key = element_blank())
ggsave("UseVsPopulation.png", width=9, height = 6.5, units="in")
#Histogram of per capita daily use by provider
ggplot(dfAllYears, aes(x=TotalGPCD, color = BASIN, fill = BASIN)) +
geom_histogram(binwidth = 50) +
#Plot vertial line for average gpcd
geom_vline(xintercept = dfAvgGPCD$AvgGPCD, color="black", size = 2) +
geom_text(data=dfAvgGPCD %>% filter(year==2015), aes(x=AvgGPCD+30,y=25), label = "Utah average", size=6, hjust=0) +
scale_x_continuous(limits = c(0,3000)) +
facet_wrap(~year) +
labs(x="Total Gallons per Person per Day", y="Number of Providers") +
theme(text = element_text(size=20), legend.title=element_blank(), legend.text=element_text(size=18),
legend.key = element_blank())
#Histogram of daily per caita water use by water volume
#Create the bins
cGPCDBins <- seq(0,max(dfAllYears$TotalGPCD), by=50)
#Assign each GPCD value to a bin
dfAllYears$TotGPCDBin <- cut(dfAllYears$TotalGPCD, breaks = cGPCDBins, labels = cGPCDBins[1:(length(cGPCDBins)-1)])
#Sum in each group
dfGCPDByVolume <- dfAllYears %>% group_by(TotGPCDBin, year) %>% summarize(TotalUse = sum(TotalUse))
ggplot(dfGCPDByVolume) +
geom_bar(aes(x= cGPCDBins[as.numeric(TotGPCDBin)], y=TotalUse/10000), fill="blue", color="blue", stat="identity") +
#Plot vertial line for average gpcd
geom_vline(xintercept = dfAvgGPCD$AvgGPCD, color="black", size = 2) +
geom_text(data=dfAvgGPCD %>% filter(year==2015), aes(x=AvgGPCD+30,y=25), label = "Utah average", size=6, hjust=0) +
scale_x_continuous(limits=c(0,3000)) +
facet_wrap(~year) +
labs(x="Total Gallons per Person per Day", y="Total Use\n(10,000 acre-feet per year)") +
theme(text = element_text(size=20), legend.title=element_blank(), legend.text=element_text(size=18),
legend.key = element_blank())
#Cumulative sum
ggplot(dfGCPDByVolume) +
geom_line(aes(x= cGPCDBins[as.numeric(TotGPCDBin)], y=cumsum(TotalUse)/10000, color=as.factor(year)), size =1.5) +
#Plot vertial line for average gpcd
geom_vline(xintercept = dfAvgGPCD$AvgGPCD, color="black", size = 2) +
#Label the vertical line
geom_text(data=dfAvgGPCD %>% filter(year==2015), aes(x=AvgGPCD+20,y=50), label = "Utah average", size=8, hjust=0) +
scale_x_continuous(limits=c(0,3000)) +
scale_y_continuous(sec.axis = sec_axis(~ . / (max(cumsum(dfAvgGPCD$TotalUse))/100)))+ #, name="Cumulative Use\n(Percent of Total)") +
#facet_wrap(~year) +
labs(x="Total Gallons per Person per Day", y="Total Use\n(10,000 acre-feet per year)") +
theme(text = element_text(size=20), legend.title=element_blank(), legend.text=element_text(size=18),
legend.key = element_blank())
#Bar graph of providers ranked by Total GPCD
#Colors to split into zones. The zones are Salt Lake City's use of 191 gpcd, 2x that, and above
#Highlight listed cities as black bars
#order the data frame by the TotalGPCD field
dfAllYearsSort <- dfAllYears %>% filter(year == 2018) %>% arrange(TotalGPCD)
dfAllYearsSort$Row <- seq(1,nrow(dfAllYearsSort), by=1)
#Cities to highlight
#These cities have a range of Total GPCD, Total GPCD is often similar to Potable GPCD, and people will recognize these providers
#Read in from Excel
dfHighlightCities <- read_excel("HighlightCities.xlsx","HighlightCities","A1:B23")
# The Salt Lake City gallons per person per day reported
nAvgGPCD <- 191 #240 = state average
sAvgText <- "Salt Lake City"
# Define the state conservation goal and calculate the equivalent GPCD that
# will reach the nAvgGPCD vaue
nStateConserveGoal <- 0.2
#Calculate the GPCD that will meet the SLC target
nGPCDToMeetTarget <- -nAvgGPCD/(nStateConserveGoal - 1)
print(paste(sprintf("%0.f",nGPCDToMeetTarget), "gpcd can reduce by state conservation goal of", sprintf("%.0f%%", nStateConserveGoal*100), "and achieve SLC use"))
# Break at 0, the average, the GPCD to reduce to meet the average, and max
cGPCDBreaks <- c(0, nAvgGPCD, nGPCDToMeetTarget, max(dfAllYearsSort$TotalGPCD))
#Count water providers within breaks
dfCounts <- hist(dfAllYearsSort$TotalGPCD, breaks = cGPCDBreaks, plot=FALSE) #, breaks = c(0,100,200,300,400,500, 1000, 5000))
dfCounts$CumSum <- nrow(dfAllYearsSort) - cumsum(dfCounts$counts)
#Assign each water provider to a break
dfAllYearsSort$TotGPCDBin <- cut(dfAllYearsSort$TotalGPCD, breaks = cGPCDBreaks, labels = cGPCDBreaks[1:(length(cGPCDBreaks)-1)])
#Sum in each group
dfGPCDByVolume <- dfAllYearsSort %>% group_by(TotGPCDBin, year) %>% summarize(TotalUse = sum(TotalUse))
#Add an additional group that is the Water providers to highlight
dfAllYearsSort$TotGPCDBin <- ifelse(dfAllYearsSort$WRENAME %in% dfHighlightCities$FullName,"Highlight", dfAllYearsSort$TotGPCDBin)
#Joint the Short cities names
dfAllYearsSort <- left_join(dfAllYearsSort, dfHighlightCities, by = c("WRENAME" = "FullName"))
#Convert NAs to ""
dfAllYearsSort$ShortName <- ifelse(is.na(dfAllYearsSort$ShortName),"",dfAllYearsSort$ShortName)
#Add the volume data to the prior counts data
dfCounts$Volume <- dfGPCDByVolume$TotalUse
dfCounts$PercentVolume <- dfCounts$Volume/sum(dfCounts$Volume)
dfCounts$TextFull <- paste("These ",dfCounts$counts, "providers use\n", sprintf("%1.0f%%", dfCounts$PercentVolume*100), "of the statewide total")
dfCounts$Text <- paste(dfCounts$counts, "\nProviders")
dfCounts$TextFull <- paste(dfCounts$counts, "\nProviders\n", "(",sprintf("%1.0f%%", dfCounts$PercentVolume*100), "state volume)")
#Nullify the last text entry
dfCounts$TextFull <- dfCounts$TextFull[1:(length(dfCounts$TextFull)-1)]
dfCounts$Text <- paste(dfCounts$counts, "\nProviders")
#Build a data frame to plot labels for the groups
dfLabels <- data.frame(Rank = dfCounts$CumSum,
Text = dfCounts$Text,
TextFull = dfCounts$TextFull,
Counts = dfCounts$counts,
FontColor = as.factor(c(1,2,3)))
#Calculate the Mid rank as Cumulative + 1/2 current counts
dfLabels$MidRank <- dfLabels$Rank + dfLabels$Counts/2
dfLabels$YPos <- 1250
dfAllYearsSort$Blank <- ""
#Build a data frame to label the lines
dfLineLabels <- data.frame(Rank = dfLabels$Rank[1:2],
GPCD = cGPCDBreaks[2:3],
#Label = c("Salt Lake City ~ 191 gpcd", "2x Salt Lake City ~ 382 gpcd"))
Label = c(paste(sAvgText, "~ ", nAvgGPCD, "gpcd"), paste("120%", sAvgText, "~", sprintf("%.0f", nGPCDToMeetTarget), "gpcd")))
sBlockLabelColor <- "red"
sBlockSepColor <- "black"
sHighlightColor <- "red"
#Flip the Short Names upside down to plot labels correctly
dfAllYearsSortFlip <- dfAllYearsSort %>% map_df(rev)
ggplot(dfAllYearsSort ) +
#Bar graph of Total gallons per person per day reversed ranked (largest at left)
geom_bar(aes(x= reorder(WRENAME, -TotalGPCD), y=TotalGPCD, fill = TotGPCDBin, color = TotGPCDBin), stat="identity") +
#Plot vertical line to separate blocks
geom_vline(xintercept = dfCounts$CumSum, color=sBlockSepColor, size = 1, linetype = "dashed") +
#Label the number of providers in each block
geom_text(data = dfLabels, aes(x = MidRank, y = YPos, label = Text, color = FontColor), size = 7) +
#Label the line breaks
geom_text(data = dfLineLabels, aes(x = Rank - 5, y = 2000, label = Label), size = 6, angle = 90, color = sBlockSepColor) +
#Plot some points to see where they are
#geom_point(data = data.frame(x = c(0,1000), y = c(0,1000)), aes(x=x,y=y), size= 10) +
#geom_text(data=dfAvgGPCD %>% filter(year==2015), aes(x=AvgGPCD+30,y=25), label = "Utah average", size=6, hjust=0) +
#Define scales
#Flip the x labels upside down
scale_x_discrete(labels = dfAllYearsSortFlip$ShortName) +
scale_color_manual(values = c(palBlues[4], palBlues[6], palBlues[8], sHighlightColor)) +
scale_fill_manual(values = c(palBlues[4], palBlues[6], palBlues[8], sHighlightColor)) +
scale_y_continuous(breaks = seq(0,4500, by=500)) +
#Turn the Fill guide off
guides(fill = "none", color = "none") +
labs(x="Utah water provider (Ranked)", y="Gallons per person per day") +
theme(text = element_text(size=20), legend.title=element_blank(), legend.text=element_text(size=18),
#Remove the minor x grid lines
panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.y = element_blank(),
legend.key = element_blank(),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1, color = sHighlightColor))
#Prepare a data frame to download to excel that shows:
# 1) Highlight city
# 2) Total GPCD
# 3) Potable GPCD
# 4) Percent reducation to get to Salt Lake City value
#Filter and select to get the data we want
dfReductions <- dfAllYearsSort %>% filter(ShortName != "") %>% select(ShortName, TotalGPCD, TotPotaGPCD) %>% arrange(-TotalGPCD)
# Calculate % reduction
dfReductions$PercentReduce <- (dfReductions$TotalGPCD - nAvgGPCD)/dfReductions$TotalGPCD
dfReductions$TargetCanMeet <- ifelse(dfReductions$PercentReduce > nStateConserveGoal, "No", "Yes")
# State whether sufficient to meet conservation goal of 20%
#Write the sorted dataframe to csv
write.csv(dfReductions, "dfReductions.csv")
#Compare TotalGPCD to Potable GPCD
ggplot(dfAllYearsSort ) +
#Bar graph of Total gallons per person per day reversed ranked (largest at left)
geom_point(aes(x= TotalGPCD, y=TotPotaGPCD, color = TotGPCDBin), size = 6) +
scale_color_manual(values = c(palBlues[4], palBlues[6], palBlues[8], sHighlightColor)) +
guides(color = "none") +
labs(x="Total Use (gallons per person per day)", y="Potable Use (gallons per person per day)") +
theme(text = element_text(size=20), legend.title=element_blank(), legend.text=element_text(size=18),
#Remove the minor x grid lines
panel.grid.minor.x = element_blank(),
#panel.grid.major.x = element_blank(),
panel.grid.minor.y = element_blank(),
legend.key = element_blank())
#axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))