/
R_SCRIPT.R
108 lines (82 loc) · 2.89 KB
/
R_SCRIPT.R
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#Get any packages you might need
#install.packages("sp")
#install.packages("rgdal")
#install.packages("GISTools")
#install.packages("classInt")
#install.packages("RColorBrewer")
#Load the packages
library(sp)
library(rgdal)
library(GISTools)
library(classInt)
library(RColorBrewer)
#Read and create data; be sure to change your directory here
setwd("I:/MKE_R_GEO")
Robberies<- readOGR("Robberies.shp")
CityLine<- readOGR("MKECityLine2.shp")
data<- readOGR("MKEDATA2.shp")
#Check the data
head(data@data)
colnames(data@data)
#Plot maps individually
plot(CityLine)
plot(Robberies)
plot(data)
#Or all three in one; 3 columns x 1 row
par(mfrow=c(1,3))
plot(CityLine)
par(new=FALSE)
plot(Robberies)
par(new=FALSE)
plot(data)
#check projections
data@proj4string
Robberies@proj4string
CityLine@proj4string
#Plot Deprivation Index--it is column 7; name it "depr" as well
depr<-data$DEPRIVATIO
depr[is.nan(depr)]<-0
depr[is.na(depr)]<-0
#Also check the distribution of the variable; make it 1x1 again instead of 3 columns
par(mfrow=c(1,1))
hist(depr)
#Gives you a basic choropleth map
spplot(data[,7])
#make it a little nicer
#9 colors with 8 breaks in between
#Choose a ramp from white-->black
colors<-brewer.pal(9,"Greys")
spplot(data[,7],col.regions=colors,cuts=8, main="Economic Deprivation, Milwaukee, 2014")
#Now the robberies, including the city borders
plot(CityLine,col="black",lwd=4,lty=1,main="Milwaukee Robberies, 2014")
plot(Robberies,pch=16,cex=0.5,add=TRUE)
#Change the shape of Milwaukee; copy a projection
CityLine@proj4string<-Robberies@proj4string
plot(CityLine,col="black",lwd=4,lty=1,main="Milwaukee Robberies, 2014")
plot(Robberies,pch=16,cex=0.5,add=TRUE)
#Add block groups
plot(data[,7],main="Milwaukee Robberies, 2014")
plot(CityLine,col="black",lwd=4,lty=1,add=TRUE)
plot(Robberies,pch=16,cex=0.5,add=TRUE)
#Now make a choropleth map of economic deprivation
#and add the robberies
#Need colors and breaks
#Colors are done above; now do breaks
#Natural breaks are used here
#Need to separate the values out from the results
breaks1<-classIntervals(data@data[,7],n=8,style="jenks")
breaks1
breaks1<-breaks1$brks
breaks1
#First just make the map print in RStudio
library(GISTools)
choropleth(sp=data,v = depr, shading(breaks1,cols = colors), main="Milwaukee Deprivation Index and Robberies, 2014")
plot(CityLine,col="black",lwd=4,lty=1,add=TRUE)
points(Robberies,col="red",pch=16,cex=0.01)
################################################################################
#Print to file: The only change is the point size on the robberies
jpeg("MKEMAP.jpg",width=9,height=9,units="in",res=300)
choropleth(sp=data,v = depr, shading(breaks1,cols = colors), main="Milwaukee Deprivation Index and Robberies, 2014")
plot(CityLine,col="black",lwd=4,lty=1,add=TRUE)
points(Robberies,col="red",pch=16,cex=0.3)
dev.off()