-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dangerous Places to work in US.R
209 lines (160 loc) · 6.63 KB
/
Dangerous Places to work in US.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
rm(list = ls())
pkgs <- c("readr", "data.table", "dplyr", "tidyr", "DT", "reshape2", "tm", "stringr", "gsubfn", "lubridate",
"ggplot2", "gridExtra", "highcharter", "plotly", "ggrepel", "leaflet", "leaflet.extras", "ggmap",
"RColorBrewer", "viridisLite", "countrycode", "ggmap", "zipcode")
for (pkg in pkgs) {
if (! (pkg %in% rownames(installed.packages())))
{ install.packages(pkg) }
require(pkg, character.only = TRUE)
}
rm(pkgs, pkg)
# load data
getwd()
df <- fread("E:\\Study\\R Projects\\Common files\\severeinjury.csv",
na.strings = "",
stringsAsFactors = FALSE,
data.table = FALSE # false = data.frame
)
glimpse(df)
# 21,578 X 26
df %>%
head(10) %>%
datatable(style = "bootstrap",
class = "table-condensed",
extensions = "Responsive")
# column names: ' ' has been changed to '_'
vas <- names(df)
vas <- gsub(' ','_',vas)
colnames(df) <- vas
df %>%
select(c(EventDate,Employer,Zip,City,State, Longitude, Latitude,
NatureTitle, Part_of_Body_Title, Hospitalized, Amputation,
EventTitle, SourceTitle, Secondary_Source_Title, Final_Narrative)) %>%
mutate(EventDate = mdy(EventDate)) -> df
head(df,4)
#### Zipcode package: to load zipcode based lat,long, city, state.
## can be pulled from govn site as well
data("zipcode")
head(zipcode)
latlong <- zipcode %>%
rename(Zip = zip)
head(latlong)
library(stringr)
df_z <- df %>% select(Zip)
df_z$Zip <- str_pad(df_z$Zip,5,pad = "0") # making the format same
colnames(df_z) <- c("Zip")
head(df_z)
df_z <- merge(df_z,latlong, by = "Zip", all.x = TRUE)
head(df_z,10)
df$Longitude <- df_z$longitude
df$Latitude <- df_z$latitude
df$State <- df_z$state
df$City <- df_z$city
vas <- names(df)
df$Employer =gsub(".*usps|us postal|united states postal|u.s. postal|u.s postal|u. s postal|u. s. postal.*","US_Postal_Service",
ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*US_Postal_Service.*","USPS", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*united parcel|ups |ups,.*","United_Parcel_Service", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*United_Parcel_Service.*","UPS", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*american airl.*","American Airlines", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*AT &|AT&.*","AT_T", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*AT_T.*","AT&T Inc", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*walmart|wallmart|wal-mart.*","wal_mart", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*wal_mart.*","Walmart", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*Publix.*","Publix_", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*Publix_.*","Publix", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*Asplundh.*","Asplundh_", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*Asplundh_.*","Asplundh", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*sodexo.*","sodexo_", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*sodexo_.*","Sodexo", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*Waste Management.*","Waste_Management", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*Waste_Management.*","Waste Management", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*Tyson Foods.*","Tyson_Foods", ignore.case = TRUE, df$Employer)
df$Employer =gsub(".*Tyson_Foods.*","Tyson Foods", ignore.case = TRUE, df$Employer)
# Loading palette
library(RColorBrewer)
display.brewer.all()
# count by employer
df %>% group_by(Employer) %>%
filter(Hospitalized != 0 || Amputation != 0) %>%
summarise(cnt = n()) %>%
arrange(-cnt) %>%
top_n(10)-> emp
ggplot(emp, aes(x = reorder(Employer,-cnt), y = cnt, fill = Employer)) +
geom_bar(stat = "identity") +
geom_text(aes(label = cnt), vjust = -0.5) +
labs(title = "Top 10 Employees (Danger)", y="Count", x = "Employer") +
scale_fill_brewer(palette = 'YlOrRd') +
theme_minimal(base_size = 11)
# count by City
df %>% group_by(City) %>%
filter(Hospitalized != 0 || Amputation != 0) %>%
summarise(cnt = n()) %>%
arrange(-cnt) %>%
top_n(10)-> ct
ggplot(ct, aes(x = reorder(City,-cnt), y = cnt, fill = City)) +
geom_bar(stat = "identity") +
geom_text(aes(label = cnt), vjust = -0.5) +
labs(title = "Top 10 Cities (Danger)", y="Count", x = "City") +
scale_fill_brewer(palette = 'YlOrRd') +
theme_minimal(base_size = 11)
# count by state
df %>% group_by(State) %>%
filter(Hospitalized != 0 || Amputation != 0) %>%
summarise(cnt = n()) %>%
arrange(-cnt) %>%
top_n(10)-> stat
ggplot(stat, aes(x = reorder(State,-cnt), y = cnt, fill = State)) +
geom_bar(stat = "identity") +
geom_text(aes(label = cnt), vjust = -0.5) +
labs(title = "Top 10 States (Danger)", y="Count", x = "State") +
scale_fill_brewer(palette = 'YlOrRd') +
theme_minimal(base_size = 11)
## State and Employer
df %>% group_by(Employer,State) %>%
filter(Hospitalized != 0 || Amputation != 0) %>%
summarise(cnt = n()) %>%
arrange(-cnt) %>%
head(30) %>%
plot_ly(x = ~Employer, y = ~State, z = ~cnt, color = ~cnt) %>%
add_markers()
## By Source (hchart)
df %>% group_by(SourceTitle) %>%
filter(Hospitalized != 0 || Amputation != 0) %>%
summarise(cnt = n()) %>%
arrange(-cnt) %>%
head(30) %>%
hchart("pie", innerSize = '40%', showInLegend = F,
hcaes(x = SourceTitle, y= cnt, color = -cnt)) %>%
hc_add_theme(hc_theme_ffx()) %>%
hc_title(text = "Top 30 Sources")
## Top injuries
df %>% group_by(NatureTitle) %>%
filter(Hospitalized != 0 || Amputation != 0) %>%
summarise(cnt = n()) %>%
arrange(-cnt) %>%
head(10) %>%
hchart("bar", innerSize = '40%', showInLegend = F,
hcaes(x = NatureTitle, y= cnt, color = -cnt)) %>%
hc_add_theme(hc_theme_economist()) %>%
hc_title(text = "Top 10 injury type")
## Injury Body parts
df %>% group_by(Part_of_Body_Title) %>%
filter(Hospitalized != 0 || Amputation != 0) %>%
summarise(cnt = n()) %>%
arrange(-cnt) %>%
head(10) %>%
hchart("bar",
hcaes(x = Part_of_Body_Title, y= cnt, color = -cnt)) %>%
hc_add_theme(hc_theme_economist()) %>%
hc_title(text = "Top 10 injured body parts")
## by events
df %>% group_by(EventTitle) %>%
filter(Hospitalized != 0 || Amputation != 0) %>%
summarise(cnt = n()) %>%
arrange(-cnt) %>%
head(20) %>%
hchart(type = "treemap",
hcaes(x = EventTitle, y= cnt, color = cnt)) %>%
hc_add_theme(hc_theme_538()) %>%
hc_title(text = "Top 20 Events")