-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecreationalandsporting_goodsandservices.R
359 lines (258 loc) · 13.6 KB
/
recreationalandsporting_goodsandservices.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
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
# Consumer prices of recreational and sports
# https://ec.europa.eu/eurostat/statistics-explained/index.php?title=Consumer_prices_of_recreational_and_sporting_goods_and_services
library(restatapi)
library(ggplot2)
library(kableExtra)
library(reshape2)
library(openxlsx)
library(xlsx)
library(plotly)
#The all-items HICP and the HICPs for sporting goods and services, EU, 2013-2023 ---------------------------------------------------------------------------
#Choose dataset and filters here
dataset <- "prc_hicp_aind"
categories <- c("All-items HICP",
"Equipment for sport, camping and open-air recreation",
"Recreational and sporting services")
start_year <- 2013
end_year <- 2023
geo <- "EU27_2020"
unit <- "RCH_A_AVG"
#Plot title, subtitle, caption here
title <- "The all-items HICP and the HICPs for sporting goods and services, EU, 2013-2023"
subtitle <- "(% annual rate of change)"
caption = "Note: the data refer to the official EU aggregate. Its country coverage changes in line with the addition of new EU
Member States and integrates them using a chain-linked index formula.
Source: Eurostat (online data code: prc_hicp_aind)"
#Choose colors for plot here. Eurostat pallete B
euro_palette <- c("#B655BD", "#2644A7", "#B09120", "#672DC4", "#388AE2", "#AF155C")
palette <- c("All-items HICP" = euro_palette[1],
"Equipment for sport, camping and open-air recreation" = euro_palette[2],
"Recreational and sporting services" = euro_palette[3])
#CODE ---------------------
prc_hicp_aind_eu <- get_eurostat_data(dataset, filters = list(geo = geo, coicop = categories, unit = unit), date_filter=seq(start_year,end_year,1), label = T)
prc_hicp_aind_eu$time <- as.numeric(as.character(prc_hicp_aind_eu$time))
png("plot1.png")
plot1 <- ggplot(data = prc_hicp_aind_eu, aes(x = time, y = values, group = coicop, color = coicop)) +
geom_point(size = 2, shape = 21, aes(fill = coicop)) +
geom_line(linewidth = 1) +
labs(title = title,
subtitle = subtitle,
x = "",
y = subtitle,
caption = caption) +
theme_bw() +
theme(plot.title = element_text(size = 12, face = "bold", hjust = 0),
axis.title.y = element_text(size = 8),
axis.title.x = element_blank(),
legend.text = element_text(size = 10),
legend.position = "bottom",
legend.title = element_blank(),
legend.direction = "vertical",
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank(),
panel.border = element_blank(),
axis.line = element_line(color = "black"),
axis.text.x = element_text(angle = 60, hjust = 1),
plot.title.position = "plot",
plot.caption = element_text(size = 7, hjust = 0),
plot.caption.position = "plot") +
scale_y_continuous(limits = c(floor(min(prc_hicp_aind_eu$values)), ceiling(max(prc_hicp_aind_eu$values))), breaks = seq(floor(min(prc_hicp_aind_eu$values)), ceiling(max(prc_hicp_aind_eu$values)), 1)) +
scale_x_continuous(breaks = start_year:end_year, labels = function(x) ifelse(x %% 2 == 1, x, "")) +
scale_color_manual(values = palette) +
scale_fill_manual(values = palette)
print(plot1)
dev.off()
plot1
library(highcharter)
interactive_plot <- hchart(prc_hicp_aind_eu, "line", hcaes(x = time, y = values, group = coicop)) %>%
hc_title(text = title) %>%
hc_subtitle(text = subtitle) %>%
hc_xAxis(title = list(text = ""), categories = unique(prc_hicp_aind_eu$time), labels = list(format = "{value}")) %>%
hc_yAxis(title = list(text = subtitle), min = floor(min(prc_hicp_aind_eu$values)), max = ceiling(max(prc_hicp_aind_eu$values))) %>%
hc_tooltip(shared = TRUE, crosshairs = TRUE, pointFormat = '<b>{series.name}</b>: {point.y}<br/>') %>%
hc_legend(align = "center", verticalAlign = "bottom", layout = "vertical") %>%
hc_colors(euro_palette) %>%
hc_plotOptions(line = list(marker = list(symbol = "circle", enabled = TRUE)))
interactive_plot
#Harmonised indices of consumer prices for selected sporting goods and services ---------------------------------------------------------------------------
#Choose dataset and filters here
dataset <- "prc_hicp_aind"
group <- "All-items HICP"
categories <- c(group,
"Recreational and sporting services - Participation",
"Recreational and sporting services - Attendance",
"Equipment for camping and open-air recreation", "Equipment for sport","")
start_year_1 <- 2018
start_year_2 <- 2022
end_year <- 2023
geo <- "EU27_2020"
unit <- "RCH_A_AVG"
#Define plot title, subtitle and colors here
title <- "Harmonised indices of consumer prices for selected sporting goods and services, EU,\nannual average rates of change 2018-2023 and 2022-2023"
subtitle <- "(%)"
#color of year group 1 in each category
color_y1 <- "#B656BD"
#color of year group 2 in each category
color_y2 <- "#f0dcf1"
#color of year group 1 in all
color_y1_all <- "#2644A7"
#color of year group 2 in all
color_y2_all <- "#9cade8"
#CODE ---------------
prc_hicp_aind_rch_eu <- get_eurostat_data(dataset, filters = list(geo = geo, coicop = categories, unit = unit), date_filter=seq(start_year_1,end_year,1), label = T)
prc_hicp_aind_rch_eu$time <- as.numeric(as.character(prc_hicp_aind_rch_eu$time))
year_group_1 <- paste(start_year_1, end_year, sep = "-")
year_group_2 <- paste(start_year_2, end_year, sep = "-")
prc_hicp_aind_g1 <- prc_hicp_aind_rch_eu[time >= start_year_1+1 & time <= end_year, .(mean(values)), by = coicop]
names(prc_hicp_aind_g1)[2] <- year_group_1
prc_hicp_aind_g2 <- prc_hicp_aind_rch_eu[time >= start_year_2+1 & time <= end_year, .(mean(values)), by = coicop]
names(prc_hicp_aind_g2)[2] <- year_group_2
prc_hicp_aind_merged <- merge(prc_hicp_aind_g1, prc_hicp_aind_g2, on = "coicop")
prc_hicp_aind_merged <- reshape2::melt(prc_hicp_aind_merged, id.vars = "coicop")
group_year_group_1 <- paste(group, year_group_1)
group_year_group_2 <- paste(group, year_group_2)
categories_year_group_1 <- paste("Others", year_group_1)
categories_year_group_2 <- paste("Others", year_group_2)
prc_hicp_aind_merged$group <- ifelse(prc_hicp_aind_merged$coicop == group&prc_hicp_aind_merged$variable == year_group_1, group_year_group_1,
ifelse(prc_hicp_aind_merged$coicop == group&prc_hicp_aind_merged$variable == year_group_2, group_year_group_2,
ifelse(prc_hicp_aind_merged$variable == year_group_1, categories_year_group_1, categories_year_group_2)))
unique_coicop <- unique(as.character(prc_hicp_aind_merged$coicop))
unique_coicop <- unique_coicop[unique_coicop != group]
ordered_levels <- c(unique_coicop, group)
prc_hicp_aind_merged$coicop <- factor(prc_hicp_aind_merged$coicop, levels = ordered_levels)
colors <- c(group_year_group_1 = color_y1_all,
group_year_group_2 = color_y2_all,
categories_year_group_1 = color_y1,
categories_year_group_2 = color_y2)
names(colors) <- c(group_year_group_1, group_year_group_2, categories_year_group_1, categories_year_group_2)
png("plot2.png")
plot2 <- ggplot(prc_hicp_aind_merged, aes(x = coicop, y = value, fill = group)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.8), width = 0.7) +
labs(title = title,
subtitle = subtitle,
fill = "coicop",
caption = caption) +
scale_fill_manual(values = colors,
breaks = c(categories_year_group_1,categories_year_group_2),
labels = c(year_group_1,year_group_2)) +
theme_minimal() +
coord_flip() +
theme(legend.position = "bottom",
legend.direction = "horizontal",
legend.title = element_blank(),
axis.text.x = element_text(hjust = 1),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
plot.title = element_text(size = 12, face = "bold"),
plot.caption = element_text(size = 7, hjust = 0),
plot.caption.position = "plot",
panel.grid.major.y = element_blank(),
plot.title.position = "plot",
legend.justification = c(0,0)
) +
scale_y_continuous(breaks = seq(0,ceiling(max(prc_hicp_aind_merged$value)),1))
print(plot2)
dev.off()
plot2
#HICP for sporting goods and services – focus on countries ---------------------------------------------------------------------------
#Choose dataset and filters here
dataset <- "prc_hicp_aind"
categories <- c("All-items HICP",
"Equipment for sport",
"Equipment for camping and open-air recreation",
"Recreational and sporting services - Participation",
"Recreational and sporting services - Attendance"
)
start_year_1 <-2018
start_year_2 <- 2022
end_year <- 2023
unit <- "RCH_A_AVG"
#Define header colors here
color_a <- "#f0dcf1"
color_b <- "#e1bae4"
#CODE ------------------------
prc_hicp_aind <- get_eurostat_data(dataset, filters = list(unit = unit, coicop = categories), date_filter=seq(start_year_1,end_year,1), label = T)
prc_hicp_aind$time <- as.numeric(as.character(prc_hicp_aind$time))
pivot_table<- dcast(prc_hicp_aind, geo + coicop ~ time, value.var = "values", fun.aggregate = mean)
start_1_col <- which(names(pivot_table) == start_year_1) + 1
start_2_col <- which(names(pivot_table) == start_year_2) + 1
end_col <- which(names(pivot_table) == end_year)
pivot_table[, start_1_col:end_col] <- sapply(pivot_table[, start_1_col:end_col], as.numeric)
year_group_1 <- paste(start_year_1, end_year, sep = "-")
year_group_2 <- paste(start_year_2, end_year, sep = "-")
#Computing mean of year groups
if (ncol(pivot_table[, start_1_col:end_col, drop = FALSE]) == 1) {
pivot_table[[year_group_1]] <- pivot_table[, end_col]
} else {
pivot_table[[year_group_1]] <- round(rowMeans(pivot_table[, start_1_col:end_col], na.rm = TRUE),1)
}
if (ncol(pivot_table[, start_2_col:end_col, drop = FALSE]) == 1) {
pivot_table[[year_group_2]] <- pivot_table[, end_col]
} else {
pivot_table[[year_group_2]] <- round(rowMeans(pivot_table[, start_2_col:end_col], na.rm = TRUE),1)
}
#converting to format
pivot_table <- pivot_table[,c("geo","coicop", year_group_1, year_group_2)]
melted_df <- melt(pivot_table, id.vars = c("geo", "coicop"),
variable.name = "year_group",
value.name = "values")
melted_df <- dcast(melted_df, geo ~ coicop + year_group, value.var = "values", fun.aggregate = mean)
column_names <- names(melted_df)
#Filtering countries and create groups of countries
eu_ctry_names<-do.call(rbind,lapply(get("cc",envir=.restatapi_env)$EU28,search_eurostat_dsd,dsd=get_eurostat_dsd(dataset),exact_match=TRUE))$name
eu_ctry_names <- eu_ctry_names[-length(eu_ctry_names)]
efta_countries <- c("Iceland", "Liechtenstein", "Norway", "Switzerland")
eu_candidate_countries <- c("Albania", "Montenegro", "North Macedonia", "Serbia", "Turkey")
cntr <- as.factor(c("European Union - 27 countries (from 2020)", eu_ctry_names, "United Kingdom", efta_countries, eu_candidate_countries))
cntr <- factor(cntr, levels = cntr)
cntr_groups <- data.frame(geo = cntr,
groups = c("EU", rep("EU27", length(eu_ctry_names)), "UK", rep("EFTA", length(efta_countries)), rep("EU Candidate", length(eu_candidate_countries))))
final_df <- melted_df[(melted_df$geo %in% cntr), ]
final_df <- merge(final_df, cntr_groups, by = "geo", all.x = TRUE)
final_df <- final_df[order(factor(final_df$geo, levels = levels(cntr))), ]
groups_info <- final_df$groups
final_df<- final_df[-length(final_df)]
#Defining header
num_categories <- length(categories)
colnames(final_df) <- c("Countries", rep(c(year_group_1, year_group_2), times = num_categories))
header <- c("\u200B" = 1)
for (name in column_names) {
base_category <- sub("_.*", "", name)
if (base_category %in% categories) {
header[base_category] <- 2
}
}
#Creating table
kable_styling <- kable(final_df, format = "html", escape = F, digits = 1,
row.names = FALSE) %>%
add_header_above(header, background = color_a) %>%
row_spec(row = 0, background = color_a, extra_css = "white-space: nowrap; border: none;") %>%
row_spec(row = 1, background = color_b, bold = TRUE, extra_css = "border: none;") %>%
column_spec(1, bold=TRUE)
groups <- unique(groups_info)
group_indices <- split(seq_len(nrow(final_df)), groups_info)
for (group in groups) {
kable_styling <- kable_styling %>%
group_rows(" ", min(group_indices[[group]]), max(group_indices[[group]]),
label_row_css = "border-bottom: 1px solid black; margin: 0; padding: 0;")
}
kable_styling <- kable_styling %>%
kable_styling(bootstrap_options = c("striped", "hover"),
full_width = F, font_size = 12, fixed_thead = TRUE)
kable_styling
# EXCEL ---------------------------------------------------------------------------
wb <- createWorkbook()
sheet_1 <- createSheet(wb, sheetName = "HICP by year")
addPicture("plot1.png", sheet_1, scale = 1, startRow = 1, startColumn = 1)
sheet_2 <- createSheet(wb, sheetName = "Harmonised indices")
addPicture("plot2.png", sheet_2, scale = 1, startRow = 1, startColumn = 1)
sheet_3 <- createSheet(wb, sheetName = "Countries")
addDataFrame(final_df, sheet_3, row.names = FALSE, startRow = 1, startColumn = 1)
saveWorkbook(wb, "Consumer prices of recreational and sporting goods and services.xlsx")
#Export the table as html and png
library(webshot)
library(htmltools)
webshot::install_phantomjs(force = TRUE)
save_kable(kable_styling, file = "table1.html")
webshot("table1.html", "table1.png", selector = "table")