forked from majerus/r_code_tips
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrmaps.R
More file actions
179 lines (137 loc) · 5.36 KB
/
Copy pathrmaps.R
File metadata and controls
179 lines (137 loc) · 5.36 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
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
# http://rmaps.github.io/blog/posts/animated-choropleths/
#require(devtools)
#install_github('ramnathv/rCharts@dev')
#install_github('ramnathv/rMaps')
library(rMaps)
library(rCharts)
library(reshape2)
# change file path to match location on your machine
folder <- '/Users/majerus/Desktop/2014 projects/blog/post1_logs/'
# change file name to match name on your machine
file <- 'state_enrollment_reed.csv'
# read in enrollment data
state <- read.csv(paste(folder, file, sep=''))
# rename columns for reshape
colnames(state) <- c('State', '2007', '2008', '2009', '2010', '2011', '2012', '2013')
# reshape data from wide to long
state_long <- melt(state)
# rename columns
colnames(state_long) <- c('State', 'Year', 'Students')
# check class of each variable
sapply(state_long, class)
# convert year to numeric
state_long$Year <- as.integer(as.character(state_long$Year))
# convert state to character
state_long$State <- as.character(state_long$State)
# convert students to numeric
state_long$Students <- as.numeric(state_long$Students)
# change stage names to abbr.
state_long$abr <- state.abb[match(as.character(state_long$State), state.name)]
# log
state_long$Students_log <- ifelse(state_long$Students==0, 0, log(state_long$Students))
# no students from
map <-
ichoropleth(Students ~ abr,
data = state_long,
ncuts = 1,
animate = 'Year',
play = TRUE,
legend = FALSE
)
map$save('/Users/majerus/Desktop/2014 projects/blog/post1_logs/rmaps/no_students.html', cdn = TRUE)
ichoropleth(Students ~ abr,
data = state_long,
ncuts = 5,
animate = 'Year',
play = TRUE,
legend = FALSE
)
slider <-
MYchoropleth(Students ~ abr,
data = state_long,
animate = 'Year',
legend = TRUE
)
slider$save('/Users/majerus/Desktop/2014 projects/blog/post1_logs/rmaps/slider.html', cdn = TRUE)
play <-
MYchoropleth(Students ~ abr,
data = state_long,
animate = 'Year',
legend = FALSE,
play=TRUE
)
play$save('/Users/majerus/Desktop/2014 projects/blog/post1_logs/rmaps/play.html', cdn = TRUE)
hist(state_long$Students_log)
MYchoropleth <- function(x, data, pal = "Blues", ncuts = 5, animate = NULL, play = F, map = 'usa', legend = TRUE, labels = TRUE, ...){
d <- Datamaps$new()
fml = lattice::latticeParseFormula(x, data = data)
data = transform(data,
fillKey = cut(
fml$left,
c(-1,0, 5,10,25,50,100),
ordered_result = TRUE
)
)
fillColors = c('white', RColorBrewer::brewer.pal(5, 'YlOrRd'))
d$set(
scope = map,
fills = as.list(setNames(fillColors, levels(data$fillKey))),
legend = legend,
labels = labels,
...
)
if (!is.null(animate)){
range_ = summary(data[[animate]])
data = dlply(data, animate, function(x){
y = toJSONArray2(x, json = F)
names(y) = lapply(y, '[[', fml$right.name)
return(y)
})
d$set(
bodyattrs = "ng-app ng-controller='rChartsCtrl'"
)
d$addAssets(
jshead = "http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.min.js"
)
if (play == T){
d$setTemplate(chartDiv = sprintf("
<div class='container'>
<button ng-click='animateMap()'>Play</button>
<div id='{{chartId}}' class='rChart datamaps'></div>
</div>
<script>
function rChartsCtrl($scope, $timeout){
$scope.year = %s;
$scope.animateMap = function(){
if ($scope.year > %s){
return;
}
map{{chartId}}.updateChoropleth(chartParams.newData[$scope.year]);
$scope.year += 1
$timeout($scope.animateMap, 1000)
}
}
</script>", range_[1], range_[6])
)
} else {
d$setTemplate(chartDiv = sprintf("
<div class='container'>
<input id='slider' type='range' min=%s max=%s ng-model='year' width=200>
<div id='{{chartId}}' class='rChart datamaps'></div>
</div>
<script>
function rChartsCtrl($scope){
$scope.year = %s;
$scope.$watch('year', function(newYear){
map{{chartId}}.updateChoropleth(chartParams.newData[newYear]);
})
}
</script>", range_[1], range_[6], range_[1])
)
}
d$set(newData = data, data = data[[1]])
} else {
d$set(data = dlply(data, fml$right.name))
}
return(d)
}