setwd("~/Desktop/Classes/Programming Methods/chicago")

#libraries
library(data.table)
library(sp)
library(rgdal)
## rgdal: version: 1.4-6, (SVN revision 841)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.4.2, released 2019/06/28
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rgdal/gdal
##  GDAL binary built with GEOS: FALSE 
##  Loaded PROJ.4 runtime: Rel. 5.2.0, September 15th, 2018, [PJ_VERSION: 520]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rgdal/proj
##  Linking to sp version: 1.3-1
library(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
#read in data table
chi_dat = fread("chicago_6mos_google_communities.csv")

#convert to data table
chi_dat = as.data.table(chi_dat)

#table of total incidents by community
#filter places outside of chicago communities
#final number of observations = 1021
chi_tab = chi_dat[community!=""]

#make data spatial
coordinates(chi_tab) = c("google_longitude","google_latitude")
crs.geo1 = CRS("+proj=longlat")  
proj4string(chi_tab) = crs.geo1  

plot(chi_tab, pch = 20, col = "steelblue")

#read in shapefile of chicago
chicago = readOGR(dsn = "./communities", layer = "geo_export_84596be1-10fc-47e4-a7d0-36d6c9a9e0a3") #name of file and object
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/Daniela/Desktop/Classes/Programming Methods/chicago/communities", layer: "geo_export_84596be1-10fc-47e4-a7d0-36d6c9a9e0a3"
## with 77 features
## It has 9 fields
#name without file extension
#no forward slash at end of folder name
plot(chicago)
points(chi_tab, pch = 20, col = "orange")