forked from malecki/edav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rMapsBlogSample.R
181 lines (145 loc) · 4.15 KB
/
rMapsBlogSample.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
#Install rMaps
require(devtools)
install_github('ramnathv/rCharts@dev')
install_github('ramnathv/rMaps')
#libraries to load
library(rMaps)
library(Quandl)
library(reshape2)
library(knitr)
library(plyr)
library(dplyr)
library(rcharts)
###############################
#Violent Crime Rate heatmap Cholorpleths for US
##############################
#load US Violent Crimerate data from Quandl
vcData = Quandl("FBI_UCR/USCRIME_TYPE_VIOLENTCRIMERATE")
#format Dataframe with html
kable(head(vcData[,1:9]), format = 'html', table.attr = "class=nofluid")
#reshape data to year, state and crime and subset
datm <- melt(vcData, 'Year',
variable.name = 'State',
value.name = 'Crime'
)
datm <- subset(na.omit(datm),
!(State %in% c("United States", "District of Columbia"))
)
#format new Dataframe with html
kable(head(datm), format = 'html', table.attr = "class=nofluid")
#transform the data
datm2 <- transform(datm,
State = state.abb[match(as.character(State), state.name)],
fillKey = cut(Crime, quantile(Crime, seq(0, 1, 1/5)), labels = LETTERS[1:5]),
Year = as.numeric(substr(Year, 1, 4))
)
kable(head(datm2), format = 'html', table.attr = "class=nofluid")
#format fill for map
fills = setNames(
c(RColorBrewer::brewer.pal(5, 'YlOrRd'), 'white'),
c(LETTERS[1:5], 'defaultFill')
)
#use dply to create LargeList format of data for map
dat2 <- dlply(na.omit(datm2), "Year", function(x)
{
y = toJSONArray2(x, json = F)
names(y) = lapply(y, '[[', 'State')
return(y)
})
#### End data munging section
#Siplest R Chart example static map could have used GGMaps
options(rcharts.cdn = TRUE)
map <- Datamaps$new()
map$set(
dom = 'chart_1',
scope = 'usa',
fills = fills,
data = dat2[[1]],
legend = TRUE,
labels = TRUE
)
map
map2 = map$copy()
map2$set(
bodyattrs = "ng-app ng-controller='rChartsCtrl'"
)
map2$addAssets(
jshead = "http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"
)
map2$setTemplate(chartDiv = "
<div class='container'>
<input id='slider' type='range' min=1960 max=2010 ng-model='year' width=200>
<span ng-bind='year'></span>
<div id='' class='rChart datamaps'></div>
</div>
<script>
function rChartsCtrl($scope){
$scope.year = 1960;
$scope.$watch('year', function(newYear){
map.updateChoropleth(chartParams.newData[newYear]);
})
}
</script>"
)
map2$set(newData = dat2, scope = 'usa')
map2
map3 = map2$copy()
map3$setTemplate(chartDiv = "
<div class='container'>
<button ng-click='animateMap()'>Play</button>
<div id='chart_1' class='rChart datamaps'></div>
</div>
<script>
function rChartsCtrl($scope, $timeout){
$scope.year = 1960;
$scope.animateMap = function(){
if ($scope.year > 2010){
return;
}
mapchart_1.updateChoropleth(chartParams.newData[$scope.year]);
$scope.year += 1
$timeout($scope.animateMap, 1000)
}
}
</script>"
)
map3
map$set(dom = 'chart_1', newData = dat2, scope = 'usa', fills = fills, legend = TRUE, labels = TRUE)
map
#shows the 1 line of code needed to create
source('ichoropleth.R')
ichoropleth(Crime ~ State,
data = datm2[,1:3],
pal = 'PuRd',
ncuts = 5,
animate = 'Year'
)
#R Code #2
###############################
#call a Leaflet map and apply a heatmap on top of it
##############################
library(rMaps)
L2 <- Leaflet$new()
L2$setView(c(29.7632836, -95.3632715), 10)
L2$tileLayer(provider = "MapQuestOpen.OSM")
L2
#get the data
data(crime, package = 'ggmap')
library(plyr)
crime_dat = ddply(crime, .(lat, lon), summarise, count = length(address))
crime_dat = toJSONArray2(na.omit(crime_dat), json = F, names = F)
cat(rjson::toJSON(crime_dat[1:2]))
#heatmap it
# Add leaflet-heat plugin. Thanks to Vladimir Agafonkin
L2$addAssets(jshead = c(
"http://leaflet.github.io/Leaflet.heat/dist/leaflet-heat.js"
))
# Add javascript to modify underlying chart
L2$setTemplate(afterScript = sprintf("
<script>
var addressPoints = %s
var heat = L.heatLayer(addressPoints).addTo(map)
</script>
", rjson::toJSON(crime_dat)
))
L2