forked from UBC-MDS/DSCI532_Group112_Unemployment_DashR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
371 lines (314 loc) · 12.9 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
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
library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)
library(dashTable)
library(tidyverse)
library(scales)
library(plotly)
app <- Dash$new(external_stylesheets = "https://codepen.io/chriddyp/pen/bWLwgP.css")
# Load the data
unemply_df_year <- read_csv("https://raw.githubusercontent.com/UBC-MDS/DSCI532_Group112_Unemployment_DashR/master/data/unemply_df_year.csv")
unemply_df_month <- read_csv("https://raw.githubusercontent.com/UBC-MDS/DSCI532_Group112_Unemployment_DashR/master/data/unemply_df_month.csv")
# Some wrangling
df <- unemply_df_year %>%
select(-c(count, rate, X1))
df <- df %>%
spread(key = 'year', value = 'total')
######### SELECTION COMPONENTS ################
#Range year slider (get the years from the dataset to make ticks on the slider)
yearMarks <- map(unique(unemply_df_year$year), as.character)
names(yearMarks) <- unique(unemply_df_year$year)
yearRangeSlider <- dccRangeSlider(
id = 'yearrange',
marks = yearMarks,
min = 2000,
max = 2010,
step=1,
value = list(2003, 2005)
)
# Select the single year for plot 3
yearSlider <- dccSlider(
id = 'year',
marks = yearMarks[-11],
min = 2000,
max = 2009,
value = 2000
)
#Dropdown to select industries interested
all_industries <- unique(unemply_df_year$industry)
industryDropdown <- dccDropdown(
id = 'industry',
options = map(
unique(unemply_df_year$industry), function(x){
list(label=x, value=x)
}),
value = c("Agriculture", "Construction"),
multi = TRUE
)
#Radio button to select statistic type
statRadioButton <- dccRadioItems(
id = 'stat',
options=list(
list(label = "Rate", value = "rate"),
list(label = "Count", value = "count")
),
value = "rate"
)
# Plot 1
make_plot_1 <- function(year_range = c(2003, 2005), stat = "rate"){
new_df <- df %>%
select(industry)
if(stat == "rate"){
new_df$rate <- unlist(round((df[as.character(year_range[2])] -
df[as.character(year_range[1])]) / df[as.character(year_range[1])], 2))
new_df <- new_df %>%
mutate(colour = ifelse(rate < 0, "type1", "type2"))
p <- ggplot(new_df, aes(industry, rate, colour = colour)) +
geom_segment(aes(xend = industry, y = 0, yend = rate)) +
geom_point(size = 2) +
coord_flip() +
scale_y_continuous(labels = percent_format(accuracy = 1L)) +
labs(x = '', y = 'Percentage Change')+
theme_bw() +
theme(legend.position = "none")
} else {
new_df$count <- unlist(round((df[as.character(year_range[2])] - df[as.character(year_range[1])])))
new_df <- new_df %>%
mutate(colour = ifelse(count < 0, "type1", "type2"))
p <- ggplot(new_df, aes(industry, count, colour = colour)) +
geom_segment(aes(xend = industry, y = 0, yend = count)) +
geom_point(size = 2) +
coord_flip() +
labs(x = ' ', y = 'Absolute Change')+
theme_bw() +
theme(legend.position = "none")
}
gp <- ggplotly(p, width = 800, height = 500, tooltip = FALSE)
return(gp)
}
# Plot 2
make_plot_2 <- function(industries = c("Agriculture", "Construction"), stat = "rate"){
avg_df <- unemply_df_year %>%
group_by(year) %>%
summarize(rate = mean(rate),
count = mean(count))
new_df <- unemply_df_year %>%
filter(industry %in% industries)
if(stat == "rate"){
p <- ggplot() +
geom_line(new_df, mapping = aes(factor(year), rate, colour = industry, group = industry)) +
geom_point(new_df, mapping = aes(factor(year), rate, colour = industry, group = industry)) +
geom_line(avg_df, mapping = aes(factor(year), rate, group = 1), alpha = 0.4, linetype = 'dashed') +
scale_y_continuous(labels = percent_format(accuracy = 1L)) +
labs(x = '', y = 'Rate', colour = 'Industry') +
theme_bw()
} else {
p <- ggplot() +
geom_line(new_df, mapping = aes(factor(year), count, colour = industry, group = industry)) +
geom_point(new_df, mapping = aes(factor(year), count, colour = industry, group = industry)) +
geom_line(avg_df, mapping = aes(factor(year), count, group = 1), alpha = 0.4, linetype = 'dashed') +
labs(x = '', y = 'Count', colour = 'Industry') +
theme_bw()
}
gp <- ggplotly(p, width = 800, height = 500, tooltip =FALSE)
return(gp)
}
# Plot 3
make_plot_3 <- function(industries = c("Agriculture", "Construction"), year_desired = 2000, stat = "rate"){
avg_df <- unemply_df_month %>%
group_by(month) %>%
summarize(rate = mean(rate),
count = mean(count))
new_df <- unemply_df_month %>%
filter(year == year_desired,
industry %in% industries)
if(stat == "rate"){
p <- ggplot() +
geom_line(new_df, mapping = aes(factor(month), rate, colour = industry, group = industry)) +
geom_point(new_df, mapping = aes(factor(month), rate, colour = industry, group = industry)) +
geom_line(avg_df, mapping = aes(month, rate), alpha = 0.4, linetype = 'dashed') +
scale_y_continuous(labels = percent_format(accuracy = 1L)) +
scale_x_discrete(breaks = seq_along(1:12), labels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) +
labs(x = '', y = 'Rate', colour = 'Industry') +
theme_bw()
} else {
p <- ggplot() +
geom_line(new_df, mapping = aes(factor(month), count, colour = industry, group = industry)) +
geom_point(new_df, mapping = aes(factor(month), count, colour = industry, group = industry)) +
geom_line(avg_df, mapping = aes(month, count), alpha = 0.4, linetype = 'dashed') +
scale_x_discrete(breaks = seq_along(1:12), labels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) +
labs(x = '', y = 'Count', colour = 'Industry') +
theme_bw()
}
gp <- ggplotly(p, width = 800, height = 500, tooltip =FALSE)
return(gp)
}
# Create tabs and put graph in each tab
tabs <- dccTabs(id='tabs', value='tab-1', children=list(
dccTab(label='Job Growth Across Industries', value='tab-1'),
dccTab(label='Unemployment Throughout The Years', value='tab-2'),
dccTab(label='Seasonal Unemployment', value='tab-3')
))
graph1 <- dccGraph(
id = 'tab1-graph',
figure = make_plot_1()
)
graph2 <- dccGraph(
id = 'tab2-graph',
figure = make_plot_2()
)
graph3 <- dccGraph(
id = 'tab3-graph',
figure = make_plot_3()
)
#Contents for each tab
content1 <- htmlDiv(style = list('display' = 'flex'), list(
htmlDiv(style = list('columnCount' = 1, 'padding' = '25px', "width" = '60%'), list(
htmlIframe(height=1, width=10, style=list(borderWidth = 0)), #space
graph1
)),
htmlDiv(style = list('columnCount' = 1, 'padding' = '10px', 'width' = '30%'), list(
#selection components go here
htmlIframe(height=60, width=10, style=list(borderWidth = 0)), #space
dccMarkdown('**Select a statistic:**'),
statRadioButton,
htmlIframe(height=20, width=10, style=list(borderWidth = 0)), #space
dccMarkdown('**Select a year range:**'),
yearRangeSlider,
htmlIframe(height=25, width=10, style=list(borderWidth = 0)), #space
#end selection components
htmlIframe(height=30, width=10, style=list(borderWidth = 0)), #space
dccMarkdown("[Data Source](https://observablehq.com/@randomfractals/vega-datasets)")
))
))
content2 <- htmlDiv(style = list('display' = 'flex'), list(
htmlDiv(style = list('columnCount' = 1, 'padding' = '25px', "width" = '60%'), list(
htmlIframe(height=5, width=10, style=list(borderWidth = 0)), #space
graph2,
htmlLabel('Dashed line is the average', style = list("margin-left" = "500px", 'font-size'='14px'))
)),
htmlDiv(style = list('columnCount' = 1, 'padding' = '10px', 'width' = '30%'), list(
#selection components
htmlIframe(height=60, width=10, style=list(borderWidth = 0)), #space
dccMarkdown('**Select a statistic:**'),
statRadioButton,
htmlIframe(height=20, width=10, style=list(borderWidth = 0)), #space
dccMarkdown('**Select industries:**'),
industryDropdown,
#end selection components
htmlIframe(height=20, width=10, style=list(borderWidth = 0)), #space
dccMarkdown("[Data Source](https://observablehq.com/@randomfractals/vega-datasets)")
))
))
content3 <- htmlDiv(style = list('display' = 'flex'), list(
htmlDiv(style = list('columnCount' = 1, 'padding' = '25px', "width" = '60%'), list(
htmlIframe(height=5, width=10, style=list(borderWidth = 0)), #space
graph3,
htmlLabel('Dashed line is the average', style = list("margin-left" = "500px", 'font-size'='14px'))
)),
htmlDiv(style = list('columnCount' = 1, 'padding' = '10px', 'width' = '30%'), list(
htmlIframe(height=60, width=10, style=list(borderWidth = 0)),
#selection components go here
dccMarkdown('**Select a statistic:**'),
statRadioButton,
htmlIframe(height=20, width=10, style=list(borderWidth = 0)), #space
dccMarkdown('**Select industries:**'),
industryDropdown,
htmlIframe(height=20, width=10, style=list(borderWidth = 0)), #space
dccMarkdown('**Select a year:**'),
yearSlider,
#end selection components
htmlIframe(height=20, width=10, style=list(borderWidth = 0)), #space
dccMarkdown("[Data Source](https://observablehq.com/@randomfractals/vega-datasets)")
))
))
# Title features
colors <- list(
background = '#111111',
text = '#7FDBFF',
lightblue= '#71adc9'
)
pageTitle <- htmlDiv(list(
htmlH1('Exploring Unemployment Across Industries'),
htmlH5('These graphs display a framework for countries to examine their unemployment rates/counts across industries.')),
style = list(
textAlign = 'center',
color = '#0f1314',
'font-family'= 'Optima',
'font-style'= 'bold',
"margin-left"= "5px",
"margin-top"= "5px",
"margin-bottom"="5px",
'font-size'='40px',
padding = 25,
backgroundColor = colors$lightblue
))
markdown_text <- "
Unemployment rates is defined as the number of civilian labor force divided by the number of unemployed, but are actively seeking work.
This can help inform us on multiple factors for a country or an industry including the rise and fall of a country's economic condition, as well as the trends on the job market.
There are 3 main questions we are interested in exploring:
**Tab 1** ***Which industry has grown/shrunk the most?***
**Tab 2** ***How does overall unemployment change in country X over the years?***
**Tab 3** ***Is the unemployment rate across industries seasonal?***
Understanding unemployment trends could help us address economic challenges and determine which industries are facing job losses or gains.
Explore these graphs yourselves!
"
app$layout(htmlDiv(list(
htmlH1(pageTitle),
htmlIframe(height=10, width=10, style=list(borderWidth = 0)), #space
dccMarkdown(children=markdown_text, style= list('font-family'='Geneva')),
htmlIframe(height=15, width=10, style=list(borderWidth = 0)), #space
dccTabs(id="tabs", value='tab-1', style= list('font-family'= 'Optima', 'font-style'= 'bold','font-weight' = 900),
children=list(
dccTab(label='Job Growth Across Industries', value='tab-1'),
dccTab(label='Unemployment Throughout The Years', value='tab-2'),
dccTab(label='Seasonal Unemployment', value='tab-3')
)),
htmlDiv(id='tabs-content')
)))
#Callbacks
app$callback(output('tabs-content', 'children'),
params = list(input('tabs', 'value')),
function(tab){
if(tab == 'tab-1'){
return(content1)}
else if(tab == 'tab-2'){
return(content2)}
else if(tab == 'tab-3'){
return(content3)}
}
)
app$callback(
# update figure of tab1-graph
output=list(id = 'tab1-graph', property='figure'),
# based on values of year, industries components
params=list(input(id = 'yearrange', property='value'),
input(id = 'stat', property='value')),
# this translates your list of params into function arguments
function(year_value, stat) {
make_plot_1(year_value, stat)
})
app$callback(
# update figure of tab2-graph
output=list(id = 'tab2-graph', property='figure'),
# based on values of year, industries components
params=list(input(id = 'industry', property='value'),
input(id = 'stat', property='value')),
# this translates your list of params into function arguments
function(industry, stat) {
make_plot_2(industry, stat)
})
app$callback(
# update figure of tab3-graph
output=list(id = 'tab3-graph', property='figure'),
# based on values of year, industries components
params=list(input(id = 'industry', property='value'),
input(id = 'year', property='value'),
input(id = 'stat', property='value')),
# this translates your list of params into function arguments
function(industry, year, stat) {
make_plot_3(industry, year, stat)
})
app$run_server(host = "0.0.0.0", port = Sys.getenv('PORT', 8050))