This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(ggbeeswarm) | |
library(gglabeller) | |
library(scales) | |
data <- read_csv("schoolshootingsdata FINAL - data.csv", | |
col_types = cols(date = col_date(format = "%m/%d/%Y"))) | |
tidy.shooter.age <- gather(data, "shooter_i", "shooter.age", c("age_shooter", "shooter 2 age")) %>% | |
filter(is.numeric(shooter.age)) | |
p <- ggplot(tidy.shooter.age, aes(shooter.age)) + | |
geom_dotplot(binwidth=1) + | |
theme_minimal() + | |
ggtitle('Distribution of shooter age') | |
ggsave('shooter_age_histogram.pdf', plot=p, useDingbats=FALSE) | |
theeeeme <- theme_minimal() + theme( | |
axis.text.y=element_blank(), | |
axis.ticks=element_blank(), | |
axis.line.y=element_blank(), | |
panel.grid=element_blank()) | |
ggplot(data, aes(date, 0, size=adjust)) + | |
geom_beeswarm(method = "frowney", groupOnX=FALSE, dodge.width=0.5) + | |
scale_size_area(max_size = 20) + | |
scale_x_date(date_breaks="1 year", labels=date_format("%Y")) + | |
theeeeme | |
ggsave('shooting_dots_timeline.pdf', width=50, height=5, limitsize=FALSE) | |
ggplot(data, aes(date, 0, size=adjust)) + | |
geom_beeswarm(method = "frowney", groupOnX=FALSE, dodge.width=0.5, alpha=0.5) + | |
scale_size_area(max_size = 20) + | |
scale_x_date(date_breaks="1 year", labels=date_format("%Y"), limits=as.Date(c('1998-04-01', '2023-04-01'))) + | |
scale_y_continuous(limits=c(-20, 10)) + | |
coord_polar(direction = -1) + | |
theeeeme | |
ggsave('shooting_timeline.pdf', width=40, height=40) | |
years <- c("1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018") | |
startvalues <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0) | |
start.frame <- data.frame(year=years, sum=startvalues, dayofyear=startvalues, school_name=rep(list(""), 19), stringsAsFactors=FALSE) | |
cumulative <- data %>% | |
mutate(year = format(date, '%Y'), dayofyear=as.numeric(format(date, '%j'))) %>% | |
group_by(year) %>% | |
arrange(date) %>% | |
mutate(sum=cumsum(adjust)) %>% | |
ungroup() %>% | |
select(sum, dayofyear, year, school_name) %>% | |
rbind(start.frame) | |
endofyear <- cumulative %>% | |
group_by(year) %>% | |
summarise(sum=max(sum)) %>% | |
filter(year!=2018) %>% | |
mutate(dayofyear=365, school_name="end of year") | |
cumulative %>% rbind(endofyear) %>% rbind(start.frame) %>% arrange(year) %>% | |
ggplot(aes(dayofyear, sum, group=year)) + | |
geom_step(alpha=0.4) + | |
geom_text(data=endofyear, aes(dayofyear, sum, label=year), hjust = 0, vjust = 0.5) + | |
geom_text() | |
theme_minimal() + | |
ggtitle("Cumulative number of students in school") | |
ggsave('cumulative_adjust.pdf') | |
cumulative.casualties <- data %>% | |
mutate(year = format(date, '%Y'), dayofyear=as.numeric(format(date, '%j'))) %>% | |
group_by(year) %>% | |
arrange(date) %>% | |
mutate(sum=cumsum(ifelse(is.na(casualties), 0, casualties))) %>% | |
ungroup() %>% | |
select(sum, dayofyear, year) %>% | |
rbind(start.frame) | |
endofyear.casualties <- cumulative.casualties %>% | |
group_by(year) %>% | |
summarise(sum=max(sum)) %>% | |
filter(year!=2018) %>% | |
mutate(dayofyear=365) | |
cumulative.casualties %>% rbind(endofyear.casualties) %>% rbind(start.frame) %>% arrange(year) %>% | |
ggplot(aes(dayofyear, sum, group=year)) + | |
geom_step(alpha=0.4) + | |
geom_text(data=endofyear.casualties, aes(dayofyear, sum, label=year), hjust = 0, vjust = 0.5) + | |
theme_minimal() + | |
ggtitle("Cumulative causualties") | |
ggsave('cumulative_causalties.pdf') | |
cumulative.count <- data %>% | |
mutate(year = format(date, '%Y'), dayofyear=as.numeric(format(date, '%j')), one=1) %>% | |
group_by(year) %>% | |
arrange(date) %>% | |
mutate(sum=cumsum(one)) %>% | |
ungroup() %>% | |
select(sum, dayofyear, year) %>% | |
rbind(start.frame) | |
endofyear.count <- cumulative.count %>% | |
group_by(year) %>% | |
summarise(sum=max(sum)) %>% | |
filter(year!=2018) %>% | |
mutate(dayofyear=365) | |
cumulative.count %>% rbind(endofyear.count) %>% rbind(start.frame) %>% arrange(year) %>% | |
ggplot(aes(dayofyear, sum, group=year)) + | |
geom_step(alpha=0.4) + | |
geom_text(data=endofyear.count, aes(dayofyear, sum, label=year), hjust = 0, vjust = 0.5) + | |
theme_minimal() + | |
ggtitle("Cumulative count") | |
ggsave('cumulative_count.pdf') | |
data %>% | |
mutate(year = format(date, '%Y')) %>% | |
ggplot(aes(year)) + geom_bar() | |
ggsave('years.pdf') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment