-
Notifications
You must be signed in to change notification settings - Fork 1
/
ggmap-0-vignette.R
100 lines (75 loc) · 2.61 KB
/
ggmap-0-vignette.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
# ggmap -------------------------------------------------------------------
#basado en: https://github.com/dkahle/ggmap
#complicaciones:
#Failed to connect to tile.stamen.com
# librerias ---------------------------------------------------------------
library(tidyverse)
library(ggmap)
# credentials -------------------------------------------------------------
#limitante: para usar google maps, se debe de crear una cuenta
#alternativa: usar openstreetmaps
register_google(key = "AIzaSyAvGB5P3z9Y7ATJa1GUzYaYNwX7SaM0Vk4")
ggmap_credentials()
# githubpage --------------------------------------------------------------
us <- c(left = -125, bottom = 25.75, right = -67, top = 49)
get_stamenmap(us, zoom = 5, maptype = "toner-lite") %>% ggmap()
# start -------------------------------------------------------------------
violent_crimes <- crime %>%
# only violent crimes
filter(offense != "auto theft",
offense != "theft",
offense != "burglary") %>%
# rank violent crimes
mutate(offense=as.factor(offense),
offense=forcats::fct_drop(offense)) %>% #summary()
# restrict to downtown
filter(-95.39681 <= lon & lon <= -95.34188,
29.73631 <= lat & lat <= 29.78400)
qmplot(lon, lat,
data = violent_crimes,
maptype = "toner-lite",
#check the effect of I() function
color = I("red"),
extent = "panel")
qmplot(lon, lat,
data = violent_crimes,
maptype = "toner-lite",
#check the effect of I() function
color = I("red"),
extent = "panel",
geom = "density2d")
qmplot(lon, lat,
data = violent_crimes,
maptype = "toner-lite",
#check the effect of I() function
color = offense,
extent = "panel"#,
#geom = "density2d"
) +
facet_wrap(~offense)
qmplot(lon, lat,
data = violent_crimes,
maptype = "toner-lite",
#check the effect of I() function
color = offense,
extent = "panel",
darken = .4
#geom = "density2d"
) +
facet_wrap(~month)
### Heatmap ------------------
robberies <- violent_crimes %>%
filter(offense == "robbery")
qmplot(lon, lat,
data = violent_crimes,
geom = "blank", zoom = 15,
maptype = "toner-background", darken = .7,
legend = "topleft") +
stat_density_2d(aes(fill = ..level..),
geom = "polygon", alpha = .3, color = NA) +
scale_fill_gradient2("Robbery\nPropensity",
low = "white",
mid = "yellow",
high = "red",
midpoint = 650)
### Clustering ------------------