-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathShootings_html_version.Rmd
2159 lines (1604 loc) · 91.1 KB
/
Shootings_html_version.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "US Mass Shootings 1982 to 2019"
date: 15/04/2019
author: Nic Fox
output:
html_document:
toc: true # table of content true
toc_depth: 2 # upto three depths of headings (specified by #, ## and ###)
toc_float: true # table of contents is always visible, to the left of the document
theme: united
css: "Shootings.css"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r import_data, include=FALSE}
library(tidyverse)
library(operators)
library(magrittr)
library(dplyr)
library(knitr)
library(sf)
library(usmap)
library(waffle)
library(treemapify)
library(gganimate)
library(tweenr)
library(gifski)
library(png)
library(viridis)
# Import the shootings data from the CSV file and save it as a data frame called imported_data
imported_data <- read.csv("ShootingsData.csv", strip.white = TRUE)
# Create a copy of the data to change during cleansing, so that the original data is preserved
cleansed_data <- imported_data
# Create the colour palette for the data visualisations using the magma colour palette from the viridis package
colour_palette <- magma(101)
```
```{r cleanse_data, include=FALSE}
# Create new column containing 1 for each row, to use when showing number of shootings per grouping
cleansed_data$for_count <- 1
# Update the gender column so that there is only one version of "Female" etc
cleansed_data$gender = case_when(
cleansed_data$gender == "M" ~ "Male",
cleansed_data$gender == "Male" ~ "Male",
cleansed_data$gender == "F" ~ "Female",
cleansed_data$gender == "Female" ~ "Female",
cleansed_data$gender == "Male & Female" ~ "Male & Female"
)
# Update the race column so that there is only one version of Unknown, Black, White etc
cleansed_data$race = case_when(
cleansed_data$race == "Other" ~ "Unknown",
cleansed_data$race == "-" ~ "Unknown",
cleansed_data$race == "unclear" ~ "Unknown",
cleansed_data$race == "" ~ "Unknown",
cleansed_data$race == "black" ~ "Black",
cleansed_data$race == "Black" ~ "Black",
cleansed_data$race == "Latino" ~ "Latino",
cleansed_data$race == "Native American" ~ "Native American",
cleansed_data$race == "white" ~ "White",
cleansed_data$race == "White" ~ "White",
cleansed_data$race == "White " ~ "White"
)
# For rows where race is "Asian" the approach in the code above is not recognising race as "Asian" for some reason and so its saving race as "Unknown". So I'm setting the race for those based on the case description.
cleansed_data[cleansed_data$case == "Yountville veterans home shooting", "race"] <- "Asian"
cleansed_data[cleansed_data$case == "San Francisco UPS shooting", "race"] <- "Asian"
cleansed_data[cleansed_data$case == "Oikos University killings", "race"] <- "Asian"
cleansed_data[cleansed_data$case == "Su Jung Health Sauna shooting", "race"] <- "Asian"
cleansed_data[cleansed_data$case == "Binghamton shootings", "race"] <- "Asian"
cleansed_data[cleansed_data$case == "Virginia Tech massacre", "race"] <- "Asian"
cleansed_data[cleansed_data$case == "Xerox killings", "race"] <- "Asian"
cleansed_data[cleansed_data$case == "University of Iowa shooting", "race"] <- "Asian"
# For rows where race is NA, set it to Unknown
cleansed_data[is.na(cleansed_data$race), "race"] <- "Unknown"
# Update the "Weapons Obtained Legally" column so that there is only one version of "Yes" etc
cleansed_data$weapons_obtained_legally = case_when(
cleansed_data$weapons_obtained_legally == "No" ~ "No",
cleansed_data$weapons_obtained_legally == "TBD" ~ "Unknown",
cleansed_data$weapons_obtained_legally == "-" ~ "Unknown",
cleansed_data$weapons_obtained_legally == "Unknown" ~ "Unknown",
cleansed_data$weapons_obtained_legally == "Kelley passed federal criminal background checks; the US Air Force failed to provide information on his criminal history to the FBI" ~ "Unknown",
cleansed_data$weapons_obtained_legally == "Yes" ~ "Yes",
cleansed_data$weapons_obtained_legally == "\nYes" ~ "Yes",
cleansed_data$weapons_obtained_legally == "Yes " ~ "Yes",
cleansed_data$weapons_obtained_legally %!in% c("Yes", "\nYes", "No", "Unknown", "-", "TBD") ~ "Unknown"
)
# Set weapons_obtained_legally to "Yes" for the Chattanooga military recruitment center shooting. The value in the imported data set contains quotes and I haven't figured out how to get the match condition to work with that value yet so this is a temporary workaround.
cleansed_data[cleansed_data$case == "Chattanooga military recruitment center", "weapons_obtained_legally"] <- "Yes"
# Create a new column containing the text after the comma in the location column
cleansed_data$state <- sub('.*\\,', '', cleansed_data$location)
# Strip leading white spaces from state
cleansed_data$state <- sub("^\\s+", "", cleansed_data$state)
# Replace state names with their codes e.g. California becomes CA
cleansed_data$state <- ifelse(cleansed_data$state == "California", "CA", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Pennsylvania", "PA", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Florida", "FL", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Ohio", "OH", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Washington", "WA", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Lousiana", "LA", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Texas", "TX", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Kansas", "KS", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Michigan", "MI", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Colorado", "CO", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Oregon", "OR", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Tennessee", "TN", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "South Carolina", "SC", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Wisconsin", "WI", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "New York", "NY", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Connecticut", "CT", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Minnesota", "MN", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Georgia", "GA", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Nevada", "NV", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Arizona", "AZ", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "North Carolina", "NC", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Kentucky", "KY", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Illinois", "IL", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Missouri", "MO", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Nebraska", "NE", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Virginia", "VA", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Utah", "UT", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Mississippi", "MS", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Massachusetts", "MA", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Hawaii", "HI", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Arkansas", "AR", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Iowa", "IA", cleansed_data$state)
cleansed_data$state <- ifelse(cleansed_data$state == "Oklahoma", "OK", cleansed_data$state)
# Strip leading white spaces from location.1 so that there's only one instance of "Workplace"
cleansed_data$location.1 <- sub("^\\s+", "", cleansed_data$location.1)
# Strip trailing white spaces from location.1 so that there's only one instance of "Other"
cleansed_data$location.1 <- sub("\\s+$", "", cleansed_data$location.1)
# Change "-", "TBD" and "Unclear" to "Unknown"
cleansed_data$prior_signs_mental_health_issues = case_when(
cleansed_data$prior_signs_mental_health_issues == "-" ~ "Unknown",
cleansed_data$prior_signs_mental_health_issues == "No" ~ "No",
cleansed_data$prior_signs_mental_health_issues == "TBD" ~ "Unknown",
cleansed_data$prior_signs_mental_health_issues == "Unclear" ~ "Unknown",
cleansed_data$prior_signs_mental_health_issues == "Unknown" ~ "Unknown",
cleansed_data$prior_signs_mental_health_issues == "Yes" ~ "Yes"
)
# Copy date to a new column called international_date as a character variable (rather than a factor)
cleansed_data$international_date <- as.character.Date(cleansed_data$date) # copy the dates to the new column
# Trim leading and trailing white spaces
cleansed_data$international_date <- trimws(cleansed_data$international_date)
# change /17 at the end of the string to /2017
cleansed_data$international_date <- str_replace(cleansed_data$international_date, "^(.*/)[1]{1}[7]{1}$", "\\12017")
# change /18 at the end of the string to /2018
cleansed_data$international_date <- str_replace(cleansed_data$international_date, "^(.*/)[1]{1}[8]{1}$", "\\12018")
# change /19 at the end of the string to /2019
cleansed_data$international_date <- str_replace(cleansed_data$international_date, "^(.*/)[1]{1}[9]{1}$", "\\12019")
# Change the date from US format (month-day-year) to international date format (year-month-day)
cleansed_data$international_date <- format(as.Date(cleansed_data$international_date, format = "%m/%d/%Y"), "%Y-%m-%d")
# Change international_date to date format
cleansed_data$international_date <- as.Date(cleansed_data$international_date)
```
# Data
This analysis is based on data on US mass shootings 1982 - 2019 provided by Mother Jones.
> ["Our research focused on indiscriminate rampages in public places resulting in four or more victims killed by the attacker. We exclude shootings stemming from more conventionally motivated crimes such as armed robbery or gang violence."](https://www.motherjones.com/politics/2012/07/mass-shootings-map/)
The download is available on [motherjones.com](https://www.motherjones.com/politics/2012/12/mass-shootings-mother-jones-full-data/)
# Key Findings
## Victims By State
```{r create_state_victims_summary, include=FALSE}
# Create a new data frame that shows number of victims by state
victims_by_state <- aggregate(cleansed_data$total_victims, by=list(cleansed_data$state), FUN = sum)
# Change the column heading names to "state" and "victims"
colnames(victims_by_state) <- c("state", "victims")
```
```{r display_plot_state_victims, echo=FALSE, fig.width=9, fig.height=7}
# Create a chart showing the states on a map of the US and shade the states by the number of victims
plot_state_map_victims <- plot_usmap(data = victims_by_state, values = "victims", regions = "states", labels = TRUE, label_color = "white") +
scale_fill_continuous(low = colour_palette[50], high = colour_palette[20], na.value = "white", name = "victims") +
theme(
legend.position = "top",
legend.title = element_text(colour = "#666666", size = 16, vjust = 0.75),
legend.text = element_text(colour = "#666666", size = 8),
plot.margin=unit(c(1,0,1,0),"cm"))
# Display the map chart
plot_state_map_victims
```
**35** of the 50 states (**70%**) had at least one mass shooting between 1982 and 2019.
**Nevada** has had the most victims from mass shootings, due to the 2017 Las Vegas Strip massacre in which there were **604** victims (546 injured, and 58 killed).
## Shootings By State
```{r create_state_summary, include=FALSE}
# Create a new data frame that shows number of shootings by state
shootings_per_state <- data.frame(table(cleansed_data$state))
# Change the column heading names to "state" and "shootings"
colnames(shootings_per_state) <- c("state", "shootings")
```
```{r display_plot_state, echo=FALSE, fig.width=10, fig.height=5}
# Create a treemap chart of the shootings per state
treemap_state_shootings <- ggplot(shootings_per_state, aes(area = shootings, label = state)) +
theme(
legend.position = "top",
legend.title = element_text(colour = "#666666", size = 16),
legend.text = element_text(colour = "#666666", size = 16))+
geom_treemap(aes(alpha = shootings), fill = colour_palette[20]) +
geom_treemap_text(colour = "white", place = "centre",
grow = TRUE)
# Display the treemap chart of shootings per state
treemap_state_shootings
```
**California** has had the most mass shootings (**19**).
## Shootings By Gender
```{r create_gender_summary, include=FALSE}
# Create a new data frame that shows number of shootings per gender
shootings_per_gender <- data.frame(table(cleansed_data$gender))
# Change the column heading names to "gender" and "shootings"
colnames(shootings_per_gender) <- c("gender", "shootings")
```
### 96%
of mass shooters were **men**
## Shootings By Year
```{r create_year_summary, include=FALSE}
# Create a new data frame that shows number of shootings per year
shootings_per_year <- data.frame(table(cleansed_data$year))
# Change the column heading names to "year" and "shootings"
colnames(shootings_per_year) <- c("year", "shootings")
```
```{r display_plot_year, echo=FALSE, fig.height=5, fig.width=10}
# Create a line graph showing the number of mass shootings per year
my_line_plot <- ggplot(data=shootings_per_year, aes(x=year, y=shootings, group=1)) +
geom_line(color="#FB825F", size = 2)+
geom_point(color="#FB825F")+
labs(x = "Year",
y = "Number of mass shootings")+
geom_text(data=subset(shootings_per_year, year == "1984" | year == "1989" | year == "1991"| year == "1993" | year == "1999" | year == "2012" | year == "2017" | year == "2018"), aes(label=shootings), vjust = -1, size = 6, col = "#666666", fontface = "bold")+
theme(
axis.text.y = element_blank(),
axis.text.x = element_text(colour = "#666666", size = 18, angle = 90, vjust = 0.5),
axis.ticks = element_blank(),
axis.title.y = element_text(colour = "#666666", size = 18),
axis.title.x = element_blank(),
axis.line.x = element_line(colour = "#666666"),
axis.line.y = element_line(colour = "#666666"),
panel.background = element_blank(), # Remove the grey background
panel.grid.major = element_line(colour = "#e0e0e0"), # Add the major grid lines back in
panel.grid.minor = element_line(colour = "#e0e0e0"), # Add the minor grid lines back in
plot.margin=unit(c(1,0,1,0),"cm"))+
geom_smooth(method = "loess", colour = "#666666", linetype = 2, size = 0.5 )+
coord_cartesian(ylim=c(0,15))
# Display the line plot
my_line_plot
```
The number of mass shootings per year in the US is **increasing**.
The year with the most shootings was **2018** which consisted of **11%** of the mass shootings.
## Shootings Per State Over Time
```{r create_year_state_summary, include=FALSE}
# Create a new data frame that shows number of shootings per year per state
shootings_per_year_state <- aggregate(cleansed_data$for_count, by=list(cleansed_data$year, cleansed_data$state),FUN = sum)
# Change the column heading names to "year", "state", "shootings"
colnames(shootings_per_year_state) <- c("year", "state", "shootings")
```
```{r animated_plot, echo=FALSE, fig.height=7, fig.width=10}
animated_plot <- ggplot(data = shootings_per_year_state, aes(x = state, y = shootings, fill = state)) +
geom_col()+
scale_fill_viridis_d(aesthetics = "fill", option = "A", direction = 1, begin = 0.2, end = 1)+
theme(legend.position = "none",
axis.ticks = element_blank(),
axis.text = element_text(colour = "#666666", size = 8, angle = 90, vjust = 0.5),
axis.title = element_text(colour = "#666666", size = 16),
plot.title = element_text(colour = "#666666", size = 18),
panel.background = element_blank(), # Remove the grey background
panel.grid.major = element_line(colour = "#e0e0e0"), # Add the major grid lines back in
panel.grid.minor = element_line(colour = "#e0e0e0"), # Add the minor grid lines back in)
plot.margin=unit(c(1,0,1,0),"cm"))+
# The gganimate specific bits:
labs(title = "Year: {frame_time}", x = "", y = "Number of Mass Shootings") +
transition_time(year) +
ease_aes('linear')+
enter_grow()+
exit_shrink()
# Display the animated plot and set the duration
animated_shootings_gif <- animate(animated_plot, duration = 20)
# Display the animated plot
animated_shootings_gif
# Save the animation as a gif
# anim_save('animated_shootings.gif', animation = animated_shootings_gif) # This saves a file but its a bunch of static frames rather than an animated gif. Need to look into this.
```
## Shootings By Decade
```{r create_decade_column, include=FALSE}
# Create a new column called decade and populate it with the decade of each shooting
cleansed_data$decade <- case_when(
cleansed_data$year < "1990" ~ "1980-1989",
cleansed_data$year > "1989" & cleansed_data$year < "2000" ~"1990-1999",
cleansed_data$year > "1999" & cleansed_data$year < "2010" ~"2000-2009",
cleansed_data$year > "2009" & cleansed_data$year < "2020" ~"2010-2019"
)
```
```{r create_decade_summary, include=FALSE}
# Create a new data frame that shows number of shootings per decade
shootings_per_decade <- data.frame(table(cleansed_data$decade))
# Change the column heading names to "decade" and "shootings"
colnames(shootings_per_decade) <- c("decade", "shootings")
```
```{r display_plot_decade, echo=FALSE, fig.height=5, fig.width=10}
# Create a bar chart using ggplot2 to show shootings by decade
plot_decade <- ggplot(shootings_per_decade, aes(y=shootings, x=decade)) +
geom_bar(position="dodge", stat="identity", fill = colour_palette[47]) +
labs(x = "Decade",
y = "Number of mass shootings") +
geom_text(aes(label=shootings), vjust = -0.5, size = 8, col = "#666666", fontface = "bold") +
theme(
axis.text.y = element_blank(),
axis.text.x = element_text(colour = "#666666", size = 22),
axis.ticks = element_blank(),
axis.title.y = element_text(colour = "#666666", size = 18),
axis.title.x = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(colour = "#666666"),
plot.margin=unit(c(1,0,1,0),"cm")) +
coord_cartesian(ylim=c(0,70))
# Display the chart
plot_decade
```
The number of mass shootings this decade is almost **triple** the number in the previous decade.
## Shootings By Race
```{r create_race_summary, include=FALSE}
# Create a new data frame that shows number of shootings per race
shootings_per_race <- data.frame(table(cleansed_data$race))
# Change the column heading names to "race" and "shootings"
colnames(shootings_per_race) <- c("race", "shootings")
```
```{r display_plot_race, echo=FALSE, fig.width=10, fig.height=5}
# Create a lollipop chart using ggplot2 to show number of shootings per race
lollipop_race <- ggplot(shootings_per_race, aes(y=shootings, x=reorder(race, -shootings))) +
geom_segment( aes(x = race, xend=race, y=0, yend=shootings),
color = "#e0e1e2",
size = 2) +
geom_point( size=15,
color=colour_palette[20],
fill=colour_palette[20],
shape=21
) +
labs(y = "Number of mass shootings") +
geom_text(aes(label=shootings),hjust=0.5, vjust=0.5, color = "#FFFFFF", size = 9) +
theme(
axis.text.y = element_blank(),
axis.text.x = element_text(colour = "#666666", size = 14),
axis.ticks = element_blank(),
axis.title.y = element_text(colour = "#666666", size = 18),
axis.title.x = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(colour = "#666666"),
plot.margin=unit(c(1,0,1,0),"cm"))+
coord_cartesian(ylim=c(0,70))
# Display the chart
lollipop_race
```
**More than half** (56%) of mass shooters were **white**.
## Prior Signs of Mental Health Issues
```{r create_prior_summary, include=FALSE}
# Create a new data frame that shows number of shootings per race
shootings_per_prior <- data.frame(table(cleansed_data$prior_signs_mental_health_issues))
# Change the column heading names to "prior_signs_of_mental_health_issues" and "shootings"
colnames(shootings_per_prior) <- c("prior_signs_of_MH_issues", "shootings")
```
```{r display_plot_prior, echo=FALSE, fig.width=10, fig.height=5}
# Create a treemap chart
treemap_prior_shootings <- ggplot(shootings_per_prior, aes(area = shootings, label = prior_signs_of_MH_issues)) +
theme(
legend.position = "top",
legend.title = element_text(colour = "#666666", size = 16),
legend.text = element_text(colour = "#666666", size = 16))+
geom_treemap(aes(alpha = shootings), fill = "#e1ad46") +
geom_treemap_text(colour = "white", place = "centre",
grow = TRUE)
# Display the treemap chart
treemap_prior_shootings
```
**More than half** (53%) of mass shooters had **prior signs** of mental health issues.
Arkansas is the only state where none of their shooters had prior signs of mental health issues, but Arkansas has only had **1** mass shooting and the shooter was only **11 years old**.
## State & Prior Signs of Mental Health Issues
```{r create_state_prior_summary, include=FALSE}
# Create a new data frame that shows number by state and whether there were prior signs of mental health issues
shootings_per_state_prior <- aggregate(cleansed_data$for_count, by=list(cleansed_data$state, cleansed_data$prior_signs_mental_health_issues),FUN = sum)
# Change the column heading names to State, Prior_Signs_Of_Mental_Health_Issues, and Num_Shootings
colnames(shootings_per_state_prior) <- c("State", "Prior_Signs_Of_Mental_Health_Issues", "Num_Shootings")
```
```{r display_plot_state_prior, echo=FALSE, fig.width=10, fig.height=5}
# Create a bar chart using ggplot2 to show shootings by state and whether there were prior signs of mental health issues
plot_state_prior <- ggplot() +
geom_bar(aes(y=Num_Shootings, x=State, fill=Prior_Signs_Of_Mental_Health_Issues), data = shootings_per_state_prior, stat = "identity", position = "fill") +
scale_fill_manual(values = c(colour_palette[60], colour_palette[80], colour_palette[97])) +
labs(x = "State",
y = "% of mass shootings",
fill = "Prior Signs of Mental Health Issues?") +
theme(
legend.position = "top",
legend.title = element_text(colour = "#666666", size = 16, vjust = 0.75),
legend.text = element_text(colour = "#666666", size = 16),
axis.text.y = element_text(colour = "#666666", size = 16),
axis.text.x = element_text(colour = "#666666", size = 7.5),
axis.title = element_text(colour = "#666666", size = 16),
axis.ticks = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(colour = "#666666"),
plot.margin=unit(c(1,0,1,0),"cm"))
# Display the bar chart
plot_state_prior
# Create a heatmap showing the number of mass shootings per state and whether there were prior signs of mental health issues
heatmap_state_prior <- ggplot(shootings_per_state_prior, aes(Prior_Signs_Of_Mental_Health_Issues, State)) +
geom_tile(aes(fill = Num_Shootings), colour = "white") +
scale_fill_gradient(low = "#e892c3", high = "#a51c6a")+
geom_text(aes(label=Num_Shootings), hjust=0.5, vjust=0.5, color = "#FFFFFF", size = 4) +
labs(x = "Prior Signs of Mental Health Issues", fill = "Number of Shootings")+
theme(
legend.position = "top",
legend.title = element_text(colour = "#666666", size = 16, vjust = 0.75),
legend.text = element_text(colour = "#666666", size = 8),
axis.text.y = element_text(colour = "#666666", size = 16),
axis.text.x = element_text(colour = "#666666", size = 8),
axis.ticks = element_blank(),
axis.title.y = element_text(colour = "#666666", size = 16),
axis.title.x = element_blank(),
panel.background = element_rect(fill = "#FFFFFF", color = "#666666", size = 1, linetype = "solid"),
plot.margin=unit(c(1,0,1,0),"cm"))+
coord_flip() +
scale_x_discrete(limits = rev(levels(as.factor(shootings_per_state_prior$Prior_Signs_Of_Mental_Health_Issues))))
# Display heatmap of shootings per state and whether there were prior signs of mental health issues
heatmap_state_prior
```
In most of the 35 states, **most** of the mass shooters had **prior signs** of mental health issues.
Only 2 of the states (**5%**) have had more mass shootings with **no prior signs** of mental health issues.
## Were Weapons Obtained Legally?
```{r create_legally_summary, include=FALSE}
# Create a new data frame that shows number of shootings based on whether the weapons were obtained legally or not
shootings_per_legally <- data.frame(table(cleansed_data$weapons_obtained_legally))
# Change the column heading names to "legally" and "shootings"
colnames(shootings_per_legally) <- c("weapons_obtained_legally", "shootings")
```
```{r display_plot_legally, echo=FALSE, fig.width=10, fig.height=5}
# Create a treemap chart for shootings involving legal and illegal weapons
treemap_legally_shootings <- ggplot(shootings_per_legally, aes(area = shootings, label = weapons_obtained_legally)) +
theme(
legend.position = "top",
legend.title = element_text(colour = "#666666", size = 16),
legend.text = element_text(colour = "#666666", size = 16))+
geom_treemap(aes(alpha = shootings), fill = colour_palette[30]) +
geom_treemap_text(colour = "white", place = "centre",
grow = TRUE)
# Display the treemap chart
treemap_legally_shootings
```
**72%** of mass shooters obtained weapons **legally**.
## Shootings By Location Type
```{r create_loc_type_summary, include=FALSE}
# Create a new data frame that shows number of shootings by location type
shootings_per_loc_type <- data.frame(table(cleansed_data$location.1))
# Change the column heading names to "location.1" and "shootings"
colnames(shootings_per_loc_type) <- c("location.1", "shootings")
```
```{r display_plot_loc_type, echo=FALSE, fig.width=10, fig.height=5}
# Create a lollipop chart using ggplot2 to show number of shootings per location type
lollipop_loc_type <- ggplot(shootings_per_loc_type, aes(y = shootings, x=location.1)) +
geom_segment( aes(x = location.1, xend=location.1, y=0, yend=shootings),
color = "#e0e1e2",
size = 2) +
geom_point( size=15,
color="#e1ad46",
fill="#e1ad46",
shape=21
) +
labs(y = "Number of mass shootings") +
geom_text(aes(label=shootings),hjust=0.5, vjust=0.5, color = "#FFFFFF", size = 9) +
theme(
axis.text.y = element_blank(),
axis.text.x = element_text(colour = "#666666", size = 22),
axis.ticks = element_blank(),
axis.title.y = element_text(colour = "#666666", size = 18),
axis.title.x = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(colour = "#666666"),
plot.margin=unit(c(1,0,1,0),"cm"))+
coord_cartesian(ylim=c(0,50))
# Display the chart
lollipop_loc_type
```
A **third** of US mass shootings occurred in a **workplace**.
**15%** occurred in a **school**.
## Location Type & Race
```{r create_loc_type_race_summary, include=FALSE}
# Create a new data frame that shows number by location type and race
shootings_per_loc_type_race <- aggregate(cleansed_data$for_count, by=list(cleansed_data$location.1, cleansed_data$race),FUN = sum)
# Change the column heading names to Location_Type, Race and Num_Shootings
colnames(shootings_per_loc_type_race) <- c("Location_Type", "Race", "Num_Shootings")
```
```{r display_plot_loc_type_race, echo=FALSE, fig.width=10, fig.height=5}
# Create a bar chart using ggplot2 to show shootings by location type and race
plot_loc_type_race <- ggplot() +
geom_bar(aes(y=Num_Shootings, x=Location_Type, fill=Race), data = shootings_per_loc_type_race, stat = "identity", position = "fill") +
scale_fill_manual(values = c(colour_palette[20], colour_palette[45], colour_palette[60], colour_palette[75], colour_palette[85], colour_palette[95])) +
labs(x = "Location Type",
y = "% of mass shootings",
fill = "Race") +
theme(
legend.position = "top",
legend.title = element_text(colour = "#666666", size = 16),
legend.text = element_text(colour = "#666666", size = 16),
axis.text.y = element_blank(),
axis.text.x = element_text(colour = "#666666", size = 16),
axis.title = element_text(colour = "#666666", size = 16),
axis.ticks = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(colour = "#666666"),
plot.margin=unit(c(1,0,1,0),"cm"))
# Display the bar chart
plot_loc_type_race
```
**All** 6 of the mass shootings in a **religious** place involved a shooter who was **white**
**Over half** of mass shootings in a **school** involved a shooter who was **white**.
## Age Of Shooter
```{r create_age_summary, include=FALSE}
# Create a new data frame that shows number of shootings by age
shootings_per_age <- data.frame(table(cleansed_data$age_of_shooter))
# Change the column heading names to "age" and "shootings"
colnames(shootings_per_age) <- c("age_of_shooter", "shootings")
```
```{r display_plot_age, echo=FALSE, fig.width=10, fig.height=5}
# Create a bar chart using ggplot2 to show shootings by age
plot_age <- ggplot(shootings_per_age, aes(y=shootings, x=age_of_shooter)) +
geom_bar(position="dodge", stat="identity", fill = colour_palette[45]) +
labs(x = "Age",
y = "Number of mass shootings") +
geom_text(aes(label=shootings), vjust = -0.5, size = 6, col = "#666666", fontface = "bold") +
theme(
axis.text.y = element_blank(),
axis.text.x = element_text(colour = "#666666", size = 12),
axis.ticks = element_blank(),
axis.title.y = element_text(colour = "#666666", size = 18),
axis.title.x = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(colour = "#666666"),
plot.margin=unit(c(1,0,1,0),"cm")) +
coord_cartesian(ylim=c(0,8))
# Display the bar chart
plot_age
```
Shooter ages ranged from **11** to **66**.
## Age Bracket Of Shooter
```{r create_age_bracket_column, include=FALSE}
# Create a new column called age_bracket and populate it with the age bracket of each shooter.
cleansed_data$age_bracket <- case_when(
cleansed_data$age_of_shooter <= 12 ~ "1-12",
cleansed_data$age_of_shooter > 12 & cleansed_data$age_of_shooter < 20 ~ "13-19",
cleansed_data$age_of_shooter >= 20 & cleansed_data$age_of_shooter < 30 ~ "20s",
cleansed_data$age_of_shooter >= 30 & cleansed_data$age_of_shooter < 40 ~ "30s",
cleansed_data$age_of_shooter >= 40 & cleansed_data$age_of_shooter < 50 ~ "40s",
cleansed_data$age_of_shooter >= 50 & cleansed_data$age_of_shooter < 60 ~ "50s",
cleansed_data$age_of_shooter >= 60 & cleansed_data$age_of_shooter < 70 ~ "60s"
)
```
```{r create_age_bracket_summary, include=FALSE}
# Create a new data frame that shows number of shootings by age bracket
shootings_per_age_bracket <- data.frame(table(cleansed_data$age_bracket))
# Change the column heading names to "age bracket" and "shootings"
colnames(shootings_per_age_bracket) <- c("age_bracket", "shootings")
```
```{r display_plot_age_bracket, echo=FALSE, fig.width=10, fig.height=5}
# Create a bar chart using ggplot2 to show shootings by age bracket
plot_age_bracket <- ggplot(shootings_per_age_bracket, aes(y=shootings, x=age_bracket)) +
geom_bar(position="dodge", stat="identity", fill = colour_palette[30]) +
labs(x = "Age bracket",
y = "Number of mass shootings") +
geom_text(aes(label=shootings), vjust = -0.5, size = 8, col = "#666666", fontface = "bold") +
theme(
axis.text.y = element_blank(),
axis.text.x = element_text(colour = "#666666", size = 22),
axis.ticks = element_blank(),
axis.title.y = element_text(colour = "#666666", size = 18),
axis.title.x = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(colour = "#666666"),
plot.margin=unit(c(1,0,1,0),"cm")) +
coord_cartesian(ylim=c(0,40))
# Display the bar chart
plot_age_bracket
```
A **third** of mass shooters were in their **20s** (34% of mass shootings).
## Year & Age Bracket
```{r create_year_age_summary, include=FALSE}
# Create a new data frame that shows number by year and age bracket
shootings_per_year_age <- aggregate(cleansed_data$for_count, by=list(cleansed_data$year, cleansed_data$age_bracket),FUN = sum)
# Change the column heading names to Year, Age_Bracket and Num_Shootings
colnames(shootings_per_year_age) <- c("Year", "Age_Bracket", "Num_Shootings")
```
```{r display_plot_year_age, echo=FALSE, fig.width=10, fig.height=5}
without_child_age_bracket <- shootings_per_year_age[shootings_per_year_age$Age_Bracket != "1-12", ]
# Create a faceted line graph showing the number of mass shootings per year for each age bracket
faceted_line_graph_year_age <- ggplot(data=without_child_age_bracket, aes(x=Year, y = Num_Shootings, colour = Age_Bracket, group=1)) +
labs(x = "Year", y="Number of Mass Shootings")+
geom_line(size = 2)+
geom_point()+
scale_color_viridis_d(option = "magma", begin = 0.1, end = 0.85) +
geom_smooth(method = "lm", colour = "#666666", linetype = 2, size = 0.5 )+
theme(legend.position = "none", # Remove the legend
strip.text.x = element_text(size = 22, colour = "#666666"), # Change the size of the facet titles
axis.text = element_text(size = 18, colour = "#666666"), # Change the size of the axis numbers/years
axis.title = element_text(size = 18, colour = "#666666"),
panel.background = element_blank(), # Remove the grey background
panel.grid.major = element_line(colour = "#e0e0e0"), # Add the major grid lines back in
panel.grid.minor = element_line(colour = "#e0e0e0"),
plot.margin=unit(c(1,0,1,0),"cm"))+ # Add the minor grid lines back in)
facet_wrap(~Age_Bracket)
# Display the faceted line graph
faceted_line_graph_year_age
```
There is a noticeable **increase** in recent years in the number of mass shooters in their **twenties**.
Note: The age bracket "1-12" has been excluded from the charts because there was only one instance in that category and so there is little use trying to illustrate the pattern over time for that age bracket.
## Semi-Automatic Weapons
```{r create_semi_auto_used_column, include=FALSE}
# Create a new column called semi_auto_used and populate it with Yes or No depending on whether the shooter used a semi-automatic weapon
cleansed_data$semi_auto_used <- "No"
cleansed_data[grep("automatic", cleansed_data$weapon_type, ignore.case = TRUE), "semi_auto_used"] <- "Yes"
```
```{r create_semi_summary, include=FALSE}
# Create a new data frame that shows number of shootings by whether semi automatic weapons were used
shootings_per_semi <- data.frame(table(cleansed_data$semi_auto_used))
# Change the column heading names to "semi automatic weapon used" and "shootings"
colnames(shootings_per_semi) <- c("semi_auto_used", "shootings")
# Add a Percentage column containing the % of shootings for each category (yes, no)
shootings_per_semi$percentage <- round(shootings_per_semi$shootings / sum(shootings_per_semi$shootings) * 100, 0)
```
```{r display_plot_semi_auto_used, echo=FALSE, fig.width=10, fig.height=2}
# Create a waffle chart
semi_auto_waffle <- waffle(c('Yes'=65, 'No'=35), rows = 4, colors = c('Yes'= colour_palette[70], 'No'=colour_palette[95]))
# Display the waffle chart
semi_auto_waffle
```
Almost **two thirds** (65%) of mass shootings involved **semi-automatic weapons**.
## State & Semi-Automatic Weapons
```{r create_state_semi_summary, include=FALSE}
# Create a new data frame that shows number by state and whether semi-automatic weapons were used
shootings_per_state_semi <- aggregate(cleansed_data$for_count, by=list(cleansed_data$state, cleansed_data$semi_auto_used),FUN = sum)
# Change the column heading names to State, Semi_Auto_Weapons_Used, and Num_Shootings
colnames(shootings_per_state_semi) <- c("State", "Semi_Auto_Weapons_Used", "Num_Shootings")
# Change the Num_Shootings values for "No" to negative numbers, so values for "No" are on the bottom and values for "Yes" are on the top
shootings_per_state_semi$Num_Shootings = case_when(
shootings_per_state_semi$Semi_Auto_Weapons_Used == "No" ~ -shootings_per_state_semi$Num_Shootings,
shootings_per_state_semi$Semi_Auto_Weapons_Used == "Yes" ~ shootings_per_state_semi$Num_Shootings
)
```
```{r display_plot_state_semi, echo=FALSE, fig.width=10, fig.height=5}
brks <- seq(-16, 16, 2) # X Axis Breaks
lbls = as.character(c(seq(16, 0, -2), seq(2, 16, 2))) # X Axis Labels
# Create a bar chart using ggplot2 to show shootings by state and whether semi-automatic weapons were used
plot_state_semi <- ggplot() +
geom_bar(aes(y=Num_Shootings, x=State, fill=Semi_Auto_Weapons_Used),
data = shootings_per_state_semi,
stat = "identity") +
scale_y_continuous(breaks = brks, # Breaks
labels = lbls) + # Labels
scale_fill_manual(values = c(colour_palette[95], colour_palette[50])) +
labs(x = "State",
y = "Number of mass shootings",
fill = "Semi-Auto Weapons Used?") +
theme(
legend.position = "top",
legend.title = element_text(colour = "#666666", size = 16),
legend.text = element_text(colour = "#666666", size = 16),
axis.text.y = element_text(colour = "#666666", size = 16),
axis.text.x = element_text(colour = "#666666", size = 8),
axis.title = element_text(colour = "#666666", size = 16),
axis.ticks = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(colour = "#666666"),
plot.margin=unit(c(1,0,1,0),"cm"))+
guides(fill = guide_legend(reverse = TRUE))
# Display the bar chart
plot_state_semi
```
Of the 35 states that have had a mass shooting, 30 (**85%**) have had shootings involving semi-automatic weapons.
For 14 states (**40%** of the states that have had mass shootings), for example Connecticut, **all** their mass shootings have involved semi-automatic weapons.
In the 3 states with the most mass shootings - California, Florida and Texas - more shootings have involved semi-automatic weapons than without.
Only 5 states have not had any mass shootings with semi-automatic weapons.
## Decade & Semi-Automatic Weapons
```{r create_decade_semi_summary, include=FALSE}
# Create a new data frame that shows number by decade and whether semi-automatic weapons were used
shootings_per_decade_semi <- aggregate(cleansed_data$for_count, by=list(cleansed_data$decade, cleansed_data$semi_auto_used),FUN = sum)
# Change the column heading names to Decade, Semi_Auto_Weapons_Used, and Num_Shootings
colnames(shootings_per_decade_semi) <- c("Decade", "Semi_Auto_Weapons_Used", "Num_Shootings")
```
```{r display_plot_decade_semi, echo=FALSE, fig.width=10, fig.height=5}
# Create a bubble chart
bubble_decade_semi <- ggplot(shootings_per_decade_semi, aes(x=Decade, y=Num_Shootings, color = Semi_Auto_Weapons_Used)) +
geom_point(alpha=0.7, size = 10)+
scale_colour_manual(values = c(colour_palette[70], colour_palette[90]))+
labs(x = "Decade",
y = "Number of mass shootings",
colour = "Semi-Auto Weapons Used?") +
theme(
legend.position = "top",
legend.title = element_text(colour = "#666666", size = 16),
legend.text = element_text(colour = "#666666", size = 16),
legend.key = element_rect(fill = NA, colour = NA),
axis.text.y = element_text(colour = "#666666", size = 16),
axis.text.x = element_text(colour = "#666666", size = 16),
axis.title = element_text(colour = "#666666", size = 16),
axis.ticks = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_line(colour = "#e0e0e0"), # Add the major grid lines back in
panel.grid.minor = element_line(colour = "#e0e0e0"),
axis.line.x = element_line(colour = "#666666"),
plot.margin=unit(c(1,0,1,0),"cm"))+
coord_cartesian(ylim=c(0,40))
# Display the bubble chart
bubble_decade_semi
```
In **every decade**, more mass shootings have involved semi-automatic weapons than without.
## Multiple Weapons
```{r create_multiple_weapons_used_column, include=FALSE}
# Create a new column called multiple_weapons_used and populate it with Yes or No depending on whether the shooter used more than one weapon
cleansed_data$multiple_weapons_used <- "No"
cleansed_data[grep("Two", cleansed_data$weapon_type, ignore.case = TRUE), "multiple_weapons_used"] <- "Yes"
cleansed_data[grep("Three", cleansed_data$weapon_type, ignore.case = TRUE), "multiple_weapons_used"] <- "Yes"
cleansed_data[grep("Five", cleansed_data$weapon_type, ignore.case = TRUE), "multiple_weapons_used"] <- "Yes"
cleansed_data[grep(" and ", cleansed_data$weapon_type, ignore.case = TRUE), "multiple_weapons_used"] <- "Yes"
cleansed_data[grep(",", cleansed_data$weapon_type, ignore.case = TRUE), "multiple_weapons_used"] <- "Yes"
cleansed_data[grep(";", cleansed_data$weapon_type, ignore.case = TRUE), "multiple_weapons_used"] <- "Yes"
cleansed_data[grep("rifles", cleansed_data$weapon_type, ignore.case = TRUE), "multiple_weapons_used"] <- "Yes"
```
```{r create_multiple_weapons_summary, include=FALSE}
# Create a new data frame that shows number of shootings by whether multiple weapons were used
shootings_per_multiple <- data.frame(table(cleansed_data$multiple_weapons_used))
# Change the column heading names to "multiple weapon used" and "shootings"
colnames(shootings_per_multiple) <- c("multiple_weapons_used", "shootings")
# Add a Percentage column containing the % of shootings for each category (yes, no)
shootings_per_multiple$percentage <- round(shootings_per_multiple$shootings / sum(shootings_per_multiple$shootings) * 100, 0)
```
```{r display_plot_multiple_weapons_used, echo=FALSE, fig.width=10, fig.height=2}
# Create a waffle chart
multiple_weapons_waffle <- waffle(c('Yes'=53, 'No'=47), rows = 4, colors = c('Yes'= colour_palette[20], 'No'=colour_palette[55]))
# Display the waffle chart
multiple_weapons_waffle
```
Just **over half** (53%) of mass shootings involved **multiple weapons**.