Questions to ask:
Where do people tend to die (urban, rural, mixed)
distance to clinics, pharmacies, hospitals
correlation between deaths and admissions to these distances?
treat$Admissions[is.na(treat$Admissions)] <- 0
gg <- ggplot(treat, aes(y=Admissions, x=FiscalYear))
gg <- gg + geom_bar(stat="identity")
gg <- gg + facet_wrap(~Town, ncol=4, scale="free_x")
#gg <- gg + facet_wrap(~state, scale="free", ncol=5)
#gg <- gg + scale_x_continuous(limits=c(2012,2016), breaks=c(2012,2016),
# labels=c("2012", "2016"))
gg <- gg + labs(x=NULL, y=NULL,
title="Opioid Related Treatment Admissions by Town",
subtitle="",
caption="Department of Mental Health and Addiction Services")
gg <- gg + theme_minimal(base_family="Lato Regular")
gg <- gg + theme(panel.grid.major.y=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(plot.title=element_text(family="Lato Black"))
gg
# gg <- ggplot(treat, aes(y=Unduplicated.Clients, x=FiscalYear))
# gg <- gg + geom_bar(stat="identity")
# gg <- gg + facet_wrap(~Town, ncol=4, scale="free_x")
# #gg <- gg + facet_wrap(~state, scale="free", ncol=5)
# #gg <- gg + scale_x_continuous(limits=c(2012,2016), breaks=c(2012,2016),
# # labels=c("2012", "2016"))
# gg <- gg + labs(x=NULL, y=NULL,
# title="Opioid Related Treatment Admissions (unduplicated clients) by Town",
# subtitle="",
# caption="Department of Mental Health and Addiction Services")
# gg <- gg + theme_minimal(base_family="Lato Regular")
# gg <- gg + theme(panel.grid.major.y=element_blank())
# gg <- gg + theme(panel.grid.minor=element_blank())
# gg <- gg + theme(plot.title=element_text(family="Lato Black"))
# gg
treat$Admissions[is.na(treat$Admissions)] <- 0
treat <- ctpopulator(Town, treat)
## [1] "Checking to see if names match..."
treat$per_capita_a <- round(treat$Admissions/treat$pop2013*1000, 2)
gg <- ggplot(treat, aes(y=per_capita_a, x=FiscalYear))
gg <- gg + geom_bar(stat="identity")
gg <- gg + facet_wrap(~Town, ncol=4, scale="free_x")
#gg <- gg + facet_wrap(~state, scale="free", ncol=5)
#gg <- gg + scale_x_continuous(limits=c(2012,2016), breaks=c(2012,2016),
# labels=c("2012", "2016"))
gg <- gg + labs(x=NULL, y=NULL,
title="Opioid Related Treatment Admissions per 1,000 residents by Town",
subtitle="",
caption="Department of Mental Health and Addiction Services")
gg <- gg + theme_minimal(base_family="Lato Regular")
gg <- gg + theme(panel.grid.major.y=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(plot.title=element_text(family="Lato Black"))
gg
town_shape <- readOGR(dsn="maps", layer="ctgeo")
## OGR data source with driver: ESRI Shapefile
## Source: "maps", layer: "ctgeo"
## with 169 features
## It has 6 fields
town_shape_df <- fortify(town_shape, region="NAME10")
names(treat)[names(treat) == 'Town'] <- 'id'
treat$id <- str_to_title(treat$id)
town_map <- left_join(town_shape_df, treat)
gg <- ggplot(town_map, aes(long,lat, group=group, fill=per_capita_a))
gg <- gg + geom_polygon()
gg <- gg + geom_path(color = "grey73")
gg <- gg + coord_equal()
gg <- gg + facet_wrap(~FiscalYear, ncol=2)
gg <- gg + scale_fill_gradient(low="grey73", high="blue")
gg <- gg + labs(x=NULL, y=NULL, title="Opioid Related Treatment Admissions by town",
subtitle="Per 1,000 residents",
caption="SOURCE: Department of Mental Health and Addiction Services\nAndrew Ba Tran/TrendCT.org")
gg <- gg + theme_bw(base_family="Lato Regular")
gg <- gg + theme(text = element_text(size=16))
gg <- gg + theme(panel.grid.major=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(axis.ticks=element_blank())
gg <- gg + theme(axis.text.x=element_blank())
gg <- gg + theme(axis.text.y=element_blank())
gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Black", size=22))
gg <- gg + theme(plot.subtitle=element_text(face="italic", size=9, margin=margin(b=12)))
gg <- gg + theme(plot.caption=element_text(size=12, margin=margin(t=10, r=80), color="#7a7d7e"))
gg <- gg + theme(plot.margin = unit(c(1, 1, 1, 1), "lines"))
gg
tab1 <- treat %>%
select(id, FiscalYear, per_capita_a) %>%
spread(FiscalYear, per_capita_a) %>%
arrange(-`2016`)
kable(head(tab1, 10))
id | 2012 | 2013 | 2014 | 2015 | 2016 |
---|---|---|---|---|---|
Canaan | 28.75 | 24.39 | 20.91 | 32.23 | 31.36 |
Windham | 16.87 | 20.26 | 22.36 | 27.53 | 25.08 |
Torrington | 10.40 | 13.37 | 16.06 | 19.94 | 23.74 |
New London | 16.09 | 17.40 | 23.27 | 18.92 | 20.41 |
Hartford | 18.17 | 17.59 | 18.60 | 20.34 | 19.12 |
Sharon | 10.47 | 13.00 | 14.08 | 15.53 | 18.06 |
Griswold | 10.29 | 12.54 | 15.30 | 15.72 | 16.97 |
Waterbury | 12.08 | 12.47 | 12.90 | 16.16 | 16.66 |
Hampton | 5.51 | 14.88 | 6.06 | 13.77 | 16.53 |
Sprague | 11.72 | 11.72 | 16.08 | 15.07 | 16.08 |
source("urban_rural_mixed.R")
town_count <- town_count[c("NAME10", "Type", "perc_urban")]
colnames(town_count) <- c("id", "town_type", "perc_urban")
tab2 <- treat %>%
select(id, FiscalYear, Admissions, pop2013, per_capita_a)
tab2_joined <- left_join(tab2, town_count)
gg <- ggplot(tab2_joined, aes(x=town_type, y=Admissions,fill=town_type))
gg <- gg + geom_boxplot()
gg <- gg + facet_wrap(~FiscalYear, ncol=5)
gg <- gg + labs(x=NULL, y="Admissions", title="Overall opioid-related checkins in Connecticut",
subtitle="",
caption="SOURCE: Department of Mental Health and Addiction Services\nAndrew Ba Tran/TrendCT.org")
gg <- gg + theme_bw(base_family="Lato Regular")
gg <- gg + theme(text = element_text(size=16))
gg <- gg + theme(panel.grid.major=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(axis.text.x=element_text(angle=90, vjust=0.4,hjust=1))
#gg <- gg + theme(axis.ticks=element_blank())
#gg <- gg + theme(axis.text.y=element_blank())
gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Black", size=22))
gg <- gg + theme(plot.subtitle=element_text(face="italic", size=9, margin=margin(b=12)))
gg <- gg + theme(plot.caption=element_text(size=12, margin=margin(t=10, r=80), color="#7a7d7e"))
gg <- gg + theme(plot.margin = unit(c(1, 1, 1, 1), "lines"))
gg
gg <- ggplot(tab2_joined, aes(x=town_type, y=per_capita_a,fill=town_type))
gg <- gg + geom_boxplot()
gg <- gg + facet_grid(.~FiscalYear)
gg <- gg + labs(x=NULL, y="Admissions per capita", title="Adjusted opioid-related checkins in Connecticut",
subtitle="Per 1,000 residents",
caption="SOURCE: Department of Mental Health and Addiction Services\nAndrew Ba Tran/TrendCT.org")
gg <- gg + theme_bw(base_family="Lato Regular")
gg <- gg + theme(text = element_text(size=16))
gg <- gg + theme(panel.grid.major=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(axis.text.x=element_text(angle=90, vjust=0.4,hjust=1))
#gg <- gg + theme(axis.ticks=element_blank())
#gg <- gg + theme(axis.text.y=element_blank())
gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Black", size=22))
gg <- gg + theme(plot.subtitle=element_text(face="italic", size=9, margin=margin(b=12)))
gg <- gg + theme(plot.caption=element_text(size=12, margin=margin(t=10, r=80), color="#7a7d7e"))
gg <- gg + theme(plot.margin = unit(c(1, 1, 1, 1), "lines"))
gg
Prepping the data
drug_deaths <- read.csv("https://data.ct.gov/api/views/rybz-nyjw/rows.csv?accessType=DOWNLOAD", stringsAsFactors = F)
# Cleaning up race
drug_deaths$R <- "Unknown"
drug_deaths$R <- ifelse(drug_deaths$Race=="Asian Indian", "Asian", drug_deaths$R)
drug_deaths$R <- ifelse(drug_deaths$Race=="Asian, Other", "Asian", drug_deaths$R)
drug_deaths$R <- ifelse(drug_deaths$Race=="Chinese", "Asian", drug_deaths$R)
drug_deaths$R <- ifelse(drug_deaths$Race=="Hispanic, Black", "Hispanic", drug_deaths$R)
drug_deaths$R <- ifelse(drug_deaths$Race=="Hispanic, White", "Hispanic", drug_deaths$R)
drug_deaths$R <- ifelse(drug_deaths$Race=="Hispanic, Black", "Hispanic", drug_deaths$R)
drug_deaths$R <- ifelse(drug_deaths$Race=="Native American, Other", "Other", drug_deaths$R)
drug_deaths$R <- ifelse(drug_deaths$Race=="", "Unknown", drug_deaths$R)
drug_deaths$R <- ifelse(drug_deaths$Race=="Asian", "Asian", drug_deaths$R)
drug_deaths$R <- ifelse(drug_deaths$Race=="Black", "Black", drug_deaths$R)
drug_deaths$R <- ifelse(drug_deaths$Race=="Hispanic", "Hispanic", drug_deaths$R)
drug_deaths$R <- ifelse(drug_deaths$Race=="White", "White", drug_deaths$R)
# Cleaning up death and residence city locations
drug_deaths <- ctnamecleaner(Death.City, drug_deaths)
## Your file with fixed town names has been exported.
## Unfortunately, no matches were found for 4 They can be found in your folder. The file is called no_matches.csv
names(drug_deaths)[names(drug_deaths) == 'real.town.name'] <- 'town.of.death'
drug_deaths$Residence.City <- gsub(" ", " ", drug_deaths$Residence.City)
drug_deaths <- ctnamecleaner(Residence.City, drug_deaths)
## Your file with fixed town names has been exported.
## Unfortunately, no matches were found for 150 They can be found in your folder. The file is called no_matches.csv
names(drug_deaths)[names(drug_deaths) == 'real.town.name'] <- 'town.of.residence'
drug_deaths$residence.and.death <- ""
drug_deaths$residence.and.death <- ifelse(drug_deaths$town.of.death == drug_deaths$town.of.residence, "Same town of death and residence", "Different town of death than residence")
drug_deaths$residence.and.death <- ifelse(is.na(drug_deaths$town.of.death), "Town of death unknown", drug_deaths$residence.and.death)
drug_deaths$residence.and.death <- ifelse(is.na(drug_deaths$town.of.residence), "Residence unknown", drug_deaths$residence.and.death)
drug_deaths$residence.and.death <- ifelse((is.na(drug_deaths$town.of.residence) & (drug_deaths$Residence.State!="CT")), "Out of town resident, local death", drug_deaths$residence.and.death)
drug_deaths$residence.and.death <- ifelse((is.na(drug_deaths$town.of.residence) & (drug_deaths$Residence.City=="")), "Unknown residence", drug_deaths$residence.and.death)
drug_deaths$date.fixed <- mdy(drug_deaths$Date)
drug_deaths$Year <- year(drug_deaths$date.fixed)
drug_deaths$row <- row.names(drug_deaths)
# fentanyl only
new_fentanyl <- drug_deaths %>%
mutate(cause=str_to_lower(drug_deaths$ImmediateCauseA)) %>%
filter(grepl("fent", cause) | Fentanyl=="Y" )
new_fent <- select(new_fentanyl, row)
new_fent$update_fent <- "Yes"
new_fent <- as.data.frame(new_fent)
drug_deaths <- left_join(drug_deaths, new_fent)
# heroin only
new_heroin <- drug_deaths %>%
mutate(cause=str_to_lower(drug_deaths$ImmediateCauseA)) %>%
filter(grepl("heroin", cause) | Heroin=="Y" )
new_her <- select(new_heroin, row)
new_her$update_heroin <- "Yes"
new_her <- as.data.frame(new_her)
drug_deaths <- left_join(drug_deaths, new_her)
# prescription opioids
new_presc <- drug_deaths %>%
mutate(cause=str_to_lower(drug_deaths$ImmediateCauseA)) %>%
filter(grepl("oxy", cause) | Oxycodone=="Y" | Oxymorphone=="Y" |
grepl("hydro", cause) | Hydrocodone=="Y" |
grepl("tramad", cause) | Tramad=="Y" |
grepl("morphine", cause) | Morphine..not.heroin.=="Y" )
new_pre <- select(new_presc, row)
new_pre$update_presc <- "Yes"
new_pre <- as.data.frame(new_pre)
drug_deaths <- left_join(drug_deaths, new_pre)
#leftover <- subset(drug_deaths, is.na(update_fent) & is.na(update_heroin) & is.na(update_presc))
#ggplot(leftover, aes(Year)) + geom_bar()
# There were about 461 cases that did not involve overdoses of prescription opioids, heroin, or fentanyl
# OK, checkins and deaths by year and town
# Prepping town and year dataframe
towns_only <- data.frame(town_count[,1])
colnames(towns_only) <- "town.of.residence"
towns_only$Year <- 0
for (i in 2012:2016) {
towns_only$Year <- i
if (i == 2012) {
mega_towns <- towns_only
} else {
mega_towns <- rbind(mega_towns, towns_only)
}
}
# First, deaths by town (just opioids)
opioid_deaths <- drug_deaths %>%
filter(update_presc=="Yes") %>%
group_by(town.of.residence, Year) %>%
summarise(overdose.deaths=n())
#
checkins <- tab2
colnames(checkins) <- c("town.of.residence", "Year", "admissions", "pop", "per_capita_a")
test <- left_join(mega_towns, checkins)
test <- left_join(test, opioid_deaths)
cor(test$admissions, test$overdose.deaths, use="complete.obs")
## [1] 0.7008847
cor(test$per_capita_a, test$overdose.deaths, use="complete.obs")
## [1] 0.3995875
test2 <- test
test2$per_capita_o <- round(test2$overdose.deaths/test2$pop*10000,2)
test2$overdose.deaths[is.na(test2$overdose.deaths)] <- 0
cor(test2$admissions, test2$overdose.deaths, use="complete.obs")
## [1] 0.7249749
cor(test2$per_capita_a, test2$overdose.deaths, use="complete.obs")
## [1] 0.36486
cor(test2$per_capita_a, test2$per_capita_o, use="complete.obs")
## [1] 0.02540601
test3 <- test2 %>%
select(town.of.residence, Year, per_capita_a, per_capita_o) %>%
gather("Type", "per.capita", 3:4)
gg <- ggplot(test3, aes(x=Year, y=per.capita, group=Type, color=Type))
gg <- gg + geom_line()
gg <- gg + facet_wrap(~town.of.residence, scales="free", ncol=5)
gg
test4 <- test2 %>%
select(town.of.residence, Year, admissions, overdose.deaths) %>%
gather("Type", "Count", 3:4)
gg <- ggplot(test4, aes(x=Year, y=Count, group=Type, color=Type))
gg <- gg + geom_line()
gg <- gg + facet_wrap(~town.of.residence, scales="free", ncol=5)
gg
opioid_deaths2 <- drug_deaths %>%
filter(update_presc=="Yes") %>%
group_by(town.of.residence) %>%
summarise(overdose.deaths=n())
#
checkins <- tab2
colnames(checkins) <- c("town.of.residence", "Year", "admissions", "pop", "per_capita_a")
checkins2 <- checkins %>%
group_by(town.of.residence) %>%
summarise(admissions=sum(admissions, na.rm=T))
town_totals <- left_join(towns_only, opioid_deaths2)
## Joining, by = "town.of.residence"
## Warning in left_join_impl(x, y, by$x, by$y, suffix$x, suffix$y): joining
## character vector and factor, coercing into character vector
town_totals <- left_join(town_totals, checkins2)
## Joining, by = "town.of.residence"
cor(town_totals$admissions, town_totals$overdose.deaths, use="complete.obs")
## [1] 0.882802
town_totals <- ctpopulator(town.of.residence, town_totals)
## [1] "Checking to see if names match..."
## Joining, by = "name2"
## Warning in left_join_impl(x, y, by$x, by$y, suffix$x, suffix$y): joining
## factor and character vector, coercing into character vector
town_totals$per_cap_o <- round(town_totals$overdose.deaths/town_totals$pop2013*1000,2)
town_totals$per_cap_a <- round(town_totals$admissions/town_totals$pop2013*1000,2)
cor(town_totals$per_cap_a, town_totals$per_cap_o, use="complete.obs")
## [1] 0.2476643
gg <- ggplot(town_totals, aes(x=overdose.deaths, y=admissions))
gg <- gg + geom_point()
gg <- gg + labs(x="Overdoses", y="Admissions",
title="Overdose deaths compared to clinical admissions by town",
subtitle="",
caption="Department of Mental Health and Addiction Services")
gg
## Warning: Removed 45 rows containing missing values (geom_point).
gg <- ggplot(town_totals, aes(x=per_cap_o, y=per_cap_a))
gg <- gg + geom_point()
gg <- gg + labs(x="Overdoses per capita", y="Admissions per capita",
title="Overdose deaths compared to clinical admissions by town (per 1,000 residents)",
subtitle="",
caption="Department of Mental Health and Addiction Services")
gg
## Warning: Removed 45 rows containing missing values (geom_point).
data2014 <- getCensus(name="acs5",
vintage=2014,
key=census_key,
vars=c("NAME", "B19013_001E"),
region="county subdivision:*", regionin="state:09")
data2014$town.of.residence <- gsub(" town.*", "", data2014$NAME)
data2014 <- subset(data2014, !is.na(B19013_001E))
data2014 <- data2014[,5:6]
colnames(data2014) <- c("median.income", "town.of.residence")
town_totals2 <- town_totals
town_totals2$town.of.residence <- str_to_title(town_totals2$town.of.residence)
town_totals2 <- left_join(town_totals2, data2014)
## Joining, by = "town.of.residence"
cor(town_totals2$per_cap_a, town_totals2$median.income, use="complete.obs")
## [1] -0.5560087
gg <- ggplot(town_totals2, aes(x=median.income, y=per_cap_a))
gg <- gg + geom_point()
gg <- gg + labs(x="Median income", y="Admissions per capita",
title="Clinical admissions by town (per 1,000 residents) compared to median income",
subtitle="",
caption="Department of Mental Health and Addiction Services")
gg
cor(town_totals2$per_cap_o, town_totals2$median.income, use="complete.obs")
## [1] -0.2370663
gg <- ggplot(town_totals2, aes(x=median.income, y=per_cap_o))
gg <- gg + geom_point()
gg <- gg + labs(x="Median income", y="Overdoses per capita",
title="Opioid overdoses by town (per 1,000 residents) compared to median income",
subtitle="",
caption="Department of Mental Health and Addiction Services")
gg
## Warning: Removed 45 rows containing missing values (geom_point).
tab3_joined <- town_count
colnames(tab3_joined) <- c("town.of.residence", "town_type", "perc_urban")
tab3_joined <- left_join(test2, tab3_joined)
gg <- ggplot(tab3_joined, aes(x=town_type, y=per_capita_o, group=town_type, fill=town_type))
gg <- gg + geom_boxplot()
gg <- gg + facet_grid(.~Year)
gg <- gg + labs(x=NULL, y="Overdoses per capita", title="Adjusted opioid-related overdoses in Connecticut",
subtitle="Per 1,000 residents",
caption="SOURCE: Department of Mental Health and Addiction Services\nAndrew Ba Tran/TrendCT.org")
gg <- gg + theme_bw(base_family="Lato Regular")
gg <- gg + theme(text = element_text(size=16))
gg <- gg + theme(panel.grid.major=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(axis.text.x=element_text(angle=90, vjust=0.4,hjust=1))
#gg <- gg + theme(axis.ticks=element_blank())
#gg <- gg + theme(axis.text.y=element_blank())
gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Black", size=22))
gg <- gg + theme(plot.subtitle=element_text(face="italic", size=9, margin=margin(b=12)))
gg <- gg + theme(plot.caption=element_text(size=12, margin=margin(t=10, r=80), color="#7a7d7e"))
gg <- gg + theme(plot.margin = unit(c(1, 1, 1, 1), "lines"))
gg
gg <- ggplot(tab3_joined, aes(x=per_capita_a, y=per_capita_o, group=town_type, color=town_type))
gg <- gg + geom_point()
gg <- gg + facet_wrap(~Year, ncol=2)
gg <- gg + labs(x="Admissions per capita", y="Overdoses per capita", title="Opioid-related overdoses compared to treatment admissions",
subtitle="Per 1,000 residents",
caption="SOURCE: Department of Mental Health and Addiction Services\nAndrew Ba Tran/TrendCT.org")
gg <- gg + theme_bw(base_family="Lato Regular")
gg <- gg + theme(text = element_text(size=16))
gg <- gg + theme(panel.grid.major=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(axis.text.x=element_text(angle=90, vjust=0.4,hjust=1))
#gg <- gg + theme(axis.ticks=element_blank())
#gg <- gg + theme(axis.text.y=element_blank())
gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Black", size=22))
gg <- gg + theme(plot.subtitle=element_text(face="italic", size=9, margin=margin(b=12)))
gg <- gg + theme(plot.caption=element_text(size=12, margin=margin(t=10, r=80), color="#7a7d7e"))
gg <- gg + theme(plot.margin = unit(c(1, 1, 1, 1), "lines"))
gg
## Warning: Removed 523 rows containing missing values (geom_point).
combined <- tab3_joined %>%
group_by(town.of.residence, town_type) %>%
summarise(admissions=sum(admissions, na.rm=T), pop=mean(pop), overdose.deaths=sum(overdose.deaths, na.rm=T)) %>%
mutate(per_capita_a=round(admissions/pop*1000,2), per_capita_o=round(overdose.deaths/pop*1000,2))
gg <- ggplot(combined, aes(x=per_capita_a, y=per_capita_o, group=town_type, color=town_type))
gg <- gg + geom_point()
gg <- gg + labs(x="Admissions per capita", y="Overdoses per capita", title="Opioid-related overdoses compared to treatment admissions",
subtitle="Per 1,000 residents",
caption="SOURCE: Department of Mental Health and Addiction Services\nAndrew Ba Tran/TrendCT.org")
gg <- gg + theme_bw(base_family="Lato Regular")
gg <- gg + theme(text = element_text(size=16))
gg <- gg + theme(panel.grid.major=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(axis.text.x=element_text(angle=90, vjust=0.4,hjust=1))
#gg <- gg + theme(axis.ticks=element_blank())
#gg <- gg + theme(axis.text.y=element_blank())
gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Black", size=22))
gg <- gg + theme(plot.subtitle=element_text(face="italic", size=9, margin=margin(b=12)))
gg <- gg + theme(plot.caption=element_text(size=12, margin=margin(t=10, r=80), color="#7a7d7e"))
gg <- gg + theme(plot.margin = unit(c(1, 1, 1, 1), "lines"))
gg
town_count_merge <- town_count
colnames(town_count_merge) <- c("town.of.residence", "town_type", "perc_urban")
drug_deaths_mega <- left_join(drug_deaths, town_count_merge)
## Joining, by = "town.of.residence"
## Warning in left_join_impl(x, y, by$x, by$y, suffix$x, suffix$y): joining
## factor and character vector, coercing into character vector
kable(table(drug_deaths_mega$InjuryPlace, drug_deaths_mega$town_type))
Mixed | Rural | Urban | |
---|---|---|---|
36 | 0 | 13 | |
Alleyway | 0 | 0 | 0 |
Apartment | 4 | 0 | 1 |
Apartment House | 1 | 0 | 0 |
Automobile | 13 | 0 | 4 |
Bar or Night Club | 1 | 0 | 0 |
Beach | 2 | 0 | 0 |
Boat/Marina | 1 | 0 | 0 |
Campgrounds | 0 | 1 | 0 |
car in hartford | 0 | 0 | 1 |
Casino | 1 | 0 | 0 |
Convenience Store | 0 | 0 | 1 |
Detoxification Center | 0 | 0 | 1 |
Driveway | 3 | 0 | 0 |
Field | 1 | 0 | 1 |
Forest, Wooded Area | 0 | 0 | 1 |
Friend’s Residence | 1 | 0 | 0 |
Garage | 0 | 0 | 0 |
Halfway House | 1 | 0 | 1 |
homeless shelter | 1 | 0 | 0 |
Hospital | 43 | 1 | 13 |
Hospital or Emergency Room | 2 | 0 | 2 |
Hotel or Motel | 51 | 0 | 13 |
House | 3 | 0 | 1 |
In Vehicle | 13 | 0 | 4 |
Multiple Locations | 1 | 0 | 0 |
Nursing Home | 0 | 0 | 3 |
Office Building | 1 | 0 | 1 |
Other | 122 | 3 | 44 |
Other (unknown) | 5 | 0 | 2 |
Other indoor Area | 4 | 0 | 3 |
Other, Farm or Ranch | 1 | 0 | 0 |
Other, Industrial Area | 1 | 0 | 0 |
Other, Other Outdoor Area | 7 | 0 | 7 |
Other, Park or Recreational Area | 2 | 0 | 2 |
Other, Public Buildings | 1 | 0 | 0 |
Other, Residential Institution | 1 | 0 | 0 |
Parking Lot | 15 | 0 | 6 |
Porch | 1 | 0 | 1 |
Public Park | 1 | 0 | 0 |
Public Park, Urban or Suburban | 1 | 0 | 0 |
Rehab House | 1 | 0 | 0 |
Residence | 1308 | 30 | 446 |
Residential Building | 58 | 1 | 22 |
Rest Home | 0 | 0 | 0 |
Restaurant | 6 | 0 | 2 |
Roadway | 3 | 0 | 0 |
School, Primary or Secondary | 1 | 0 | 0 |
Sidewalk | 1 | 0 | 1 |
Store or Shopping Area | 2 | 0 | 0 |
Street | 2 | 0 | 2 |
Train or Subway Station | 2 | 0 | 0 |
Unknown | 43 | 1 | 17 |
drug_deaths_mega$Location2 <- drug_deaths_mega$Location
drug_deaths_mega$Location2 <- gsub("Convalescent Home", "Other", drug_deaths_mega$Location2)
drug_deaths_mega$Location2[is.na(drug_deaths_mega$Location2)] <- "Other"
kable(table(drug_deaths_mega$Location2, drug_deaths_mega$town_type))
Mixed | Rural | Urban | |
---|---|---|---|
13 | 1 | 1 | |
Hospital | 582 | 13 | 217 |
Other | 206 | 2 | 72 |
Residence | 968 | 21 | 326 |
percent_type <- drug_deaths_mega %>%
group_by(Location2, town_type) %>%
summarise(total=n()) %>%
mutate(percent=round(total/sum(total, na.rm=T)*100,2)) %>%
select(Location2, town_type, percent) %>%
spread(town_type, percent)
kable(percent_type)
Location2 | Mixed | Rural | Urban | |
---|---|---|---|---|
72.22 | 5.56 | 5.56 | 16.67 | |
Hospital | 67.36 | 1.50 | 25.12 | 6.02 |
Other | 56.28 | 0.55 | 19.67 | 23.50 |
Residence | 73.11 | 1.59 | 24.62 | 0.68 |
percent_type2 <- drug_deaths_mega %>%
group_by(town_type, Location2) %>%
summarise(total=n()) %>%
mutate(percent=round(total/sum(total, na.rm=T)*100,2)) %>%
select(town_type, Location2, percent) %>%
spread(town_type, percent)
kable(percent_type2)
Location2 | Mixed | Rural | Urban | |
---|---|---|---|---|
0.73 | 2.70 | 0.16 | 2.00 | |
Hospital | 32.90 | 35.14 | 35.23 | 34.67 |
Other | 11.64 | 5.41 | 11.69 | 57.33 |
Residence | 54.72 | 56.76 | 52.92 | 6.00 |
location_info <- drug_deaths_mega %>%
group_by(residence.and.death) %>%
summarise(deaths=n())
gg <- ggplot(location_info, aes(residence.and.death, deaths))
gg <- gg + geom_bar(stat="identity")
gg <- gg + labs(x=NULL, y="Overdoses", title="Overdoses by location type",
subtitle="",
caption="SOURCE: Department of Mental Health and Addiction Services, Medical Examiner's Office\nAndrew Ba Tran/TrendCT.org")
gg <- gg + theme_bw(base_family="Lato Regular")
gg <- gg + theme(text = element_text(size=16))
gg <- gg + theme(panel.grid.major=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(axis.text.x=element_text(angle=90, vjust=0.4,hjust=1))
#gg <- gg + theme(axis.ticks=element_blank())
#gg <- gg + theme(axis.text.y=element_blank())
gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Black", size=22))
gg <- gg + theme(plot.subtitle=element_text(face="italic", size=9, margin=margin(b=12)))
gg <- gg + theme(plot.caption=element_text(size=12, margin=margin(t=10, r=80), color="#7a7d7e"))
gg <- gg + theme(plot.margin = unit(c(1, 1, 1, 1), "lines"))
gg
pharm <- read.csv("https://data.ct.gov/api/views/2vby-9bet/rows.csv?accessType=DOWNLOAD", stringsAsFactors=F)
pharm <- ctnamecleaner(City, pharm)
## Joining, by = "name2"
## [1] "...All names matched. That's a rare thing."
pharm <- pharm %>%
group_by(real.town.name) %>%
summarise(pharmacies=n())
colnames(pharm) <- c("town.of.residence", "pharm")
counselors <- read.csv("https://data.ct.gov/api/views/jixj-wkjd/rows.csv?accessType=DOWNLOAD", stringsAsFactors=F)
counselors <- subset(counselors, State=="CT")
counselors <- ctnamecleaner(City, counselors)
## Joining, by = "name2"
## Your file with fixed town names has been exported.
## Unfortunately, no matches were found for 1 They can be found in your folder. The file is called no_matches.csv
counselors <- counselors %>%
group_by(real.town.name) %>%
summarise(counselors=n())
colnames(counselors) <- c("town.of.residence", "couns")
drop <- read.csv("https://data.ct.gov/api/views/h7ba-v8cg/rows.csv?accessType=DOWNLOAD", stringsAsFactors = F)
drop <- ctnamecleaner(City, drop)
## Joining, by = "name2"
## [1] "...All names matched. That's a rare thing."
drop <- drop %>%
group_by(real.town.name) %>%
summarise(drop=n())
colnames(drop) <- c("town.of.residence", "drop.offs")
combined2 <- left_join(combined, pharm)
## Joining, by = "town.of.residence"
combined2 <- left_join(combined2, counselors)
## Joining, by = "town.of.residence"
combined2 <- left_join(combined2, drop)
## Joining, by = "town.of.residence"
combined2$per_capita_p <- round(combined2$pharm/combined2$pop*10000,2)
combined2$per_capita_c <- round(combined2$couns/combined2$pop*10000,2)
combined2$per_capita_d <- round(combined2$drop.offs/combined2$pop*10000,2)
combined3 <- combined2 %>%
select(town.of.residence, town_type, per_capita_a, per_capita_o, per_capita_p, per_capita_c, per_capita_d) %>%
gather("type", "per_capita", 3:7)
combined3$type <- ifelse(combined3$type=="per_capita_a", "admissions", combined3$type )
combined3$type <- ifelse(combined3$type=="per_capita_o", "overdoses", combined3$type )
combined3$type <- ifelse(combined3$type=="per_capita_p", "pharmacies", combined3$type )
combined3$type <- ifelse(combined3$type=="per_capita_c", "counselors", combined3$type )
combined3$type <- ifelse(combined3$type=="per_capita_d", "drop.offs", combined3$type )
gg <- ggplot(combined3, aes(x=town_type, y=per_capita, group=town_type, fill=town_type))
gg <- gg + geom_boxplot()
gg <- gg + facet_wrap(~type, ncol=2, scales="free")
gg <- gg + labs(x=NULL, y=NULL, title="Connecticut",
subtitle="Per 1,000 residents",
caption="SOURCE: Department of Mental Health and Addiction Services\nAndrew Ba Tran/TrendCT.org")
gg <- gg + theme_bw(base_family="Lato Regular")
gg <- gg + theme(text = element_text(size=16))
gg <- gg + theme(panel.grid.major=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(axis.text.x=element_text(angle=90, vjust=0.4,hjust=1))
#gg <- gg + theme(axis.ticks=element_blank())
#gg <- gg + theme(axis.text.y=element_blank())
gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Black", size=22))
gg <- gg + theme(plot.subtitle=element_text(face="italic", size=9, margin=margin(b=12)))
gg <- gg + theme(plot.caption=element_text(size=12, margin=margin(t=10, r=80), color="#7a7d7e"))
gg <- gg + theme(plot.margin = unit(c(1, 1, 1, 1), "lines"))
gg
## Warning: Removed 176 rows containing non-finite values (stat_boxplot).
combined4 <- combined2 %>%
select(town.of.residence, town_type, admissions, overdose.deaths, pharm, couns, drop.offs) %>%
gather("type", "total", 3:7)
gg <- ggplot(combined4, aes(x=town_type, y=total, group=town_type, fill=town_type))
gg <- gg + geom_boxplot()
gg <- gg + facet_wrap(~type, ncol=2, scales="free")
gg <- gg + labs(x=NULL, y=NULL, title="Connecticut",
subtitle="",
caption="SOURCE: Department of Mental Health and Addiction Services\nAndrew Ba Tran/TrendCT.org")
gg <- gg + theme_bw(base_family="Lato Regular")
gg <- gg + theme(text = element_text(size=16))
gg <- gg + theme(panel.grid.major=element_blank())
gg <- gg + theme(panel.grid.minor=element_blank())
gg <- gg + theme(panel.border=element_blank())
gg <- gg + theme(axis.text.x=element_text(angle=90, vjust=0.4,hjust=1))
#gg <- gg + theme(axis.ticks=element_blank())
#gg <- gg + theme(axis.text.y=element_blank())
gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Black", size=22))
gg <- gg + theme(plot.subtitle=element_text(face="italic", size=9, margin=margin(b=12)))
gg <- gg + theme(plot.caption=element_text(size=12, margin=margin(t=10, r=80), color="#7a7d7e"))
gg <- gg + theme(plot.margin = unit(c(1, 1, 1, 1), "lines"))
gg
## Warning: Removed 176 rows containing non-finite values (stat_boxplot).