-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.Rmd
751 lines (609 loc) · 27.6 KB
/
index.Rmd
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
---
title: "Crowdsourcing: `r format(Sys.Date(), format='%B %Y')`"
output:
flexdashboard::flex_dashboard:
theme: lumen
orientation: rows
source_code: embed
---
```{r global, include=FALSE}
#Windows version
# This is a tool to allow for the automation of extracting OSM data, processing it, doing analysis and creating a dashboard to report on the results of predefined questions about the data for two cities. The code is for macOS but there is also a Windows version. This R document will do everything needed. All that is required is to run it with Knit (Rstudio: click on knit).
# Please take note that this script will take a few hours to complete.
# This is written in R markdown, a powerful and modern way of generating reports and running analysis and data processing at the same time. Automating reports and analysis is the future!
# Look for needed libraries and install them if missing using the pacman package:
if (!require("pacman")) install.packages("pacman")
pacman::p_load(flexdashboard, tidyverse, ggthemes, lubridate, leaflet,
rgdal, forcats, scales, DT, plotly, stringr, rgeos, stringr, maptools)
# Capture the date and format for its use
date <- Sys.Date()
date2 <- as.character(date)
date3 <- str_replace_all(date, "-", "")
# Create the path to save files
datePath <- file.path("C:/Projects/OSMdata", date)
datePath2 <- str_replace_all(datePath, "/", "\\\\")
if(!file.exists(datePath)) dir.create(datePath)
#The URLs to download files
ontario <- "http://download.geofabrik.de/north-america/canada/ontario-latest.osm.pbf"
quebec <- "http://download.geofabrik.de/north-america/canada/quebec-latest.osm.pbf"
#Create a time stamp and change the file names to include time of download
FileNameOnt <- "ontario-latest.osm.pbf"
FileNameOnt <- str_replace_all(FileNameOnt, "latest", date3)
FileNameQueb <- "quebec-latest.osm.pbf"
FileNameQueb <- str_replace_all(FileNameQueb, "latest", date3)
#Use created file path, download files and save into the created directory
OntFile <- file.path(datePath2, FileNameOnt)
OntFile <- str_replace_all(OntFile, "/", "\\\\")
QuebFile <- file.path(datePath2, FileNameQueb)
QuebFile <- str_replace_all(QuebFile, "/", "\\\\")
download.file(ontario, destfile = OntFile, mode="wb")
download.file(quebec, destfile = QuebFile, mode="wb")
#Import geometries
shell(paste("copy C:\\Projects\\OSMdata\\Geometries\\*.poly", datePath2))
# The first part requires system commands line. This was done on a mac. On a PC, system()
#must be replaced by shell().
# These commands will run in terminal or a shell and use Osmosis to do three things:
# a) subset osm data to the CSD of a municipality (Ottawa and Gatineau), subset the buildings (ways), then convert the resulting osm file into a GeoJSON. After processing Ottawa and Gatineau, we merge the two osm files into one OttGat file and turn it into a GeoJSON to have both together.
# The first part requires system commands line. This was done on a mac. On a PC, system()
#must be replaced by shell().
# 1. Ottawa
shell(paste("cd", datePath2, "&& osmosis --rbf", FileNameOnt, "--bounding-polygon file=\"Ottawa.poly\" completeWays=yes --wx ottawa.osm"))
shell(paste("cd", datePath2, "&& osmosis --rx ottawa.osm --tf accept-ways building=* --tf reject-relations --used-node --wx OttBuildW.osm"))
shell(paste("cd", datePath2,
"&& node --max_old_space_size=8192 c:\\node\\npm\\node_modules\\osmtogeojson\\osmtogeojson OttBuildW.osm > OttBuildW.geojson"))
# 2. Gatineau
shell(paste("cd", datePath2, "&& osmosis --rbf", FileNameQueb, "--bounding-polygon file=\"Gatineau.poly\" completeWays=yes --wx gatineau.osm"))
shell(paste("cd", datePath2, "&& osmosis --rx gatineau.osm --tf accept-ways building=* --tf reject-relations --used-node --wx GatBuildW.osm"))
shell(paste("cd", datePath2,
"&& node --max_old_space_size=8192 c:\\node\\npm\\node_modules\\osmtogeojson\\osmtogeojson GatBuildW.osm > GatBuildW.geojson"))
# The processing is finished and we need to import the GeoJSON to do produce the estimates
#Build the path from date
OttJSON <- file.path(datePath2, "OttBuildW.geojson")
GatJSON <- file.path(datePath2, "GatBuildW.geojson")
#Import GeoJSON
BuildOtt <- readOGR(OttJSON, "OGRGeoJSON", require_geomType="wkbPolygon")
BuildGat <- readOGR(GatJSON, "OGRGeoJSON", require_geomType="wkbPolygon")
#Extract data frame from spatial object
OttData <-BuildOtt@data
GatData <- BuildGat@data
OttGatData <- full_join(OttData, GatData)
OttData$timestamp <- as_date(ymd_hms(OttData$timestamp))
GatData$timestamp <- as_date(ymd_hms(GatData$timestamp))
OttGatData$timestamp <- as_date(ymd_hms(OttGatData$timestamp))
# 1. Get number of buildings
OttNumBuild <- tally(OttData) %>%
mutate(buildings = n) %>%
select(-n) %>%
unlist(use.names = FALSE)
GatNumBuild <- tally(GatData) %>%
mutate(buildings = n) %>%
select(-n) %>%
unlist(use.names = FALSE)
OttGatNumBuild <- tally(OttGatData) %>%
mutate(buildings = n) %>%
select(-n) %>%
unlist(use.names = FALSE)
# 2. Get Month
month <- Sys.Date()
# 3. Get num of users
OttUsersBuild <- OttData %>%
summarise(users = n_distinct(user)) %>%
unlist(use.names = FALSE)
GatUsersBuild <- GatData %>%
summarise(users = n_distinct(user)) %>%
unlist(use.names = FALSE)
OttGatUsersBuild <- OttGatData %>%
summarise(users = n_distinct(user)) %>%
unlist(use.names = FALSE)
# 4. calculate number of tags by removing common attributes, sum only non-NAs, sum the rows.
#remove columns with no values before counting tags
OttData2 <- OttData %>%
select_if(colSums(!is.na(.)) > 0)
GatData2 <- GatData %>%
select_if(colSums(!is.na(.)) > 0)
OttGatData2 <- OttGatData %>%
select_if(colSums(!is.na(.)) > 0)
rownames(OttData2) <- c()
rownames(GatData2) <- c()
rownames(OttGatData2) <- c()
#Remove common attributes before counting tags
CommAttr <- c("id", "user", "uid", "timestamp", "version", "changeset")
OttTagBuild <- OttData2 %>%
select(-one_of(CommAttr)) %>%
summarise_each(funs(sum(!is.na(.)))) %>%
mutate(tagsBuild = rowSums(.)) %>%
select(tagsBuild) %>%
unlist(use.names = FALSE)
GatTagBuild <- GatData2 %>%
select(-one_of(CommAttr)) %>%
summarise_each(funs(sum(!is.na(.)))) %>%
mutate(tagsBuild = rowSums(.)) %>%
select(tagsBuild) %>%
unlist(use.names = FALSE)
OttGatTagBuild <- OttGatData2 %>%
select(-one_of(CommAttr)) %>%
summarise_each(funs(sum(!is.na(.)))) %>%
mutate(tagsBuild = rowSums(.)) %>%
select(tagsBuild) %>%
unlist(use.names = FALSE)
# 5. average tags per building
OttAvgTagBuild <- OttTagBuild / OttNumBuild
GatAvgTagBuild <- GatTagBuild / GatNumBuild
OttGatAvgTagBuild <- OttGatTagBuild / OttGatNumBuild
# Make data frame from the objects
cities <- c("Ottawa", "Gatineau", "Ott/Gat")
month <- c(date, date, date)
buildings <- c(OttNumBuild, GatNumBuild, OttGatNumBuild)
usersBuild <- c(OttUsersBuild, GatUsersBuild, OttGatUsersBuild)
tagsBuild <- c(OttTagBuild, GatTagBuild, OttGatTagBuild)
averTagsBuild <- c(OttAvgTagBuild, GatAvgTagBuild, OttGatAvgTagBuild)
Build <- data.frame(cities, month, buildings, usersBuild, tagsBuild, averTagsBuild)
# Import the existing table containing previous months estimates and format date variable as date
data <- read.csv('C:/Projects/JARCID/Buildings.csv', header = TRUE)
data$month <- ymd(data$month)
# Join the existing table with new data from the new estimates
Build <- full_join(Build, data)
Build <- arrange(Build, month)
#Save the new table containing new data
write.csv(Build, "C:/Projects/JARCID/Buildings.csv", row.names = F)
#Create a name with time for the map
MapName <- "timeStamp.jpg"
MapDate <- str_replace_all(MapName, "timeStamp", date2)
#Create a theme (styling) for the map: basically, removing any background element and setting background
#to color #333333
theme1 <- theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
panel.background = element_rect(fill = "#333333"))
#Create a map from the building polygons and save it as jpeg image
jpeg(MapDate, width=1920, height=1166)
gg <- ggplot() +
geom_polygon(data = BuildOtt, aes(x = long, y = lat, group = group), fill=NA, color="white") +
geom_polygon(data = BuildGat, aes(x = long, y = lat, group = group), fill=NA, color="white") +
theme1 +
annotate("text", label = Sys.Date(), x = -76.2, y = 45.05,
size = 14, colour = "white")
print(gg)
dev.off()
#Import the table again and process it to fill the needed estimates for the dashboard
#The different versions of teh table are used for different date formating.
data <- read.csv('C:/Projects/JARCID/Buildings.csv', header = TRUE)
data2 <- data
data3 <- data
data$averTagsBuild <- round(data$averTagsBuild, 2)
data2$averTagsBuild <- round(data$averTagsBuild, 2)
data$month <- month(data$month, label = TRUE)
data2$month <- ymd(data2$month)
#The extraction, processing and analysis are almost all done. What follows is the dashboard.
#Some calculations and estimates are also done below for each segments.
#Notes: all charts are created with ggplot2. The use of plotly (ggplotly) is only there to benefit from teh autoresizing of ggplotly. This ensures that the plots are always the right size for the space they are given. The code will also make use of the captured date of the extraction where necessary.
```
Ottawa {data-navmenu="Cities"}
=======================================================================
row
-----------------------------------------------------------------------
###Buildings {.value-box}
```{r, echo=FALSE}
#Count the number of buildings for Ottawa and select the last entry in "month"
buildOtt <- data2 %>%
filter(cities == "Ottawa") %>%
top_n(1, month) %>%
select(buildings)
valueBox(comma(buildOtt), icon = "ion-ios-home-outline", color = "#9ecae1")
```
###Users {.value-box}
```{r}
#Count the number of users for Ottawa and select the last entry in "month"
usersOtt <- data2 %>%
filter(cities == "Ottawa") %>%
top_n(1, month) %>%
select(usersBuild)
valueBox(usersOtt, icon = "ion-ios-people-outline", color = "#9ecae1")
```
###Average number of tags {.value-box}
```{r}
#Get the average number of tags for Ottawa and select the last entry in "month"
tagsOtt <- data2 %>%
filter(cities == "Ottawa") %>%
top_n(1, month) %>%
select(averTagsBuild)
tagsOtt$averTagsBuild <- round(tagsOtt$averTagsBuild, 2)
valueBox(tagsOtt, icon = "ion-ios-pricetags-outline", color = "#9ecae1")
```
row {data-height=400}
-----------------------------------------------------------------------
###Buildings mapped
```{r, echo=FALSE, message=FALSE}
#Select buildings for ottawa for all months
OttBuild <- data2 %>%
filter(cities=="Ottawa") %>%
select(cities, month, buildings) %>%
group_by(cities, m = month(month)) %>%
filter(month == max(month))
OttBuild$MonthY <- format(as.Date(OttBuild$month), "%b %Y")
OttBuild$MonthY <- factor(OttBuild$MonthY, levels = OttBuild$MonthY[order(OttBuild$buildings)])
#Create a time series line chart
#With a geom_line plot, you need to add group = 1 if only one group of observations
#to avoid the warning message each is obs is one group
gg <- ggplot(OttBuild, aes(MonthY, buildings, group = 1, text = comma(buildings)))
gg <- gg + geom_line(position = "identity", color="#6baed6")
gg <- gg + scale_y_continuous(labels = comma)
gg <- gg + labs(x = NULL, y = NULL)
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme_bw(base_family = "Helvetica")
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.grid.major.x= element_blank())
gg <- gg + theme(panel.grid.major.y=element_line(linetype = "dashed"))
gg <- gg + theme(panel.grid.minor = element_blank())
gg <- gg + theme(axis.ticks=element_line(colour="#ececec"))
gg <- gg + theme(axis.text.x=element_text(size=12))
gg <- gg + theme(axis.text.y=element_text(size=10))
ggplotly(gg, tooltip = "text")
```
row {data-height=600}
-------------------------------------------------------------------
###Buildings by type
```{r}
#Select buildings for Ottawa and group them by type, calculate sum by type and sort in descending order,
#then select those with over 50 buildings per type
buildType <- OttData2 %>%
group_by(building) %>%
summarise(numB = length(building)) %>%
arrange(desc(numB)) %>%
filter(numB > 50)
#Create a dot plot
gg <- ggplot(buildType, aes(x=numB, y=reorder(building, numB), text = comma(numB)))
gg <- gg + geom_segment(aes(xend = 0, yend=building), color="#ececec")
gg <- gg + geom_point(color = "#3282bd", size = 2)
gg <- gg + scale_x_continuous(expand = c(0.1,0), labels = comma)
gg <- gg + labs(x = NULL, y = NULL)
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme_bw(base_family = "Helvetica")
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.grid.major = element_blank())
gg <- gg + theme(panel.grid.minor = element_blank())
gg <- gg + theme(axis.ticks.y=element_blank())
gg <- gg + theme(axis.text.x=element_text(size=9))
gg <- gg + theme(axis.text.y=element_text(size=12))
ggplotly(gg, tooltip = "text")
```
### Missing address fields in percentage
```{r}
# Select the variables to study (attributes)
MissAddOtt <- OttData2 %>%
select(addr.street, addr.housenumber, addr.city, addr.postcode)
#Change the variable names to descriptive names, sum and calculate the percentage of missing
MeanAddrOtt <- MissAddOtt %>%
summarise_all(funs(mean(is.na(.))*100)) %>%
gather(Field, meanNA) %>%
mutate(Field = fct_recode(Field, "Street" = "addr.street",
"Street Number" = "addr.housenumber",
"City" = "addr.city",
"Postal Code" = "addr.postcode"))
gg <- ggplot(MeanAddrOtt, aes(x=reorder(Field, meanNA), y=meanNA, text = round(meanNA, 2)))
gg <- gg + geom_segment(aes(xend = Field, yend=0), color="#ececec")
gg <- gg + geom_point(color = "#3282bd", size = 2)
gg <- gg + coord_flip()
gg <- gg + labs(x = NULL, y = NULL)
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme_bw(base_family = "Helvetica")
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.grid.major = element_blank())
gg <- gg + theme(panel.grid.minor = element_blank())
gg <- gg + theme(axis.ticks.y=element_blank())
gg <- gg + theme(axis.text.x=element_text(size=10))
gg <- gg + theme(axis.text.y=element_text(size=12))
ggplotly(gg, tooltip = "text")
```
###Number of tags
```{r}
#Select tags per building for Ottawa, change the date of August 1 to August 30th to plot a monthly series. This would be controversial in a stastical analysis but we are simply plotting the data we already have on a monthly basis. The date of extraction in August does not matter as it was just a starting point to collect data. Changing it to 30th is just more convenient for the plot and does not affect the data.
OttTagsBuild <- data2 %>%
filter(cities=="Ottawa") %>%
select(cities, month, tagsBuild) %>%
mutate(month = ymd(month)) %>%
group_by(cities, m = month(month)) %>%
filter(month == max(month))
OttTagsBuild$MonthY <- format(as.Date(OttTagsBuild$month), "%b %Y")
OttTagsBuild$MonthY <- factor(OttTagsBuild$MonthY, levels = OttTagsBuild$MonthY[order(OttTagsBuild$tagsBuild)])
gg <- ggplot(OttTagsBuild, aes(x=tagsBuild, y=MonthY, text = comma(tagsBuild)))
gg <- gg + geom_segment(aes(xend = 0, yend=MonthY), color="#ececec")
gg <- gg + geom_point(color = "#3282bd", size = 2)
gg <- gg + scale_x_continuous(expand = c(0.1,0), labels = comma)
gg <- gg + labs(x = NULL, y = NULL)
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme_bw(base_family = "Helvetica")
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.grid.major = element_blank())
gg <- gg + theme(panel.grid.minor = element_blank())
gg <- gg + theme(axis.ticks.y=element_blank())
gg <- gg + theme(axis.text.x=element_text(size=10))
gg <- gg + theme(axis.text.y=element_text(size=12))
ggplotly(gg, tooltip = "text")
```
Gatineau {data-navmenu="Cities"}
=======================================================================
row
-----------------------------------------------------------------------
###Buildings {.value-box}
```{r, echo=FALSE}
buildGat <- data2 %>%
filter(cities == "Gatineau") %>%
top_n(1, month) %>%
select(buildings)
valueBox(comma(buildGat), icon = "ion-ios-home-outline", color = "#9ecae1")
```
###Users {.value-box}
```{r}
usersGat <- data2 %>%
filter(cities == "Gatineau") %>%
top_n(1, month) %>%
select(usersBuild)
valueBox(usersGat, icon = "ion-ios-people-outline", color = "#9ecae1")
```
###Average number of tags {.value-box}
```{r}
tagsGat <- data2 %>%
filter(cities == "Gatineau") %>%
top_n(1, month) %>%
select(averTagsBuild)
tagsGat$averTagsBuild <- round(tagsGat$averTagsBuild, 2)
valueBox(tagsGat, icon = "ion-ios-pricetags-outline", color = "#9ecae1")
```
row {data-height=400}
-----------------------------------------------------------------------
###Buildings mapped
```{r, echo=FALSE, message=FALSE}
GatBuild <- data2 %>%
filter(cities=="Gatineau") %>%
select(cities, month, buildings) %>%
group_by(cities, m = month(month)) %>%
filter(month == max(month))
GatBuild$MonthY <- format(as.Date(GatBuild$month), "%b %Y")
GatBuild$MonthY <- factor(GatBuild$MonthY, levels = GatBuild$MonthY[order(GatBuild$buildings)])
#Create a time series line chart
#With a geom_line plot, you need to add group = 1 if only one group of observations
#to avoid the warning message each is obs is one group
gg <- ggplot(GatBuild, aes(MonthY, buildings, group = 1, text = comma(buildings)))
gg <- gg + geom_line(position = "identity", color="#6baed6")
gg <- gg + scale_y_continuous(labels = comma)
gg <- gg + labs(x = NULL, y = NULL)
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme_bw(base_family = "Helvetica")
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.grid.major.x= element_blank())
gg <- gg + theme(panel.grid.major.y=element_line(linetype = "dashed"))
gg <- gg + theme(panel.grid.minor = element_blank())
gg <- gg + theme(axis.ticks=element_line(colour="#ececec"))
gg <- gg + theme(axis.text.x=element_text(size=12))
gg <- gg + theme(axis.text.y=element_text(size=10))
ggplotly(gg, tooltip = "text")
```
row {data-height=600}
-------------------------------------------------------------------
###Buildings by type
```{r}
buildTypeGat <- GatData2 %>%
group_by(building) %>%
summarise(numB = length(building)) %>%
arrange(desc(numB)) %>%
filter(numB > 50)
gg <- ggplot(buildTypeGat, aes(x=numB, y=reorder(building, numB), text = comma(numB)))
gg <- gg + geom_segment(aes(xend = 0, yend=building), color="#ececec")
gg <- gg + geom_point(color = "#3282bd", size = 2)
gg <- gg + scale_x_continuous(expand = c(0.1,0), labels = comma)
gg <- gg + labs(x = NULL, y = NULL)
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme_bw(base_family = "Helvetica")
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.grid.major = element_blank())
gg <- gg + theme(panel.grid.minor = element_blank())
gg <- gg + theme(axis.ticks.y=element_blank())
gg <- gg + theme(axis.text.x=element_text(size=9))
gg <- gg + theme(axis.text.y=element_text(size=12))
ggplotly(gg, tooltip = "text")
```
### Missing address fields in percentage
```{r}
MissAddGat <- GatData2 %>%
select(addr.street, addr.housenumber, addr.city, addr.postcode)
MeanAddrGat <- MissAddGat %>%
summarise_all(funs(mean(is.na(.))*100)) %>%
gather(Field, meanNA) %>%
mutate(Field = fct_recode(Field, "Street" = "addr.street",
"Street Number" = "addr.housenumber",
"City" = "addr.city",
"Postal Code" = "addr.postcode"))
gg <- ggplot(MeanAddrGat, aes(x=reorder(Field, meanNA), y=meanNA, text = round(meanNA, 2)))
gg <- gg + geom_segment(aes(xend = Field, yend=0), color="#ececec")
gg <- gg + geom_point(color = "#3282bd", size = 2)
gg <- gg + coord_flip()
gg <- gg + labs(x = NULL, y = NULL)
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme_bw(base_family = "Helvetica")
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.grid.major = element_blank())
gg <- gg + theme(panel.grid.minor = element_blank())
gg <- gg + theme(axis.ticks.y=element_blank())
gg <- gg + theme(axis.text.x=element_text(size=10))
gg <- gg + theme(axis.text.y=element_text(size=12))
ggplotly(gg, tooltip = "text")
```
###Number of tags
```{r}
GatTagsBuild <- data2 %>%
filter(cities=="Gatineau") %>%
select(cities, month, tagsBuild) %>%
mutate(month = ymd(month)) %>%
group_by(cities, m = month(month)) %>%
filter(month == max(month))
GatTagsBuild$MonthY <- format(as.Date(GatTagsBuild$month), "%b %Y")
GatTagsBuild$MonthY <- factor(GatTagsBuild$MonthY, levels = GatTagsBuild$MonthY[order(GatTagsBuild$tagsBuild)])
gg <- ggplot(GatTagsBuild, aes(x=tagsBuild, y=MonthY, text = comma(tagsBuild)))
gg <- gg + geom_segment(aes(xend = 0, yend=MonthY), color="#ececec")
gg <- gg + geom_point(color = "#3282bd", size = 2)
gg <- gg + scale_x_continuous(expand = c(0.1,0), labels = comma)
gg <- gg + labs(x = NULL, y = NULL)
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme_bw(base_family = "Helvetica")
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.grid.major = element_blank())
gg <- gg + theme(panel.grid.minor = element_blank())
gg <- gg + theme(axis.ticks.y=element_blank())
gg <- gg + theme(axis.text.x=element_text(size=10))
gg <- gg + theme(axis.text.y=element_text(size=12))
ggplotly(gg, tooltip = "text")
```
Ottawa-Gatineau {data-navmenu="Cities"}
=======================================================================
row
-----------------------------------------------------------------------
###Buildings {.value-box}
```{r, echo=FALSE}
buildOttGat <- data2 %>%
filter(cities == "Ott/Gat") %>%
top_n(1, month) %>%
select(buildings)
valueBox(comma(buildOttGat), icon = "ion-ios-home-outline", color = "#9ecae1")
```
###Users {.value-box}
```{r}
usersOttGat <- data2 %>%
filter(cities == "Ott/Gat") %>%
top_n(1, month) %>%
select(usersBuild)
valueBox(usersOttGat, icon = "ion-ios-people-outline", color = "#9ecae1")
```
###Average number of tags {.value-box}
```{r}
tagsOttGat <- data2 %>%
filter(cities == "Ott/Gat") %>%
top_n(1, month) %>%
select(averTagsBuild)
tagsOttGat$averTagsBuild <- round(tagsOttGat$averTagsBuild, 2)
valueBox(tagsOttGat, icon = "ion-ios-pricetags-outline", color = "#9ecae1")
```
row {data-height=400}
-----------------------------------------------------------------------
###Buildings mapped
```{r, echo=FALSE, message=FALSE}
OttBuildGat <- data2 %>%
filter(cities=="Ott/Gat") %>%
select(cities, month, buildings) %>%
group_by(cities, m = month(month)) %>%
filter(month == max(month))
OttBuildGat$MonthY <- format(as.Date(OttBuildGat$month), "%b %Y")
OttBuildGat$MonthY <- factor(OttBuildGat$MonthY, levels = OttBuildGat$MonthY[order(OttBuildGat$buildings)])
#Create a time series line chart
#With a geom_line plot, you need to add group = 1 if only one group of observations
#to avoid the warning message each is obs is one group
gg <- ggplot(OttBuildGat, aes(MonthY, buildings, group = 1, text = comma(buildings)))
gg <- gg + geom_line(position = "identity", color="#6baed6")
gg <- gg + scale_y_continuous(labels = comma)
gg <- gg + labs(x = NULL, y = NULL)
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme_bw(base_family = "Helvetica")
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.grid.major.x= element_blank())
gg <- gg + theme(panel.grid.major.y=element_line(linetype = "dashed"))
gg <- gg + theme(panel.grid.minor = element_blank())
gg <- gg + theme(axis.ticks=element_line(colour="#ececec"))
gg <- gg + theme(axis.text.x=element_text(size=12))
gg <- gg + theme(axis.text.y=element_text(size=10))
ggplotly(gg, tooltip = "text")
```
row {data-height=600}
-------------------------------------------------------------------
###Buildings by type
```{r}
buildTypeOttGat <- OttGatData2 %>%
group_by(building) %>%
summarise(numB = length(building)) %>%
arrange(desc(numB)) %>%
filter(numB > 50)
gg <- ggplot(buildTypeOttGat, aes(x=numB, y=reorder(building, numB), text = comma(numB)))
gg <- gg + geom_segment(aes(xend = 0, yend=building), color="#ececec")
gg <- gg + geom_point(color = "#3282bd", size = 2)
gg <- gg + scale_x_continuous(expand = c(0.1,0), labels = comma)
gg <- gg + labs(x = NULL, y = NULL)
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme_bw(base_family = "Helvetica")
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.grid.major = element_blank())
gg <- gg + theme(panel.grid.minor = element_blank())
gg <- gg + theme(axis.ticks.y=element_blank())
gg <- gg + theme(axis.text.x=element_text(size=9))
gg <- gg + theme(axis.text.y=element_text(size=12))
ggplotly(gg, tooltip = "text")
```
### Missing address fields in percentage
```{r}
MissAddOttGat <- OttGatData2 %>%
select(addr.street, addr.housenumber, addr.city, addr.postcode)
MeanAddrOttGat <- MissAddOttGat %>%
summarise_all(funs(mean(is.na(.))*100)) %>%
gather(Field, meanNA) %>%
mutate(Field = fct_recode(Field, "Street" = "addr.street",
"Street Number" = "addr.housenumber",
"City" = "addr.city",
"Postal Code" = "addr.postcode"))
gg <- ggplot(MeanAddrOttGat, aes(x=reorder(Field, meanNA), y=meanNA, text = round(meanNA, 2)))
gg <- gg + geom_segment(aes(xend = Field, yend=0), color="#ececec")
gg <- gg + geom_point(color = "#3282bd", size = 2)
gg <- gg + coord_flip()
gg <- gg + labs(x = NULL, y = NULL)
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme_bw(base_family = "Helvetica")
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.grid.major = element_blank())
gg <- gg + theme(panel.grid.minor = element_blank())
gg <- gg + theme(axis.ticks.y=element_blank())
gg <- gg + theme(axis.text.x=element_text(size=10))
gg <- gg + theme(axis.text.y=element_text(size=12))
ggplotly(gg, tooltip = "text")
```
###Number of tags
```{r}
OttGatTagsBuild <- data2 %>%
filter(cities=="Ott/Gat") %>%
select(cities, month, tagsBuild) %>%
mutate(month = ymd(month)) %>%
group_by(cities, m = month(month)) %>%
filter(month == max(month))
OttGatTagsBuild$MonthY <- format(as.Date(OttGatTagsBuild$month), "%b %Y")
OttGatTagsBuild$MonthY <- factor(OttGatTagsBuild$MonthY, levels = OttGatTagsBuild$MonthY[order(OttGatTagsBuild$tagsBuild)])
gg <- ggplot(OttGatTagsBuild, aes(x=tagsBuild, y=MonthY, text = comma(tagsBuild)))
gg <- gg + geom_segment(aes(xend = 0, yend=MonthY), color="#ececec")
gg <- gg + geom_point(color = "#3282bd", size = 2)
gg <- gg + scale_x_continuous(expand = c(0.1,0), labels = comma)
gg <- gg + labs(x = NULL, y = NULL)
gg <- gg + theme(strip.background=element_blank())
gg <- gg + theme_bw(base_family = "Helvetica")
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.grid.major = element_blank())
gg <- gg + theme(panel.grid.minor = element_blank())
gg <- gg + theme(axis.ticks.y=element_blank())
gg <- gg + theme(axis.text.x=element_text(size=10))
gg <- gg + theme(axis.text.y=element_text(size=12))
ggplotly(gg, tooltip = "text")
```
Data
========================================================
```{r}
datatable(data2, extensions = "Buttons",
options = list(dom="lfrtBip", buttons="csv"))
```
August 2016 {data-navmenu="Maps"}
========================================================
```{r, out.width = "1024px"}
knitr::include_graphics("Aug2016.jpg")
```
`r format(date, format='%B %Y')` {data-navmenu="Maps"}
========================================================
```{r, out.width = "1024px"}
knitr::include_graphics(MapDate)
```