find the local population rates for 2014, 2015, 2016
Code
#pull the population totals from the tidycensus population_avg <-get_acs(year =2014,geography ="county",state ="CO",variables =c(population_14 ="B01001_001",output ="wide")) %>%select(GEOID, NAME,"pop14"= estimate)
Getting data from the 2010-2014 5-year ACS
Fetching data by table type ("B/C", "S", "DP") and combining the result.
Getting data from the 2012-2016 5-year ACS
Fetching data by table type ("B/C", "S", "DP") and combining the result.
calculate the three-year average population
Code
# add the population estimates to the main doc. # pop 14 is already in therepopulation_avg$pop15 <- population15$estimatepopulation_avg$pop16 <- population16$estimate# find the average population over the time periodpopulation_avg$pop_avg <-rowMeans(population_avg[, c("pop14", "pop15", "pop16")], na.rm =TRUE)population_avg <- population_avg %>%mutate_at("pop_avg", round) # round#use this data to calculate crime rateswrite.csv(population_avg, ("~/gvsu/winter 23/CIS 635/NIMBY/cleaning/crime/populations_avg.csv")) population_avg <- population_avg %>%select(1,2,6) #geo id, name and average
join the population data to the dispensary counts by county GEOID
use the population average to calcuate the count of dispensaries per 10,000 residents
Code
population_avg$GEOID <-as.integer(population_avg$GEOID)population_avg$NAME<-gsub(" County, Colorado", "", population_avg$NAME) # format the name to fit the crimedsps_per_capita <-left_join(population_avg, dsps_avg, by ="GEOID") %>%mutate_at(vars(4), replace_na, replace =0) # remove NAs# calculate the dispensary per 10,000 population ratedsps_per_capita$dps_per_cap <- (dsps_per_capita$dsps_average / dsps_per_capita$pop_avg) *10000dsps_per_capita <- dsps_per_capita %>%mutate_at(vars(starts_with("dps")), funs(round(., 1))) # round
Warning: `funs()` was deprecated in dplyr 0.8.0.
ℹ Please use a list of either functions or lambdas:
# Simple named list: list(mean = mean, median = median)
# Auto named with `tibble::lst()`: tibble::lst(mean, median)
# Using lambdas list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
pull in the fbi crime data same as the dispensary
same as the “database” section minus adjusting for naming conventions. although crime data gets a bad rap, at least it was uniform.
filter to get 2016 data, and pivot, summarize by year and geography
Warning: `funs()` was deprecated in dplyr 0.8.0.
ℹ Please use a list of either functions or lambdas:
# Simple named list: list(mean = mean, median = median)
# Auto named with `tibble::lst()`: tibble::lst(mean, median)
# Using lambdas list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
Warning: `funs()` was deprecated in dplyr 0.8.0.
ℹ Please use a list of either functions or lambdas:
# Simple named list: list(mean = mean, median = median)
# Auto named with `tibble::lst()`: tibble::lst(mean, median)
# Using lambdas list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
join the crime rates to the dispensary rates by county GEOID
Code
# connect the variables, pulling some in from the density filedsps_yoy_qtiles <-read.csv("~/gvsu/winter 23/CIS 635/NIMBY/cleaning/counting_dispensaries/dps_data_cleaned/dsps_yoy_qtiles.csv")linear_variables <-left_join(crime_per_capita, dsps_per_capita, by ="GEOID")linear_variables <-left_join(linear_variables, dsps_yoy_qtiles, by ="GEOID")colnames(linear_variables)
The following object is masked from 'package:purrr':
map
Code
library(dplyr)library(cowplot)
Attaching package: 'cowplot'
The following object is masked from 'package:lubridate':
stamp
Code
# Load the county boundaries data for Coloradocounty_centers <-read.csv("~/gvsu/winter 23/CIS 635/NIMBY/cleaning/geo_files/co_cords.csv") # centeralized county lat and long downloaded from the webcounties <-map_data("county", "colorado")counties <-rename(counties, County = subregion)# join the average dispensary county with the center of the countydsp_points <-left_join(county_centers, linear_variables, by ="County")# join the crime data with the shape files# make the counties the same case so they match with the other datalinear_variables$County <-tolower(linear_variables$County)my_map_data <-left_join(counties, linear_variables, by ="County")my_map <-ggplot() +# heat mapgeom_polygon(data = my_map_data , aes(x = long, y = lat, group = group, fill = crime_per_cap, color ="")) +coord_fixed(ratio =1) +labs(title ="Crime Rate and Dispensary Count by County",subtitle ="State of Colorado | Three-Year Average 2014, 2015, 2016",caption ="Mineral county crime data not available",fill ="avg crime rate",color ="county boarder") +# colorsscale_fill_gradient(low ="#DAF0FA", high ="#B41876") +scale_color_manual(values ="black", guide =FALSE) +# remove border color # repostion legends guides(fill =guide_colorbar(title.position ="top",title.hjust =0.5, label.position ="bottom",direction ="horizontal")) +theme(legend.position ="bottom",plot.caption =element_text(size =8, face ="italic", hjust = .1),axis.text =element_blank(),axis.ticks =element_blank(),axis.title =element_blank()) +# geo bubble chartsgeom_point(data = dsp_points,aes(x = Longitude, y = Latitude, size = dsps_average),color ="#23CB7F", alpha =0.8)+labs(size ="avg no. of dispensaries")+# horizontal legendguides(size =guide_legend(title.position ="top",title.hjust =0.5,label.position ="bottom",direction ="horizontal")) +# add county labelsgeom_text(data= county_centers, aes(x = Longitude, y = Latitude, label = County),size =2, color ="black", fontface ="bold", nudge_y =-0.08) +theme_minimal() +theme(plot.caption =element_text(size =8, face ="italic", hjust = .1),axis.text =element_blank(), # remove extra stuffaxis.ticks =element_blank(),axis.title =element_blank(), # messing with the legend for far too longlegend.position ="right",legend.key.size =unit(.25, 'cm'), #change legend key sizelegend.key.height =unit(.25, 'cm'), #change legend key heightlegend.key.width =unit(.5, 'cm'),legend.title =element_text(size=7),legend.text =element_text(size =7)) my_map
Code
ggsave("images/co_map1.png")
Saving 7 x 5 in image
Code
# for some reason I have to run the code twice for it to work, so I'm linking to the final result