Skip to content

Instantly share code, notes, and snippets.

@IndrajeetPatil
Last active February 23, 2020 19:49
Show Gist options
  • Save IndrajeetPatil/0351d415bb9a20c339c609f82865818c to your computer and use it in GitHub Desktop.
Save IndrajeetPatil/0351d415bb9a20c339c609f82865818c to your computer and use it in GitHub Desktop.
How foreign investment in the middle-east collapsed after the revolution
# setup
set.seed(123)
library(tidyverse)
library(hrbrthemes)
library(ggplot2)
library(ggrepel)
# make the dataframe
df <-
tibble::tribble(
~year, ~FDI, ~investment,
2006, "Inflows", 70043,
2007, "Inflows", 80291,
2008, "Inflows", 88502,
2009, "Inflows", 78283,
2010, "Inflows", 70086,
2011, "Inflows", 44842,
2012, "Inflows", 52719,
2013, "Inflows", 40509,
2014, "Inflows", 31629,
2015, "Inflows", 25690,
2016, "Inflows", 32430,
2017, "Inflows", 28711,
2006, "Outflows", 22761,
2007, "Outflows", 37401,
2008, "Outflows", 44187,
2009, "Outflows", 18923,
2010, "Outflows", 21100,
2011, "Outflows", 29546,
2012, "Outflows", 21573,
2013, "Outflows", 42498,
2014, "Outflows", 16752,
2015, "Outflows", 37251,
2016, "Outflows", 36225,
2017, "Outflows", 34984,
) %>%
dplyr::mutate(.data = ., year = as.factor(year)) %>%
dplyr::mutate(.data = ., investment = investment / 1000) %>%
dplyr::mutate(.data = ., event = dplyr::case_when(
year == "2010" & FDI == "Inflows" ~ "Arab Spring",
TRUE ~ NA_character_
))
# plot
p <-
ggplot(df, aes(year, investment, colour = FDI)) +
geom_point(
size = 4,
alpha = 0.7,
show.legend = FALSE
) +
geom_path(aes(color = FDI, group = FDI)) +
ggrepel::geom_label_repel(
aes(label = investment),
size = 4,
show.legend = FALSE,
family = "Roboto Condensed"
) +
geom_vline(
xintercept = "2010",
linetype = "dashed",
color = "#E7298AFF",
size = 1,
alpha = 0.5,
na.rm = TRUE
) +
geom_label(
data = df,
aes(x = "2010", y = 50, label = event),
inherit.aes = FALSE,
size = 5,
color = "#E7298AFF",
show.legend = FALSE,
fontface = "bold",
family = "Roboto Condensed"
) +
hrbrthemes::theme_ipsum_rc() +
scale_y_continuous(
breaks = seq(0, 100, 10),
labels = scales::dollar_format(suffix = "bn")
) +
scale_color_manual(
values = c("#1B9E77FF", "#D95F02FF"),
guide = guide_legend(
title = "",
direction = "horizontal",
label.position = "top",
nrow = 1
)
) +
labs(
title = "FDI inflows and outflows from MENA",
subtitle = "(MENA: the Greater Middle East)",
caption = substitute(paste(
italic("Source"), ": Annual Investment Climate Report (2018)"
)),
y = "Foreign Direct Investment (FDI)"
) +
theme(
legend.position = "top",
legend.justification = "left",
legend.text = ggplot2::element_text(size = 12, face = "bold"),
axis.title.x = ggplot2::element_text(size = 12, face = "bold"),
axis.title.y = ggplot2::element_text(size = 12, face = "bold"),
axis.text.x = ggplot2::element_text(size = 12, face = "bold"),
axis.text.y = ggplot2::element_text(size = 12, face = "bold"),
strip.text.x = ggplot2::element_text(size = 12, face = "bold"),
strip.text.y = ggplot2::element_text(size = 12, face = "bold"),
strip.text = ggplot2::element_text(size = 12, face = "bold"),
plot.title = ggplot2::element_text(
color = "black",
size = 16,
face = "bold"
),
plot.caption = ggplot2::element_text(
color = "black",
size = 13,
face = "plain"
)
)
# plot
plot(p)
# save the plot
ggplot2::ggsave(
plot = p,
filename = "fdi.png",
width = 11,
height = 9,
dpi = 300
)
@IndrajeetPatil
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment