forked from groene/TK2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_cda.R
112 lines (76 loc) · 3.7 KB
/
check_cda.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
east_groninge <- c(9663L, 9665L, 9631L, 9640L, 9641L, 9642L, 9644L, 9645L, 9646L, 9648L, 9540L, 9541L, 9545L, 9550L, 9551L, 9560L, 9561L, 9563L, 9566L, 9695L, 9696L, 9697L, 9698L, 9699L, 9670L, 9671L, 9672L, 9673L, 9674L, 9675L, 9677L, 9678L, 9679L, 9681L, 9682L, 9684L, 9685L, 9686L, 9687L, 9688L, 9691L, 9693L, 9942L, 9943L, 9944L, 9500L, 9501L, 9502L, 9503L, 9580L, 9581L, 9584L, 9585L, 9591L, 9661L)
east_groninge <- as.character(east_groninge)
all_zip <- election_dat30 %>%
filter(type == "location") %>%
filter(location_type == "zips") %>%
mutate(total_spend_formatted = total_spend_pct*total_spend_formatted) %>%
filter(!is_exclusion) %>%
group_by(internal_id, total_spend_pct, party) %>%
mutate(n_together = n()) %>%
# select(page_id, value, num_ads, total_spend_formatted, n_together)
mutate(total_spend_formatted_old = total_spend_formatted) %>%
mutate(total_spend_formatted = total_spend_formatted/n_together) %>%
select(party, internal_id, value, n_together, total_spend_formatted, total_spend_formatted_old) %>%
ungroup() %>%
arrange(value) %>%
group_by(value, party) %>%
summarize(total_spend = sum(total_spend_formatted),
total_spend_old = sum(total_spend_formatted_old)) %>%
ungroup() %>%
# filter(party == "VVD") %>%
dplyr::mutate(pc4 = str_remove_all(value, ", Netherlands") %>% as.numeric) %>%
left_join(cbs %>%
janitor::clean_names())
all_zip %>%
filter(party == "CDA") %>%
filter(pc4 %in% east_groninge) %>% View()
# # Create the heading for the party
# cat("\n")
# cat("##### ", partyz, "\n")
# Prepare the data for plotting
party_data <- geo %>%
left_join(all_zip %>%
filter(party == "CDA") %>%
mutate(pc4_code = str_remove_all(value, ", Netherlands"))) %>%
filter(prov_name == "Groningen") %>%
mutate(east_gron = pc4 %in% east_groninge)
# Extract the color for the party
party_color <- color_dat %>%
filter(party == "CDA") %>%
pull(colors) %>%
first() # Make sure 'color' column has exactly one color per party
# Print the plot
# print(
ggplot(party_data) +
geom_sf(aes(fill = total_spend_old), colour = NA, size = 0.1) +
scale_fill_gradient2(low = 'lightgrey', high = party_color, na.value = 'lightgrey') +
theme_void() +
theme(legend.position = 'bottom') +
guides(fill = guide_colourbar(title = 'Spend (€)', barwidth = 10, barheight = 0.5)) +
geom_sf_label(aes(label = pc4, alpha = east_gron))
# )
length(east_groninge)
election_dat30 %>%
filter(type == "location") %>%
filter(location_type == "zips") %>%
mutate(total_spend_formatted = total_spend_pct*total_spend_formatted) %>%
filter(is_exclusion) %>%
group_by(internal_id, total_spend_pct, party) %>%
mutate(n_together = n()) %>%
# select(page_id, value, num_ads, total_spend_formatted, n_together)
mutate(total_spend_formatted_old = total_spend_formatted) %>%
mutate(total_spend_formatted = total_spend_formatted/n_together) %>%
select(party, internal_id, value, n_together, total_spend_formatted, total_spend_formatted_old) %>%
ungroup() %>%
arrange(value) %>%
dplyr::mutate(pc4 = str_remove_all(value, ", Netherlands") %>% as.numeric) %>%
mutate(east_gron = pc4 %in% east_groninge) %>% View()
election_dat30 %>%
filter(type == "location") %>%
filter(location_type == "zips") %>%
mutate(total_spend_formatted = total_spend_pct*total_spend_formatted) %>%
filter(party == "CDA") %>%
dplyr::mutate(pc4 = str_remove_all(value, ", Netherlands") %>% as.numeric) %>%
filter(pc4 == "9644") %>%
mutate(east_gron = pc4 %in% east_groninge) %>% View()
election_dat30 %>% filter(page_name == "D66") %>% View()