-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.R
More file actions
111 lines (102 loc) · 2.51 KB
/
visualization.R
File metadata and controls
111 lines (102 loc) · 2.51 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
library(dplyr)
library(leaflet)
library(rgdal)
load('dogs.RData')
######################################
# Owner Age - Dog Breed Relationship #
######################################
# Generate breeds table for totals
breeds <- table(breed=dogs2020$BREED, age=dogs2020$AGE)
breeds <- cbind(breeds, total = rowSums(breeds)) %>%
as.data.frame()
# Use pie charts to visualise
par(mfrow = c(2,5))
pie(breeds$`11-20`, dogs2020$BREED, main="11-20")
pie(breeds$`21-30`, dogs2020$BREED, main="21-30")
pie(breeds$`31-40`, dogs2020$BREED, main="31-40")
pie(breeds$`41-50`, dogs2020$BREED, main="41-50")
pie(breeds$`51-60`, dogs2020$BREED, main="51-60")
pie(breeds$`61-70`, dogs2020$BREED, main="61-70")
pie(breeds$`71-80`, dogs2020$BREED, main="71-80")
pie(breeds$`81-90`, dogs2020$BREED, main="81-90")
pie(breeds$`91-100`, dogs2020$BREED, main="91-100")
pie(breeds$total, dogs2020$BREED, main="All ages")
# Delete generated table
rm(breeds)
zh_rg <- readOGR("./data_sources/stzh.adm_stadtkreise_v.json")
# group sub-districts together
list_districts <- list(
District_1 <- c(
"Rathaus",
"Hochschulen",
"Lindenhof",
"City"
),
District_2 <- c(
"Wollishofen",
"Leimbach",
"Enge"
),
District_3 <- c(
"Alt-Wiedikon",
"Friesenberg",
"Sihlfeld"
),
District_4 <- c(
"Werd",
"Langstrasse",
"Hard"
),
District_5 <- c(
"Gewerbeschule",
"Escher Wyss"
),
District_6 <- c(
"Unterstrass",
"Oberstrass",
"Unterstrass"
),
District_7 <- c(
"Fluntern",
"Hottingen",
"Hirslanden",
"Witikon"
),
District_8 <- c(
"Seefeld",
"Mühlebach",
"Weinegg"
),
District_9 <- c(
"Albisrieden",
"Altstetten"
),
District_10 <- c(
"Höngg",
"Wipkingen"
),
District_11 <- c(
"Affoltern",
"Oerlikon",
"Seebach"
),
District_12 <- c(
"Saatlen",
"Schwamendingen-Mitte",
"Hirzenbach"
)
)
avg_wealth <- unlist(lapply(seq_len(length(list_districts)), function (z) {
mean(unlist(lapply(list_districts[[z]], function (y) {
mean(unlist(lapply(y, function (x) {
dogs2020[which(x == dogs2020$DISTRICT_NAME),]$WEALTH_T_CHF
})))
})))
}))
bins <- c(0, 25, 30, 50, 60, 75, 100, 120, 150, 175)
pal <- colorBin("Greens", domain = avg_wealth, bins = bins)
leaflet(zh_rg) %>%
addPolygons(fillColor = ~pal(unlist(avg_wealth)), weight = 2, fillOpacity = 0.9,
opacity = 1) %>%
addTiles() %>%
addLegend(colors = pal(unlist(avg_wealth)), labels = zh_rg$kname, title = "Zurich Districts", opacity = 1)