-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.2 capital_flows.R
80 lines (56 loc) · 2.48 KB
/
1.2 capital_flows.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
####### Script to produce capital flows figure
####### Note: for other regions just change sheet filter
# Basic cleaning flows and reserves data: ------
# Note: removed 2021-Q1 because data not available for all countries
paths=c("~/Dropbox/APD/APD_material/raw_data/financial_capital_flows.xlsx",
"~/Dropbox/APD/APD_material/raw_data/financial_reserves.xlsx")
list_flows <- paths %>%
map(~ read_xlsx(.x, sheet = "APD")) %>%
map(~ .x %>% mutate(country.code = as.character(country.code))) %>%
map(~ .x %>% filter(country.code %in% apd_list_countries)) %>%
map(~ .x %>% select(-`2021-Q1`)) %>%
map(~ .x %>% gather("quarter","value",`2005-Q1`:`2020-Q4`)) %>%
map(~ .x %>% mutate(value = as.numeric(value)))
# Change reserves data from stock to flow: ------
list_flows[[2]] <- list_flows[[2]] %>%
group_by(country.code) %>%
mutate(value_flow = -(value - dplyr::lag(value,1))) %>%
mutate(`Type Inflow` = "International Reserves") %>%
select(-value) %>%
select(country, country.code, `Type Inflow`,everything()) %>%
setNames(c("country","country.code","Type Inflow", "Indicator","Unit","quarter","value")) %>%
ungroup()
# Calculate sum by instrument type and quarter: ------
df <-list_flows[[1]] %>%
filter(country != "Japan") %>%
filter(quarter >= "2014-Q1") %>%
group_by(quarter, `Type Inflow`) %>%
summarise(sum = sum(value, na.rm = T)/1000) %>%
group_by(quarter) %>%
mutate(total = sum(sum))
# Plot evolution over time: ------
df %>%
ggplot(aes(`quarter`,sum, fill = `Type Inflow`)) +
geom_col(width = 0.4) +
geom_line(aes(y=total, group = 1, col = "Total"), size = 1.5) +
xlab("") +
ylab("USD Billions") +
labs(fill = "", col = "") +
scale_fill_manual(values = c("#4472C4","#ED7D31","#92D050","grey")) +
theme_minimal() +
scale_color_manual(values = "black") +
scale_x_discrete(breaks = map_chr(2014:2020, ~ paste0(.x,"-Q1"))) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
theme(legend.position = "bottom",
legend.text = element_text(size = 14)) +
theme(axis.text.x = element_text(size = 18),
axis.title.x = element_blank(),
axis.text.y = element_text(size = 18),
axis.title.y = element_text(size = 22))
# Export intermediate data:
df %>%
rio::export("../APD_material/intermediate_data/replication_figures/gross_capital_flows_figure.xlsx")
# Export figure:
ggsave("../APD_material/output/figures/gross_capital_flows.pdf",
height = 7,
width = 12.5)