-
Notifications
You must be signed in to change notification settings - Fork 0
/
sormehy_final.Rmd
378 lines (289 loc) · 13.6 KB
/
sormehy_final.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
---
title: "NES Industry Analysis in 4 States"
resource_files:
- cb_2018_us_state_500k.shx
- cb_2018_us_state_500k.shp.iso.xml
- cb_2018_us_state_500k.shp.ea.iso.xml
- cb_2018_us_state_500k.prj
- cb_2018_us_state_500k.dbf
- cb_2018_us_state_500k.cpg
runtime: shiny
output:
flexdashboard::flex_dashboard:
orientation: rows
source_code: embed
theme: flatly
---
```{r setup, include=FALSE}
#knitr::opts_knit$set(root.dir = normalizePath())
knitr::opts_chunk$set(echo = FALSE)
```
```{r context="setup", include=FALSE}
library(shiny)
library(ggplot2)
library(dplyr)
library(tidyr)
library(splines)
library(stringr)
library(RColorBrewer)
library(reshape2)
library(plotly)
library(leaflet)
library(DT)
require(rgdal)
require(leaflet.extras)
require(readxl)
```
```{r data}
## load raw data
#data <- read.csv("nes_selectdata.csv")
data <- read.csv("nes_extratrim.csv")
#data <- read.csv("~/sormehy_final/nes_trimmed.csv")
## Make the data easier to read for the user
nes_st0418_p <- data %>% mutate(Industry.Title = ifelse(NAICS == 23, 'Construction',
ifelse(NAICS == 4853, 'Taxi & Limousine Service', ifelse(NAICS == 484, 'Truck Transportation', ifelse(NAICS == 4841, 'General Freight Trucking',
ifelse(NAICS == 48411, 'General Freight Trucking, Local', ifelse(NAICS == 48412, 'General Freight Trucking, Long-Distance',
ifelse(NAICS == 4842, 'Specialized Freight Trucking', ifelse(NAICS == 236, 'Construction of Buildings',
ifelse(NAICS == 2361, 'Residential Building Construction', ifelse(NAICS == 2362, 'Nonresidential Building Construction',
ifelse(NAICS == 237, 'Heavy & Civil Engineering Construction', ifelse(NAICS == 2371, 'Utility System Construction',
ifelse(NAICS == 2372 | 23721, 'Land Subdivision', ifelse(NAICS == 2373 | 23731, 'Highway, Street, & Bridge Construction',
ifelse(NAICS == 2379 | 23799, 'Other Heavy & Civ Eng Construction', ifelse(NAICS == 238, "Specialty Trade Contractors",
ifelse(NAICS == 2381, "Foundation, Structure, & Building Exterior Contractors",
ifelse(NAICS == 2382, "Building Equipment Contractors",
ifelse(NAICS == 23821, "Electrical Contractors and Other Wiring Installation Contractors",
ifelse(NAICS == 23822, "Plumbing, Heating, and Air-Conditioning Contractors",
ifelse(NAICS == 23829, "Other Building Equipment Contractors",
ifelse(NAICS == 2383, "Building Finishing Contractors",
ifelse(NAICS == 2389, "Other Specialty Trade Contractors"))))))))))))))))))))))))
#construction <- pa.ind.emp.raw %>%
# filter(str_detect(NAICS, "^23"))
NAICS.Categories = c("^4853", "^23", "^48")
Big.Industries = c('Taxi & Limousine Service', 'Construction', 'Truck Transportation')
Count.Type = c("Total_RCPTOT", "Total_ESTAB")
Ind.Industries = c("Taxi & Limousine Service", "Truck Transportation", "General Freight Trucking", "General Freight Trucking, Local",
"General Freight Trucking, Long-Distance", "Specialized Freight Trucking", "Construction", "Construction of Buildings",
"Residential Building Construction", "Nonresidential Building Construction", "Heavy & Civil Engineering Construction",
"Utility System Construction", "Land Subdivision", "Highway, Street, & Bridge Construction",
"Other Heavy & Civ Eng Construction",
"Specialty Trade Contractors", "Foundation, Structure, & Building Exterior Contractors", "Building Equipment Contractors",
"Electrical Contractors and Other Wiring Installation Contractors", "Plumbing, Heating, and Air-Conditioning Contractors",
"Other Building Equipment Contractors", "Building Finishing Contractors", "Other Specialty Trade Contractors")
State.op <- c("Pennsylvania", "Ohio", "Illinois", "Montana")
Year.op <- c(2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018)
## PICK THE THREE out of FOUR STATES FOR COMPARISON
nes_st0418_p <- nes_st0418_p %>% mutate(ST_n = ifelse(ST == 17, 'Illinois', ifelse(ST == 39, 'Ohio', ifelse(ST == 42, 'Pennsylvania', ifelse(ST == 30, 'Montana', 'NaN')))))
#print(getwd())
```
Inputs Column {.sidebar}
-----------------------------------------------------------------------
```{r context="render"}
selectInput("naics_var", label = "Select NAICS Category:", choices = NAICS.Categories)
selectInput("big_ind", label = "Select Industry Title Categories:", choices = Big.Industries)
selectInput("count_type", label = "Select Type of Count:", choices = Count.Type)
checkboxGroupInput("ch_state", "Select States to Compare:", choices = State.op,
selected = c("Pennsylvania", "Ohio", "Illinois"), inline = TRUE
)
# checkboxGroupInput("ind_ind", "Select Detailed Industries:", choices = Ind.Industries,
# selected = c("Taxi and Limousine Service", "Truck Transportation", "Construction"), inline = TRUE
# )
selectizeInput("ind_ind", "Select Detailed Industries:", choices= Ind.Industries,
selected = c("Taxi and Limousine Service", "Truck Transportation", "Construction"),
multiple = TRUE)
selectizeInput("yr", "Select Year:", choices= Year.op,
selected = c("2012"),
multiple = FALSE)
downloadButton("downloadFile", label = "Download Data")
downloadHandler(filename = function() {
paste('resultset-', Sys.Date(), '.csv', sep='')
},
content = function(file) {
write.csv(industry_subset(), file, row.names = FALSE)
}
)
```
Page with Tabset {.tabset .tabset-fade}
-----------------------------------------------------------------------
### Time Series Comparing States across Industries
```{r context="server"}
industry_subset <- reactive({
nes_st0418_p[nes_st0418_p$Industry.Title %in% input$ind_ind & nes_st0418_p$ST_n %in% input$ch_state,]
})
data_year <- reactive({
filter(data,
input$big_ind
)
})
count_type <- reactive({
filter(nes_st0418_p, input$count_type)
})
output$mod.plot <- renderPlotly({
if(input$employed_year == "Emp.2018"){
ggplotly(ggplot(data = industry_subset(), mapping = aes(x = reorder(Industry.Title, -Emp.2018), xend = reorder(Industry.Title, -Emp.2018), y = Emp.2018, yend = 0, color = Industry.Title)) +
geom_point() +
geom_segment()+
coord_flip() +
labs(x='', y='Number of Employees', title='Pennsylvanians Employed in Given Industry') + theme(legend.position = "none"))
}
else {
ggplotly(ggplot(data = industry_subset(), mapping = aes(x = reorder(Industry.Title, -Emp.2028), xend = reorder(Industry.Title, -Emp.2028), y = Emp.2028, yend = 0, color = Industry.Title)) +
geom_point() +
geom_segment()+
coord_flip() +
labs(x='', y='Number of Employees', title='Pennsylvanians Employed in Given Industry') + theme(legend.position = "none"))
}
})
output$compare.plot <- renderPlotly({
if(input$count_type == 'Total_RCPTOT'){
ggplotly(ggplot(data = industry_subset(), mapping = aes(x=Year, y=(Total_RCPTOT/1000000), color = ST_n)) +
geom_line() + geom_point() +
labs(title='Industry Growth Over Time (2004 - 2018)',
x = 'Year', y = 'Total Receipts (in millions) in Given State', color="State") +
facet_wrap('Industry.Title') + theme(
plot.title = element_text(size=12, face="bold"),
axis.title.x = element_text(size=13),
axis.title.y = element_text(size=13)))
}else{
ggplotly(ggplot(data = industry_subset(), mapping = aes(x=Year, y=(Total_ESTAB/10000), color = ST_n)) +
geom_line() + geom_point() +
labs(title='Industry Growth Over Time (2004 - 2018)',
x = 'Year', y = 'Total Establishments (in 10,000s)', color="State") +
facet_wrap('Industry.Title') + theme(
plot.title = element_text(size=12, face="bold"),
axis.title.x = element_text(size=13),
axis.title.y = element_text(size=13)))
}
})
```
```{r context="render"}
#plotlyOutput("scatter.ordered")
#plotlyOutput("mod.plot")
plotlyOutput("compare.plot")
```
### Time Series Comparing Industries across States
```{r context="server"}
industry_subset <- reactive({
nes_st0418_p[nes_st0418_p$Industry.Title %in% input$ind_ind & nes_st0418_p$ST_n %in% input$ch_state,]
})
data_year <- reactive({
filter(data,
input$big_ind
)
})
count_type <- reactive({
filter(nes_st0418_p, input$count_type)
})
output$compare.plot2 <- renderPlotly({
if(input$count_type == 'Total_RCPTOT'){
ggplotly(ggplot(data = industry_subset(), mapping = aes(x=Year, y=(Total_RCPTOT/1000000), color = Industry.Title)) +
geom_line() + geom_point() +
labs(title='Industry Growth Over Time (2004 - 2018)',
x = 'Year', y = 'Total Receipts (in millions) in Given Industry', color="Industry") +
facet_wrap('ST_n') + theme(
plot.title = element_text(size=12, face="bold"),
axis.title.x = element_text(size=13),
axis.title.y = element_text(size=13)))
}else{
ggplotly(ggplot(data = industry_subset(), mapping = aes(x=Year, y=(Total_ESTAB/10000), color = Industry.Title)) +
geom_line() + geom_point() +
labs(title='Industry Growth Over Time (2004 - 2018)',
x = 'Year', y = 'Total Establishments (in 10,000s)', color="Industry") +
facet_wrap('ST_n') + theme(
plot.title = element_text(size=12, face="bold"),
axis.title.x = element_text(size=13),
axis.title.y = element_text(size=13)))
}
})
```
```{r context="render"}
plotlyOutput("compare.plot2")
```
### Mapping
```{r, controls}
# leaflet() %>%
# # Basemaps
# addTiles(group = "OSM (default)") %>%
# addProviderTiles("Stamen.Toner", group = "Toner") %>%
# addProviderTiles("Stamen.TonerLite", group = "Toner Lite") %>%
# # Layers control
# addLayersControl(
# baseGroups = c("OSM (default)", "Toner", "Toner Lite"),
# options = layersControlOptions(collapsed = FALSE)
# )
```
```{r, polygon}
# op_data <- read_excel("Geographies_of_Opportunity_Ranking_Well_Being_by_Congressional_District_(114th_Congress).xlsx") %>%
# mutate(Number = str_pad(Number, 4, pad = "0"))
# library(raster)
# cds.load <- shapefile("cb_2018_us_state_500k.shp")
cds.load <- readOGR("cb_2018_us_state_500k.shp", layer = "cb_2018_us_state_500k", GDAL1_integer64_policy = TRUE)
# Just having the matching GEOID's
cds <- cds.load[cds.load$GEOID %in% nes_st0418_p$ST ,]
cds@data <- merge(cds@data, nes_st0418_p, sort = FALSE, by.x = "GEOID", by.y = "ST")
#cds <- reactive({cds[cds$Industry.Title %in% input$big_ind & cds$Year %in% input$yr,]})
pal <- colorNumeric(
palette = "viridis",
domain = cds$`Total_RCPTOT`)
pal2 <- colorNumeric(
palette = "Blues",
domain = cds$`Total_ESTAB`)
output$map.rcp <- renderLeaflet({
if(input$count_type == 'Total_RCPTOT'){
leaflet(data = cds) %>%
addProviderTiles("Stamen.Toner") %>%
addPolygons(color = ~pal(`Total_RCPTOT`), popup = ~paste0("<b>", `Industry.Title`, ":</b> ", round(`Total_RCPTOT`, 2), " $")) %>%
addLegend(position = "bottomright", pal = pal, values = cds$`Total_RCPTOT`, title = "Value of Total Receipts filed<br> in given year (USD)")
} else {
leaflet(data = cds) %>%
addProviderTiles("Stamen.Toner") %>%
addPolygons(color = ~pal2(`Total_ESTAB`), popup = ~paste0("<b>", `Industry.Title`, ":</b> ", round(`Total_ESTAB`, 2), " estab")) %>%
addLegend(position = "bottomright", pal = pal2, values = cds$`Total_ESTAB`, title = "Value of Total Establishments<br> in given year (USD)")
}
})
# pal <- colorNumeric(
# palette = "Blues",
# domain = cds$`Total_RCPTOT`)
#
# output$map.rcp <- renderLeaflet({
# leaflet(data = cds) %>%
# addProviderTiles("Stamen.Toner") %>%
# addPolygons(color = ~pal(`Total_RCPTOT`), popup = ~paste0("<b>", `Industry.Title`, ":</b> ", round(`Total_RCPTOT`, 2), " years")) %>%
# addLegend(position = "bottomright", pal = pal, values = cds$`Total_RCPTOT`, title = "Value of Total Receipts filed<br> in given year (USD)")
# })
```
```{r context="render"}
leafletOutput('map.rcp')
```
### Segment Plot by Year
```{r context="server"}
unique_ind <- reactive({
nes_st0418_p[ str_detect(nes_st0418_p$NAICS, input$naics_var) & nes_st0418_p$ST_n %in% input$ch_state & nes_st0418_p$Year %in% input$yr,]
})
output$seg.plot <- renderPlotly({
if(input$count_type == 'Total_RCPTOT'){
ggplotly(ggplot(data = unique_ind(), mapping = aes(x = reorder(Industry.Title, -Total_RCPTOT), xend = reorder(Industry.Title, -Total_RCPTOT), y = Total_RCPTOT, yend = 0, color = Industry.Title)) +
geom_point() +
geom_segment()+
coord_flip() +
labs(x='', y='Total Value from Receipts', title='Industry Total Value in Given Year') + theme(legend.position = "none"))
}
else {
ggplotly(ggplot(data = unique_ind(), mapping = aes(x = reorder(Industry.Title, -Total_ESTAB), xend = reorder(Industry.Title, -Total_ESTAB), y = Total_ESTAB, yend = 0, color = Industry.Title)) +
geom_point() +
geom_segment()+
coord_flip() +
labs(x='', y='Total Number of Establishments', title='Industry Size in Given Year') + theme(legend.position = "none"))
}
})
```
```{r context="render"}
#plotlyOutput("scatter.ordered")
plotlyOutput("seg.plot")
```
Column {data-width=350}
-----------------------------------------------------------------------
### Industry Details {data-width=400}
```{r}
renderTable({
industry_subset()
})
```