forked from miningchi/Redlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RLCmapandHeatMap.R
183 lines (147 loc) · 7.64 KB
/
RLCmapandHeatMap.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
###RLC MAP AND HEAT MAP
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## Output 2 - Map
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
output$maptitle <- renderUI({helpText(HTML("<b>Red Light cameras in Red, Speed cameras in Blue</b>"))})
output$mapcenter <- renderUI({textInput("center", "Enter a Location to Center Map, such as city or zipcode, the click Update", "Chicago")})
output$maptype <- renderUI({selectInput("type", "Choose Google Map Type:", choice = c("roadmap", "satellite", "hybrid","terrain"))})
output$mapres <- renderUI({checkboxInput("res", "High Resolution?", FALSE)})
output$mapbw <- renderUI({checkboxInput("bw", "Black & White?", FALSE)})
output$mapzoom <- renderUI({sliderInput("zoom", "Zoom Level (Recommended - 12):", min = 9, max = 20, step = 1, value = 12)})
output$map <- renderPlot({
# Set Defaults for when Map starts
if (is.null(input$center)) {map.center <- geocode("Chicago")}
else {map.center = geocode(input$center)}
if (is.null(input$bw)) {temp.color <- "color"}
else {
temp.color <- "color"
if (input$bw) {temp.color <- "bw"}}
if (is.null(input$res)) {temp.scale <- 2}
else {
temp.scale <- 1
if (input$res) {temp.scale <- 2}}
if (is.null(input$zoom)) {temp.zoom <- 12}
else {temp.zoom <- input$zoom }
#Get Base Map
map.base <- get_googlemap(
as.matrix(map.center),
maptype = input$type, ## Map type as defined above (roadmap, terrain, satellite, hybrid)
# markers = map.center,
zoom = temp.zoom, ## 14 is just about right for a 1-mile radius
color = temp.color, ## "color" or "bw" (black & white)
scale = temp.scale, ## Set it to 2 for high resolution output
messaging = FALSE,
)
## Convert the base map into a ggplot object
## All added Cartesian coordinates to enable more geom options later on
map.base <- ggmap(map.base, extend = "panel", messaging = FALSE) + coord_cartesian() + coord_fixed(ratio = 1.5)
## add points
redlightdatabase <- datesubset()
p <- map.base + geom_point(aes(x=long, y=lat), colour="red", size = 4, na.rm=TRUE, data=subset(redlightdatabase,Red.Speed == 1))
p <- p + geom_point(aes(x=long, y=lat), colour="blue", size = 4, na.rm=TRUE, data=subset(redlightdatabase,Red.Speed == 0))
plot(p)
})
#, width = 1800, height = 1800)
###########
####HEAT MAP
############
output$hmaptitle <- renderUI({helpText(HTML("<b>DENSITY PLOT SETTINGS</b>"))})
output$hmapcenter <- renderUI({textInput("center", "Enter a Location to Center Map, such as city or zipcode, the click Update", "Chicago")})
output$hmaptype <- renderUI({selectInput("type", "Choose Google Map Type:", choice = c("roadmap", "satellite", "hybrid","terrain"))})
output$hmapres <- renderUI({checkboxInput("res", "High Resolution?", FALSE)})
output$hmapbw <- renderUI({checkboxInput("bw", "Black & White?", FALSE)})
output$hmapzoom <- renderUI({sliderInput("zoom", "Zoom Level (Recommended - 14):", min = 9, max = 20, step = 1, value = 14)})
output$halpharange <-renderUI({sliderInput("halpharanage", "Alpha Range:",
min = 0, max = 1, step = 0.1, value = c(0.1, 0.4))})
output$hbins <-renderUI({sliderInput("hbins", "Number of Bins:",
min = 5, max = 50, step = 5, value = 15)})
output$hboundwidth <-renderUI({sliderInput("hboundwidth", "Boundary Lines Width:",
min = 0, max = 1, step = 0.1, value = 0.1)})
output$hboundcolor <-renderUI({selectInput("hboundcolor", "Boundary Lines Colour:",
choice = c("grey95","black", "white", "red", "orange", "yellow", "green", "blue", "purple"))})
output$hlow <-renderUI({selectInput("hlow", "Fill Gradient (Low):",
choice = c("yellow", "red", "orange", "green", "blue", "purple", "white", "black", "grey"))})
output$hhigh <-renderUI({selectInput("hhigh", "Fill Gradient (High):",
choice = c("red", "orange", "yellow", "green", "blue", "purple", "white", "black", "grey"))})
output$heatmap <- renderPlot({
crimetypedatabase <- dfall
# Set Defaults for when Map starts
if (is.null(input$center)) {map.center <- geocode("Chicago")}
else {map.center = geocode(input$center)}
if (is.null(input$bw)) {temp.color <- "color"}
else {
temp.color <- "color"
if (input$bw) {temp.color <- "bw"}}
if (is.null(input$res)) {temp.scale <- 2}
else {
temp.scale <- 1
if (input$res) {temp.scale <- 2}}
if (is.null(input$zoom)) {temp.zoom <- 14}
else {temp.zoom <- input$zoom }
if (is.null(input$halpharange)) {temp.halpharange <- c(0.1, 0.4)}
else {temp.halpharange<- input$halpharange}
if (is.null(input$hbins)) {temp.hbins <- 15}
else {temp.hbins<- input$hbins}
if (is.null(input$hboundwidth)) {temp.hboundwidth <- .1}
else {temp.hboundwidth<- input$hboundwidth}
if (is.null(input$hboundcolor)) {temp.hboundcolor <- "grey95"}
else {temp.hboundcolor<- input$hboundcolor}
if (is.null(input$hlow)) {temp.hlow <- "yellow" }
else {temp.hlow<- input$hlow}
if (is.null(input$hhigh)) {temp.hhigh <- "red"}
else {temp.hhigh<- input$hhigh}
#Get Base Map
map.base <- get_googlemap(
as.matrix(map.center),
maptype = input$type, ## Map type as defined above (roadmap, terrain, satellite, hybrid)
# markers = map.center,
zoom = temp.zoom, ## 14 is just about right for a 1-mile radius
color = temp.color, ## "color" or "bw" (black & white)
scale = temp.scale, ## Set it to 2 for high resolution output
messaging = FALSE,
)
## Convert the base map into a ggplot object
## All added Cartesian coordinates to enable more geom options later on
map.base <- ggmap(map.base, extend = "panel", messaging = FALSE) + coord_cartesian() + coord_fixed(ratio = 1.5)
## add heat map
map.final <- map.base +
## Create a density plot
## based on the ggmap's crime data example
stat_density2d(aes(x = CrashLongitude,
y = CrashLatitude,
fill = ..level..,
alpha = ..level..),
size = temp.hboundwidth,
bins = temp.hbins, ## Change and experiment with no. of bins
data = crimetypedatabase,
geom = "polygon",
colour = temp.hboundcolor) +
## Configure the scale and panel
scale_fill_gradient(low = temp.hlow, high = temp.hhigh) +
scale_alpha(range = temp.halpharange) +
## Title and labels
labs(x = "Longitude", y = "Latitude") +
# ggtitle(paste("Crimes in/around ", map.center))+
# " from ", temp.period[1],
# " to ", temp.period[length(temp.period)], sep="")) +
## Other theme settings
theme_bw() +
theme(
plot.title = element_text(size = 36, face = 'bold', vjust = 2),
#title = element_text(face = 'bold'),
axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
legend.position = "right",
#axis.text.x = element_text(size = 28),
#axis.text.y = element_text(size = 28),
#axis.title.x = element_text(size = 32),
#axis.title.y = element_text(size = 32),
strip.background = element_rect(fill = 'grey80'),
strip.text = element_text(size = 26)
)
redlightdatabase <- datesubset()
p <- map.final + geom_point(aes(x=long, y=lat), colour="red", size = 4, na.rm=TRUE, data=subset(redlightdatabase,Red.Speed == 1))
p <- p + geom_point(aes(x=long, y=lat), colour="blue", size = 4, na.rm=TRUE, data=subset(redlightdatabase,Red.Speed == 0))
plot(p)
})