-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.R
315 lines (293 loc) · 10.7 KB
/
app.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
# Load or install required packages
if(!require(shiny)){install.packages("shiny")}
if(!require(shinyjs)){install.packages("shinyjs")}
if(!require(dplyr)){install.packages("dplyr")}
if(!require(sf)){install.packages("sf")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(shinyWidgets)){install.packages("shinyWidgets")}
if(!require(lubridate)){install.packages("lubridate")}
if(!require(brazilmaps)){install.packages("brazilmaps")}
if(!require(reshape2)){install.packages("reshape2")}
# library(ggrepel)
# Constants
# Brazilian States (Save app.R with Encoding UTF-8)
STATES <- c('ACRE', 'ALAGOAS', "AMAPÁ", 'AMAZONAS', 'BAHIA', 'CEARÁ', 'DISTRITO FEDERAL', 'ESPÍRITO SANTO', 'GOIÁS', 'MARANHÃO', 'MATO GROSSO', 'MATO GROSSO DO SUL', 'MINAS GERAIS', 'PARÁ', 'PARAÍBA', 'PARANÁ', 'PERNAMBUCO', 'PIAUÍ', 'RIO DE JANEIRO', 'RIO GRANDE DO NORTE', 'RIO GRANDE DO SUL', 'RONDÔNIA', 'RORAIMA', 'SANTA CATARINA', 'SÃO PAULO', 'SERGIPE', 'TOCANTINS')
# Graphs
GRAPH_GENERAL_TYPE <- c('Comparison', 'Composition', 'Distribution', 'Map')
COMPARISON_OPTS <- c('Bar Graph - Compare State Totals', 'Bar Graph - Compare States Over Time', 'Boxplot - Compare States', 'Line Graph', 'Scatterplot - Jitter') # coord_flip()
COMPOSITION_OPTS <- c('Pie Chart', 'Treemap') # 'Stacked Bar Graph',
DISTRIBUTION_OPTS <- c('Histogram - Combine States', 'Histogram - Distinct States')
MAP_OPTS <- c('Choropleth Map')
# Graph type vectors
GRAPH_BY_STATE <- c('Bar Graph - Compare State Totals', 'Boxplot - Compare States')
# Time
SELECT_STATES <- "States: "
SELECT_YEAR <- "Years: "
SELECT_MONTH <- "Months: "
TIME_SELECTIONS <- c('Select date range', 'View year totals', 'View month totals')
BR_df <- read.csv('data/amazon_with_date.csv', encoding="UTF-8")
BR_df$state <- as.factor(BR_df$state)
BR_df$date <- as.Date(BR_df$date)
# map_states <- get_brmap(geo = "State", class = "sf")
# plot_brmap(map_states,
# data_to_join = BR_df,
# join_by = c('nome'='state'),
# var = 'Fires')
ui <- fluidPage(
shinyjs::useShinyjs(),
titlePanel("Brazil Forest Fires"),
sidebarLayout(
sidebarPanel(
selectizeInput(inputId = 'general_plot_type',
label = 'Select a general graph type:',
choices = GRAPH_GENERAL_TYPE, selected = 'bar'),
uiOutput("specific_plot_type"),
uiOutput("additional_info"),
# Turn to dropdown checkbox
pickerInput(
inputId = "selected_states",
label = SELECT_STATES,
choices = STATES,
options = list(
# Looks ugly for comparison graphs with too many states
# `maxOptions` = ?
`actions-box` = TRUE,
size = 8,
`selected-text-format` = "count > 3"
),
multiple = TRUE,
selected = c('AMAZONAS','PARANÁ', 'RIO DE JANEIRO', 'SÃO PAULO')
),
hr(),
strong(id = 'time_setting', 'Time Settings:'),
# materialSwitch(inputId = "time_switch", label = "", status = "primary"),
selectizeInput(inputId = 'time_selection',
label = 'Select a time view:',
choices = TIME_SELECTIONS,
selected = TIME_SELECTIONS[1]),
uiOutput("time_filter"),
actionButton(inputId = "submit_graph",label = "Submit Graph"),
),
mainPanel(
div(id = 'plot_div',
style = "min-height: 300px;",
plotOutput('plot')
)
)
)
)
server <- function(input, output, session) {
# Reactive specific plot list
output$specific_plot_type = renderUI({
if(input$general_plot_type == GRAPH_GENERAL_TYPE[1]){
opts <- COMPARISON_OPTS
}
else if(input$general_plot_type == GRAPH_GENERAL_TYPE[2]){
opts <- COMPOSITION_OPTS
}
else if(input$general_plot_type == GRAPH_GENERAL_TYPE[3]){
opts <- DISTRIBUTION_OPTS
}
else if(input$general_plot_type == GRAPH_GENERAL_TYPE[4]){
opts <- MAP_OPTS
}
else{
opts <- c()
}
selectizeInput(inputId = 'specific_plot_type',
label = 'Select a specific graph type:',
choices = opts, selected = '')
})
# Month or year option available
output$time_filter <- renderUI({
if(input$time_selection == TIME_SELECTIONS[1]){
tagList(
splitLayout(
dateInput(inputId = 'beg_date',
label = 'From:',
value = '1998-01-01',
startview = "month"),
dateInput(inputId = 'end_date',
label = "To:",
value = '2017-12-31',
startview = "month")
)
)
}
else if(input$time_selection == TIME_SELECTIONS[2]){
pickerInput(inputId = "selected_years",
label = SELECT_YEAR,
choices = c(1998:2017),
options = list(
`actions-box` = TRUE,
`selected-text-format` = "count > 3",
size = 8
),
multiple = TRUE,
selected = c(1998:2017))
}
else if(input$time_selection == TIME_SELECTIONS[3]){
pickerInput(inputId = "selected_months",
label = SELECT_MONTH,
choices = month.name,
options = list(
`actions-box` = TRUE,
`selected-text-format` = "count > 3",
size = 8
),
multiple = TRUE,
selected = month.name)
}
})
observeEvent(input$specific_plot_type,{
if(input$specific_plot_type %in% GRAPH_BY_STATE){
updateSelectizeInput(session,
"time_selection",
choices = c(TIME_SELECTIONS[1]),
selected = TIME_SELECTIONS[1]
)}
else{
updateSelectizeInput(session,
"time_selection",
choices = TIME_SELECTIONS,
selected = TIME_SELECTIONS[1]
)}
}
)
observeEvent(
input$submit_graph,
{
p_type <- input$specific_plot_type
# x-axis is time
if(p_type %in% c('Line Graph',
'Scatterplot - Jitter',
'Bar Graph - Compare States Over Time')){
# default (year) is False
time_measure <- input$time_selection
if(time_measure == TIME_SELECTIONS[1]){
df <- BR_df %>%
dplyr::filter(., state %in% input$selected_states) %>%
dplyr::filter(between(as.Date(date), input$beg_date, input$end_date))
}
else if(time_measure == TIME_SELECTIONS[2]){
df <- BR_df %>%
dplyr::filter(state %in% input$selected_states) %>%
dplyr::filter(year(date) %in% input$selected_years)
if(p_type == 'Line Graph'){
df <- aggregate(fires ~ year + state, data = df, sum)
}
else if(p_type == 'Bar Graph - Compare States Over Time'){
df <- aggregate(df['fires'], by=df[c('year','state')], sum)
}
}
else if(time_measure == TIME_SELECTIONS[3]){
df <- BR_df %>%
dplyr::filter(state %in% input$selected_states) %>%
dplyr::filter(month(date, label = T, abbr = F) %in% input$selected_months)
if(p_type == 'Line Graph'){
df <- aggregate(fires ~ month + state, data = df, sum)
}
}
else{
throw("Invalid time setting: ", time_measure)
}
gg <- ggplot(df) +
theme(legend.position="bottom") +
labs (x = paste0("Time (",
time_col(time_measure),
")"),
y = "Number of Fires",
title = "Number of Fires Over Time",
colour = "State"
)
# conditional graph type additions
if(p_type == 'Line Graph'){
gg <- gg + geom_line(aes_string(
x = time_col(time_measure),
y = 'fires',
color = 'state'))
}
else if(p_type == 'Scatterplot - Jitter'){
gg <- gg + geom_jitter(aes_string(
x = time_col(time_measure),
y = 'fires',
color = 'state'))
}
else if(p_type == 'Bar Graph - Compare States Over Time'){
gg <- gg +
geom_bar(position="dodge",
stat="identity",
aes_string(
x = time_col(time_measure),
y = 'fires',
fill = 'state'))
}
# Add month labels if group by month
if(time_measure == TIME_SELECTIONS[3]){
gg <- gg + scale_x_discrete(limits = month.abb)
}
output$plot <- renderPlot({
options(scipen = 6)
gg
})
}
# x-axis is states
else if(p_type %in% GRAPH_BY_STATE){
df <- BR_df %>%
dplyr::filter(state %in% input$selected_states) %>%
dplyr::filter(between(as.Date(date), input$beg_date, input$end_date))
if(p_type == GRAPH_BY_STATE[1]){
df <- aggregate(fires ~ state, data = df, sum)
}
gg <- ggplot(df) +
theme(legend.position="bottom") +
labs (x = "Brazilian States",
y = "Number of Fires",
title = paste0("Number of Fires From ",
input$beg_date,
" to ",
input$end_date,
collapse = ' '),
colour = "State")
# conditional graph type additions
if(p_type == GRAPH_BY_STATE[1]){
gg <- gg +
geom_bar(stat = "identity",
aes_string(
x = 'state',
y = 'fires',
fill = 'state')) +
coord_flip()
}
else if(p_type == GRAPH_BY_STATE[2]){
gg <- gg +
geom_boxplot(outlier.colour="black",
outlier.shape=19,
aes_string(
x = 'state',
y = 'fires',
color = 'state'))
if(length(input$selected_states) > 8){
gg <- gg +
theme(axis.text.x = element_text(angle = 90))
}
}
output$plot <- renderPlot({
options(scipen = 6)
gg
})
}
})
}
time_col <- function(time_measure){
if(time_measure == TIME_SELECTIONS[1]){
return("date")
}
else if(time_measure == TIME_SELECTIONS[2]){
return('year')
}
else{
return('month')
}
}
shinyApp(ui = ui, server = server)