-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappyazh.R
More file actions
382 lines (302 loc) · 21.5 KB
/
appyazh.R
File metadata and controls
382 lines (302 loc) · 21.5 KB
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
library(shiny)
library(shinydashboard)
library(ggplot2)
library(lubridate)
library(DT)
library(jpeg)
library(grid)
library(leaflet)
library(scales)
library(tidyr)
library(plotly)
library(dplyr)
library(dashboardthemes)
#read all the files (combining all aqi_by_county files into one dataframe and aqs_sites in another dataframe)
temp = list.files(pattern="^an.*csv")
allData2 <- lapply(temp, read.csv)
weatherinfo <- do.call(rbind, allData2)
weathermap <- read.csv(file = "aqs_sites.csv",header = TRUE, sep = ",")
#converting the variable type of County, State and Year
weatherinfo$County <- as.character(weatherinfo$County)
weatherinfo$State <- as.character(weatherinfo$State)
weatherinfo$Year <- as.numeric(as.character(weatherinfo$Year))
#changing the names of the columns for better graph plotting
names(weatherinfo)[names(weatherinfo)=="Good.Days"] <- "Good"
names(weatherinfo)[names(weatherinfo)=="Moderate.Days"] <- "Moderate"
names(weatherinfo)[names(weatherinfo)=="Unhealthy.for.Sensitive.Groups.Days"] <- "Unhealthy(Sensitive)"
names(weatherinfo)[names(weatherinfo)=="Unhealthy.Days"] <- "Unhealthy"
names(weatherinfo)[names(weatherinfo)=="Very.Unhealthy.Days"] <- "V Unhealthy"
names(weatherinfo)[names(weatherinfo)=="Hazardous.Days"] <- "Hazardous"
#gather is used to create seperate column for the AQI Parameters, Pollutants and MaxQ levels
weatherinfoGather <- gather(weatherinfo, "AQI Parameters", "Days", 5:10)
weatherinfoGather2 <- gather(weatherinfo, "Pollutants", "Days", 14:19)
weatherinfoGather3 <- gather(weatherinfo,"Maxq", "Count", 11:13)
State1info <- weatherinfoGather2
State2info <- weatherinfoGather2
State3info <- weatherinfoGather2
weatherinfo1 <- weatherinfo
weatherinfo1$CO <- (weatherinfo1$Days.CO / weatherinfo1$Days.with.AQI)* 100
weatherinfo1$NO2 <- (weatherinfo1$Days.NO2 / weatherinfo1$Days.with.AQI) *100
weatherinfo1$Ozone <- (weatherinfo1$Days.Ozone / weatherinfo1$Days.with.AQI) *100
weatherinfo1$SO2 <- (weatherinfo1$Days.SO2 / weatherinfo1$Days.with.AQI) * 100
weatherinfo1$PM2_5 <- (weatherinfo1$Days.PM2.5 / weatherinfo1$Days.with.AQI) * 100
weatherinfo1$PM_10 <- (weatherinfo1$Days.PM10 / weatherinfo1$Days.with.AQI) * 100
weatherinfo1 <- weatherinfo1 %>% mutate_if(is.numeric, round, 2)
weatherinfo1Gather <- gather(weatherinfo1,"NewPollutants","Count",20:25)
#Creating new columns which has aggregate percentage of all AQI types
weatherinfo2 <-weatherinfo
weatherinfo2$Good <- (weatherinfo2$Good / weatherinfo2$Days.with.AQI)* 100
weatherinfo2$Moderate <- (weatherinfo2$Moderate / weatherinfo2$Days.with.AQI) *100
weatherinfo2$`Unhealthy(Sensitive)` <- (weatherinfo2$`Unhealthy(Sensitive)` / weatherinfo2$Days.with.AQI) *100
weatherinfo2$Unhealthy <- (weatherinfo2$Unhealthy / weatherinfo2$Days.with.AQI) * 100
weatherinfo2$`V Unhealthy` <- (weatherinfo2$`V Unhealthy` / weatherinfo2$Days.with.AQI) * 100
weatherinfo2$Hazardous <- (weatherinfo2$Hazardous / weatherinfo2$Days.with.AQI) * 100
weatherinfo2 <- weatherinfo2 %>% mutate_if(is.numeric, round, 2)
weatherinfo2Gather <- gather(weatherinfo2,"AQIP","Count",5:10)
# Create the menu items to select the different years and the different rooms
counties <- weatherinfo[,2]
years<-c(1980:2018)
states <- weatherinfo[,1]
counties1 <- weatherinfo[,2]
years1<-c(1980:2018)
states1 <- weatherinfo[,1]
counties2 <- weatherinfo[,2]
years2<-c(1980:2018)
states2 <- weatherinfo[,1]
counties3 <- weatherinfo[,2]
years3<-c(1980:2018)
states3 <- weatherinfo[,1]
df <- weatherinfo1Gather[(weatherinfo1Gather$State == "Illinois" & weatherinfo1Gather$County == "Cook") |
(weatherinfo1Gather$State == "Alabama" & weatherinfo1Gather$County == "Autauga") |
(weatherinfo1Gather$State == "Alabama" & weatherinfo1Gather$County == "Colbert") &
weatherinfo1Gather$Year == 2018,c(2,14,15) ]
# Dreate the shiny dashboard
ui <- dashboardPage(skin = "purple",
dashboardHeader(title = "Just Breathe", titleWidth = 350),
dashboardSidebar(includeCSS("styles.css"), disable = FALSE, collapsed = FALSE,
selectInput("State", "Select the State to visualize", states, selected = "Illinois"),
selectInput("County", "Select the County to visualize", counties, selected = "Cook"),
selectInput("Year", "Select the year to visualize", years, selected = 2018),
sidebarMenu(
menuItem("About", tabName = "about"),
menuItem("AQI Air Quality",tabName = "aqiquality"),
menuItem("Pollutants In the Air ",tabName = "Pollutant") ,
menuItem("Map of USA", tabName = "mapofusa"),
menuItem("Compare Data of 3 Counties", tabName = "comparedata"))),
dashboardBody(
shinyDashboardThemes( theme = "grey_dark"),
tabItems(
tabItem("about", includeHTML("about.html")),
tabItem("aqiquality",
fluidRow(column(width =12,h1("AQI Over The Years"))),
fluidRow(column(12,fluidRow(box(width = 14,status = "info",dataTableOutput("AQIQualityTable"))))),
fluidRow(splitLayout(style = "border: 1px solid silver:", cellWidths = c(350,350,400),
plotlyOutput("AQIPiePlot"),
plotlyOutput("AQIBarGraph"),
plotlyOutput("AQImaxq")))
),
tabItem("Pollutant",
fluidRow(column(width =12,h1("Pollutant Over The Years"))),
fluidRow(splitLayout(style = "border: 1px solid silver:", cellWidths = c(760,660),
dataTableOutput("PollutantTable"),
dataTableOutput("PollutantTypeTable"))),
fluidRow(splitLayout(style = "border: 1px solid silver:", cellWidths = c(300,350,350,300),
plotlyOutput("Pollutantline"),
plotlyOutput("PollutantPiePlot"),
plotlyOutput("PollutantBarGraph")
))
),
tabItem("mapofusa", width = 12, h1("Leaflet of USA"),leafletOutput("Map0", height = 400) ),
tabItem("comparedata",
fluidRow(box(width = 4, selectInput("Year1", "Select the year to visualize", years1, selected = 2018, width="120px"),
selectInput("State1", "Select the State to visualize", states1, selected = "Illinois", width="120px"),
selectInput("County1", "Select the County to visualize", counties1, selected = "Cook", width="120px")),
box(width = 4, selectInput("Year2", "Select the year to visualize", years2, selected = 2018, width="120px"),
selectInput("State2", "Select the State to visualize", states2, selected = "Illinois", width="120px"),
selectInput("County2", "Select the County to visualize", counties2, selected = "Cook",width="120px")),
box(width = 4, selectInput("Year3", "Select the year to visualize", years3, selected = 2018, width="120px"),
selectInput("State3", "Select the State to visualize", states3, selected = "Illinois", width="120px"),
selectInput("County3", "Select the County to visualize", counties3, selected = "Cook",width="120px"))),
fluidRow(title = "Compare 3 Counties", box(splitLayout(style = "border: 1px solid silver:", cellWidths = c(350,350,350),
plotlyOutput("comparepollutant_bar", height = "60vh"),
plotlyOutput("comparepollutant_line"),
plotlyOutput("compareaqi_line", height = "60vh"),
plotlyOutput("compareaqi_bar")
))
)
)
))
)
server <- function(input,output,session){
#changing options for County depending on the State chosen
observe({
x <- input$State
# Can use character(0) to remove all choices
if (is.null(x))
x <- character(0)
# Can also set the label and select items
updateSelectInput(session, "County",
choices = weatherinfo$County[weatherinfo$State==x]
)
})
observe({
y <- input$State1
# Can use character(0) to remove all choices
if (is.null(y))
y <- character(0)
# Can also set the label and select items
updateSelectInput(session, "County1",
choices = State1info$County[State1info$State==y])
# updateSelectInput(session, "State2", choices = State2info$State[State2info$State != y]
# )
})
observe({
z <- input$State2
# Can use character(0) to remove all choices
if (is.null(z))
z <- character(0)
# Can also set the label and select items
updateSelectInput(session, "County2",
choices = State2info$County[State2info$State==z]
)
})
observe({
a <- input$State3
# Can use character(0) to remove all choices
if (is.null(a))
a <- character(0)
# Can also set the label and select items
updateSelectInput(session, "County3",
choices = State3info$County[State3info$State==a]
)
})
output$AQIQualityTable <-
renderDataTable(weatherinfo[weatherinfo$County == input$County & weatherinfo$State == input$State & weatherinfo$Year == input$Year ,c(1,2,3,5,6,7,8,9,10) ], options = list(columnDefs = list(list(
targets = 1:9 , className = "right"
)), pageLength = 5))
output$AQIBarGraph <- renderPlotly ({
plot_ly(data = weatherinfoGather[weatherinfoGather$State == input$State & weatherinfoGather$County == input$County & weatherinfoGather$Year == input$Year, ], x = reorder(weatherinfoGather[weatherinfoGather$State == input$State & weatherinfoGather$County == input$County & weatherinfoGather$Year == input$Year, 14],-weatherinfoGather[weatherinfoGather$State == input$State & weatherinfoGather$County == input$County & weatherinfoGather$Year == input$Year,15 ]), y = weatherinfoGather[weatherinfoGather$State == input$State & weatherinfoGather$County == input$County & weatherinfoGather$Year == input$Year,15 ],
color = (reorder(weatherinfoGather[weatherinfoGather$State == input$State & weatherinfoGather$County == input$County & weatherinfoGather$Year == input$Year, 14],-weatherinfoGather[weatherinfoGather$State == input$State & weatherinfoGather$County == input$County & weatherinfoGather$Year == input$Year,15 ])) ,
type = 'bar', colors = c("#2E8B57","#00CED1","#FF8C00","#000080","#C6590F","#8B008B")) %>%
layout(title = paste("AQI types for", input$Year))
})
output$AQIPiePlot <-
renderPlotly({
colors <- c("#2E8B57","#00CED1","#FF8C00","#000080","#C6590F","#8B008B")
plot_ly(weatherinfoGather[weatherinfoGather$State == input$State & weatherinfoGather$County == input$County & weatherinfoGather$Year == input$Year, ], labels = ~ weatherinfoGather[weatherinfoGather$State == input$State & weatherinfoGather$County == input$County & weatherinfoGather$Year == input$Year, 14], values = ~ weatherinfoGather[weatherinfoGather$State == input$State & weatherinfoGather$County == input$County & weatherinfoGather$Year == input$Year, 15], type = 'pie',
textposition = 'inside',
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(colors = colors,
line = list(color = '#FFFFFF', width = 1)), showlegend = FALSE) %>%
layout(title = paste('AQI Quality for year ', input$Year),
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
})
output$AQImaxq <- renderPlotly({
plot_ly(data = weatherinfoGather3[weatherinfoGather3$State == input$State & weatherinfoGather3$County == input$County,],
x = weatherinfoGather3[weatherinfoGather3$State == input$State & weatherinfoGather3$County == input$County,3],
y = weatherinfoGather3[weatherinfoGather3$State == input$State & weatherinfoGather3$County == input$County,18],
color = weatherinfoGather3[weatherinfoGather3$State == input$State & weatherinfoGather3$County == input$County,17],
colors =c("#2E8B57","#00CED1","#FF8C00"),type="scatter",mode="lines+markers") %>%
layout(title = "AQI over the years")
})
output$PollutantTable <-
renderDataTable(weatherinfo[weatherinfo$County == input$County & weatherinfo$State == input$State & weatherinfo$Year == input$Year ,c(1,2,3,14,15,16,17,18,19) ],
options = list(columnDefs = list(list(
targets = 1:9 , className = "right")), pageLength = 5))
output$PollutantTypeTable <-
renderDataTable(weatherinfo1[weatherinfo1$County == input$County & weatherinfo1$State == input$State ,c(1,2,3,20,21,22,23,24,25) ],
options = list(columnDefs = list(list(targets = 1:9 , className = "right")), pageLength = 5))
output$PollutantBarGraph <- renderPlotly ({
plot_ly(data = weatherinfoGather2[weatherinfoGather2$State == input$State & weatherinfoGather2$County == input$County & weatherinfoGather2$Year == input$Year, ], x = reorder(weatherinfoGather2[weatherinfoGather2$State == input$State & weatherinfoGather2$County == input$County & weatherinfoGather2$Year == input$Year, 14],-weatherinfoGather2[weatherinfoGather2$State == input$State & weatherinfoGather2$County == input$County & weatherinfoGather2$Year == input$Year, 15]), y = weatherinfoGather2[weatherinfoGather2$State == input$State & weatherinfoGather2$County == input$County & weatherinfoGather2$Year == input$Year,15 ],
color = reorder(weatherinfoGather2[weatherinfoGather2$State == input$State & weatherinfoGather2$County == input$County & weatherinfoGather2$Year == input$Year, 14],-weatherinfoGather2[weatherinfoGather2$State == input$State & weatherinfoGather2$County == input$County & weatherinfoGather2$Year == input$Year, 15]) ,
type = 'bar', colors = c("#2E8B57","#00CED1","#FF8C00","#000080","#C6590F","#8B008B")) %>%
layout(title = paste("Pollutant types for", input$Year))
})
output$PollutantPiePlot <-
renderPlotly({
colors <- c("#2E8B57","#00CED1","#FF8C00","#000080","#C6590F","#8B008B")
plot_ly(weatherinfoGather2[weatherinfoGather2$State == input$State & weatherinfoGather2$County == input$County &
weatherinfoGather2$Year == input$Year, ],
labels = ~ weatherinfoGather2[weatherinfoGather2$State == input$State &
weatherinfoGather2$County == input$County &
weatherinfoGather2$Year == input$Year, 14],
values = ~ weatherinfoGather2[weatherinfoGather2$State == input$State &
weatherinfoGather2$County == input$County & weatherinfoGather2$Year == input$Year, 15],
type = 'pie',
textposition = 'inside',
textinfo = 'label+percent',
insidetextfont = list(color = '#FFFFFF'),
marker = list(colors = colors,
line = list(color = '#FFFFFF', width = 1)), showlegend = FALSE) %>%
layout(title = paste('Pollutants for year ', input$Year) ,
xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))
})
output$Pollutantline <- renderPlotly({
plot_ly(data = weatherinfo1Gather[weatherinfo1Gather$State == input$State & weatherinfo1Gather$County == input$County,],
x = weatherinfo1Gather[weatherinfo1Gather$State == input$State & weatherinfo1Gather$County == input$County,3],
y = weatherinfo1Gather[weatherinfo1Gather$State == input$State & weatherinfo1Gather$County == input$County,21],
color = weatherinfo1Gather[weatherinfo1Gather$State == input$State & weatherinfo1Gather$County == input$County,20],
colors = c("#2E8B57","#00CED1","#FF8C00","#000080","#C6590F","#8B008B"),type="scatter",mode="lines+markers") %>%
layout(title = "Pollutants % over the years")
})
# Render Leaflet Map for Location Data
output$Map0 <- renderLeaflet({
map <- leaflet()
map <- addTiles(map)
map <- setView(map, lng = weathermap[weathermap$County.Name == input$County & weathermap$State.Name == input$State,5][1], lat = weathermap[weathermap$County.Name == "Cook" & weathermap$State.Name == "Illinois",4][1], zoom = 4)
map <- addMarkers(map, lng = weathermap[weathermap$County.Name == input$County & weathermap$State.Name == input$State,5][1], lat = weathermap[weathermap$County.Name == input$County & weathermap$State.Name == input$State,4][1], popup = input$County)
map
} )
compare1 <- reactive({
data1 <- weatherinfoGather[(weatherinfoGather$State == input$State1 & weatherinfoGather$County == input$County1 & weatherinfoGather$Year == input$Year1) |
(weatherinfoGather$State == input$State2 & weatherinfoGather$County == input$County2 & weatherinfoGather$Year == input$Year2) |
(weatherinfoGather$State == input$State3 & weatherinfoGather$County == input$County3 & weatherinfoGather$Year == input$Year3) ,c(2,14,15) ]
data1 <- data1[order(data1$County),]
data1
})
output$compareaqi_bar <- renderPlotly({
ggplot(compare1(), aes(y=Days, x=`AQI Parameters`, color=`AQI Parameters`, fill=`AQI Parameters`)) +
geom_bar( stat="identity") + facet_wrap(~County) + labs( y = "Days",title = paste("AQI types for", input$Year))+
theme(axis.title.x=element_blank(),axis.text.x=element_blank(),axis.ticks.x=element_blank(),legend.title =element_blank())
})
compare2 <- reactive({
data2 <- weatherinfo2Gather[(weatherinfo2Gather$State == input$State1 & weatherinfo2Gather$County == input$County1 ) |
(weatherinfo2Gather$State == input$State2 & weatherinfo2Gather$County == input$County2) |
(weatherinfo2Gather$State == input$State3 & weatherinfo2Gather$County == input$County3) ,c(2,3,14,15)]
data2 <- data2[order(data2$County),]
data2
})
output$compareaqi_line <- renderPlotly({
ggplot(compare2(), aes(y=Count, x=Year, color=AQIP, group =AQIP)) +
geom_line() + facet_wrap(~County) + labs( y = "Days",title = paste("AQI types over the Years"))+
theme(axis.title.x=element_blank(),legend.title =element_blank())
})
compare3 <- reactive({
data3 <- weatherinfoGather2[(weatherinfo2Gather$State == input$State1 & weatherinfo2Gather$County == input$County1 & weatherinfo2Gather$Year == input$Year1) |
(weatherinfo2Gather$State == input$State2 & weatherinfo2Gather$County == input$County2 & weatherinfo2Gather$Year == input$Year2) |
(weatherinfo2Gather$State == input$State3 & weatherinfo2Gather$County == input$County3 & weatherinfo2Gather$Year == input$Year3) ,c(2,14,15) ]
data3 <- data3[order(data3$County),]
data3
})
output$comparepollutant_bar <- renderPlotly({
ggplot(compare3(), aes(y=Days, x=Pollutants, color=Pollutants, fill=Pollutants)) +
geom_bar( stat="identity") + facet_wrap(~County) + labs(y = "Days",title = paste("Pollutant types for", input$Year))+
theme(axis.title.x=element_blank(),axis.text.x=element_blank(),axis.ticks.x=element_blank(),legend.title =element_blank())
})
compare4 <- reactive({
data4 <- weatherinfo1Gather[(weatherinfo1Gather$State == input$State1 & weatherinfo1Gather$County == input$County1) |
(weatherinfo1Gather$State == input$State2 & weatherinfo1Gather$County == input$County2) |
(weatherinfo1Gather$State == input$State3 & weatherinfo1Gather$County == input$County3) ,c(2,3,20,21)]
data4 <- data4[order(data4$County),]
data4
})
output$comparepollutant_line <- renderPlotly({
ggplot(compare4(), aes(y=Count, x=Year, color=NewPollutants, group =NewPollutants )) +
geom_line() + facet_wrap(~County) + labs(y = "Days",title = paste("Pollutant types % over the Years"))+
theme(axis.title.x=element_blank(),legend.title =element_blank())
})
}
shinyApp(server = server, ui = ui)