Skip to content

Instantly share code, notes, and snippets.

@carlislerainey
Last active December 4, 2015 12:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlislerainey/47c413fc0ff3ebfa3d96 to your computer and use it in GitHub Desktop.
Save carlislerainey/47c413fc0ff3ebfa3d96 to your computer and use it in GitHub Desktop.
Downloads and plots data on mass shootings from Mother Jones (http://www.motherjones.com/politics/2012/12/mass-shootings-mother-jones-full-data)
# load packages
library(magrittr)
library(googlesheets)
library(lubridate)
library(dplyr)
library(stringr)
library(tidyr)
library(ggplot2)
library(maps)
library(scales)
# load data from google sheets
u <- "https://docs.google.com/spreadsheets/d/1XV4mZi3gYDgwx5PrLwqqHTUlHkwkV-6uy_yeJh3X46o/edit?usp=sharing"
guns_raw <- gs_url(u) %>%
gs_read()
# clean data
guns <- data.frame(
date = mdy(guns_raw$Date),
year = year(mdy(guns_raw$Date)),
fatalities = guns_raw$Fatalities,
injuries = guns_raw$Injured,
case = str_to_title(guns_raw$Case),
long = guns_raw$longitude,
lat = guns_raw$latitude
)
# calculate cumulative fatalities and injuries
guns <- guns %>%
mutate(total_fatalities = order_by(date, cumsum(fatalities))) %>%
mutate(total_injuries = order_by(date, cumsum(injuries))) %>%
mutate(case = reorder(case, fatalities)) %>%
mutate(decade = cut(year, breaks = c(1980, 1990, 2000, 2010, 2015),
labels = c("1980s", "1990s", "2000s", "2011-2015")))
# plot shootings per year
guns_year <- summarize(group_by(guns, year),
mass_shootings = length(fatalities),
fatalities = sum(fatalities))
ggplot(guns_year, aes(x = year, y = mass_shootings)) +
geom_smooth() +
geom_point() +
theme_bw() +
labs(title = "Mass Shootings Per Year",
x = "Year",
y = "Number of Mass Shootings")
ggsave("shootings_per_year.png", height = 4, width = 6)
# plot fatalities per year
ggplot(guns_year, aes(x = year, y = fatalities)) +
geom_smooth() +
geom_point() +
theme_bw() +
labs(title = "Number of Fatalities from Mass Shootings Per Year",
x = "Year",
y = "Number of Fatalities")
ggsave("fatalities_per_year.png", height = 4, width = 6)
# plot cumulative fatalities and injuries over time
gather(guns, type, total_count, total_injuries, total_fatalities) %>%
mutate(type = plyr::revalue(type, c("total_fatalities" = "fatalities",
"total_injuries" = "injuries"))) %>%
ggplot(aes(x = date, y = total_count, color = type)) +
geom_line() +
scale_color_brewer(type = "qual", palette = 2) +
theme_bw() +
labs(title = "Cumulative Fatalities and Injuries from Mass Shootings Since 1980",
x = "Date",
y = "Cumulative Count",
color = "Type")
ggsave("cumulative.png", height = 4, width = 7)
# plot fatalities and injuries for each shooting
gather(guns, type, count, injuries, fatalities) %>%
ggplot(aes(x = count, y = case, color = type)) +
geom_point() +
scale_color_brewer(type = "qual", palette = 2) +
theme_bw() +
labs(title = "Mass Shooting Events Since 1980",
x = "Count",
y = "Cases",
color = "Type")
ggsave("cases.png", height = 10, width = 8)
# map of shootings
usa <- map_data("state")
gg <- ggplot() +
geom_polygon(data = usa, aes(x = long, y = lat, group = group),
color = "white", fill = "grey95") +
coord_fixed(1.3) +
geom_point(data = subset(guns, long > -140),
aes(x = long, y = lat, size = fatalities, color = injuries),
alpha = 0.7) +
scale_color_gradient(low = "#1b9e77", high = "#d95f02") +
scale_size(range = c(2, 10)) +
labs(title = "Locations of Mass Shootings Since 1980",
color = "Injuries",
size = "Fatalities") +
theme_bw() + theme(axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_blank())
print(gg)
ggsave("map.png", height = 4, width = 7)
# map by decade
gg + facet_wrap(~decade) +
labs(title = "Locations of Mass Shootings Since 1980 by Decade")
ggsave("map_by_decade.png", height = 6, width = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment