-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollecting_Formatting_Data.R
More file actions
70 lines (55 loc) · 2.25 KB
/
Copy pathCollecting_Formatting_Data.R
File metadata and controls
70 lines (55 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
###PROJECTING AND FORMATTING DATA
#load required libraries
library(spatstat)
library(rgdal)
library(maptools)
library(raster)
library(sp)
library(plyr)
###ACQUIRE AND FORMAT DATA ###
#get data from here
#https://www.google.com/maps/d/viewer?mid=18u0QER64-OR_Kacg_EoKQpDUU5g&hl=en&ll=49.16837282831341%2C-122.63991487265628&z=10
#read in homicide events as shapefile, uses rgdal readOGR
km <- readOGR("MetroVancouverHomicides.kml")
#clean up the columns
km$Name <- as.character(km$Name)
#create a year column
km$year <- as.numeric(substr(km$Name, nchar(km$Name)-4, nchar(km$Name)))
#remove one observation without a year
km <- km[complete.cases(km$year),]
#project to bc albers
kma <- spTransform(km, CRS("+init=epsg:3005"))
#kma <- spTransform(km, CRS("+proj=aea +lat_1=50 +lat_2=58.5 +lat_0=45 +lon_0=-126 +x_0=1000000
# +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs"))
#add coordinates to the data
kma$x <- coordinates(kma)[,1]
kma$y <- coordinates(kma)[,2]
#check for and remove duplicated points
#check for duplicated points
#finds zero distance among points
zd <- zerodist(kma)
zd
#remove duplicates
kma <- remove.duplicates(kma)
#create an "extent" object which can be used to create the observation window for spatstat
kma.ext <- as.matrix(extent(kma))
#observation window
window <- as.owin(list(xrange = kma.ext[1,], yrange = kma.ext[2,]))
#create ppp oject from spatstat
kma.ppp <- ppp(x = kma$x, y = kma$y, window = window)
##Load the libraries
library(gridExtra)
library(ggmap)
library(maptools)
library(maps)
##Set working directory
getwd() #First look to see what is the current directory
#setwd("/Users/chris/Desktop") #Then set the directory to where you want to retrieve and print data
##Read in data
df <- data.frame(read.csv("homicides.csv"), header=TRUE) #read csv file
attach(df) #attach dataset
world.map <- get_map(location = c(lon = mean(range(df$longitude)), lat = mean(range(df$latitude))), zoom = 3)
ggmap(world.map) + ggtitle("Dana Huget's Map") +
theme(plot.title = element_text(size = 12, face = "bold", hjust = 0.5)) +
geom_point(data = df, aes(x = df$longitude, y = df$latitude, size = `Number of Dead and Missing`, colour = `Number of Dead and Missing`)) +
scale_colour_gradient( high = "black", low = "lightpink")