Skip to content

Instantly share code, notes, and snippets.

@ehbick01
Created November 28, 2017 21:07
Show Gist options
  • Save ehbick01/dc5fb7717268e63183a313a30a85210e to your computer and use it in GitHub Desktop.
Save ehbick01/dc5fb7717268e63183a313a30a85210e to your computer and use it in GitHub Desktop.
Quick gender analysis of Louisville UCR data
## Load Packages
library(tidyverse)
## Set Theme
theme_set(
theme_bw(base_family = 'Segoe UI', base_size = 12) +
theme(
plot.title = element_text(face = 'bold', hjust = 0),
text = element_text(colour = '#445359'),
panel.background = element_rect('#ffffff'),
strip.background = element_rect('#ffffff', colour = 'white'),
plot.background = element_rect('#ffffff'),
panel.border = element_rect(colour = '#ffffff'),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
legend.background = element_rect('#ffffff'),
legend.title = element_blank(),
legend.position = 'right',
legend.direction = 'vertical',
legend.key = element_blank(),
strip.text = element_text(face = 'bold', size = 10),
axis.text = element_text(face = 'bold', size = 9),
axis.title = element_blank(),
axis.ticks = element_blank()
)
)
## Read in Data
ucr_data <- read_csv("https://data.louisvilleky.gov/sites/default/files/UniformCitationData.csv")
## Manipulate and Plot
crime_data_by_gender <- ucr_data %>%
group_by(PERSONS_SEX,
UCR_DESC) %>%
summarise(total = n()) %>%
spread(key = PERSONS_SEX,
value = total) %>%
mutate(total = rowSums(.[2:5], na.rm = TRUE)) %>%
select(UCR_DESC,
F,
M,
total) %>%
mutate(female_odds = round((F / sum(F, na.rm = TRUE)) / (total / sum(total, na.rm = TRUE)), 2),
male_odds = round((M / sum(M, na.rm = TRUE)) / (total / sum(total, na.rm = TRUE)), 2)) %>%
group_by(UCR_DESC) %>%
summarise(female_odds = mean(female_odds),
male_odds = mean(male_odds)) %>%
mutate(more_likely_gender = case_when(
ifelse(is.na(female_odds), 0, female_odds) > ifelse(is.na(male_odds), 0, male_odds) ~ "female",
TRUE ~ "male"
)) %>%
gather(key = "gender",
value = "log_odds",
-UCR_DESC,
-more_likely_gender) %>%
arrange(gender,
desc(log_odds)) %>%
ggplot(aes(x = reorder(UCR_DESC, log_odds),
y = log_odds,
fill = gender,
group = more_likely_gender)) +
geom_bar(stat = "identity",
position = "dodge") +
facet_wrap(~more_likely_gender,
ncol = 2,
scales = "free") +
theme(axis.text.x = element_text(size = 6,
angle = 90,
hjust = 1),
legend.position = "none")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment