Skip to content

Instantly share code, notes, and snippets.

@diegovalle
Created January 8, 2018 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegovalle/26f832fba82b934b3c3edf7b692caebe to your computer and use it in GitHub Desktop.
Save diegovalle/26f832fba82b934b3c3edf7b692caebe to your computer and use it in GitHub Desktop.
pop <- read.csv("../downloader/data/pop_muns.csv") %>% filter(date == '2010-06-01 00:00:00')
mex <- injury.intent %>%
filter(year_reg == 2016 & intent %in% c("Homicide", "Legal Intervention")) %>%
group_by(state_occur_death, mun_occur_death) %>%
rename(state_code = state_occur_death, mun_code = mun_occur_death) %>%
summarise(count = n()) %>%
left_join(read.csv("../downloader/data/pop_muns.csv") %>% filter(date == '2016-06-01 00:00:00'),
by = c("state_code", "mun_code")) %>%
mutate(rate = count / population * 10^5) %>%
mutate(country = 'México') %>%
left_join(read.csv("../downloader/data/municipio_names.csv", fileEncoding="windows-1252"),
by = c("state_code", "mun_code")) %>%
left_join(read.csv("../downloader/data/state_codes.csv", fileEncoding="windows-1252"),
by = c("state_code")) %>%
mutate(County = stringi::stri_trans_totitle((str_c(MUNICIPIO, ", ", state)))) %>%
ungroup() %>%
dplyr::select(population, rate, County, country) %>%
filter(population > 10^5)
#https://wonder.cdc.gov/controller/saved/D76/D27F575
us <- read.csv("~/Downloads/Hispanics-County.txt", sep = "\t", stringsAsFactors = FALSE) %>%
rename(population = Population, rate = Crude.Rate ) %>%
mutate(country = "USA") %>%
dplyr::select(population, rate, County, country) %>%
mutate(rate = as.numeric(rate)) %>%
na.omit() %>%
filter(population > 10^5)
ggplot(bind_rows(us, mex) %>% arrange(-rate) %>% head(150),
aes(reorder(County, rate), rate, fill = country)) +
geom_bar(stat = "identity") +
coord_flip() +
xlab("County") +
theme_bw() +
ggtitle('2016 Homicides and Legal Intervention Deaths by County\nin the US (only Hispanics) and Mexico (Everyone)',
subtitle = 'Only includes counties with more than 100,000 persons or 100,000 White Hispanics\nData Sources: INEGI and CDC Wonder')
ggsave("test.png", dpi = 100, width = 10, height = 18)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment