Skip to content

Instantly share code, notes, and snippets.

@JanMarvin
Created February 21, 2021 20:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JanMarvin/c06f998fc64dcad7f353ead3970f6f52 to your computer and use it in GitHub Desktop.
Save JanMarvin/c06f998fc64dcad7f353ead3970f6f52 to your computer and use it in GitHub Desktop.
Exchange rates GBP, EUR and USD
library(reshape2)
library(ragg)
library(ggplot2)
# devtools::install_github("opensdmx/rsdmx")
library(rsdmx)
usd_eur <- readSDMX("https://sdw-wsrest.ecb.europa.eu/service/data/EXR/D.USD.EUR.SP00.A")
usd_eur <- as.data.frame(usd_eur)
usd_eur <- usd_eur[c("obsTime", "obsValue")]
names(usd_eur) <- c("date", "usd")
gbp_eur <- readSDMX("https://sdw-wsrest.ecb.europa.eu/service/data/EXR/D.GBP.EUR.SP00.A")
gbp_eur <- as.data.frame(gbp_eur)
gbp_eur <- gbp_eur[c("obsTime", "obsValue")]
names(gbp_eur) <- c("date", "gbp")
dd <- merge(gbp_eur, usd_eur)
names(dd) <- c("date", "EUR in GBP", "EUR in USD")
dd <- dd[complete.cases(dd),]
dd$date <- as.Date(dd$date)
dd[["GBP in USD"]] <- dd[["EUR in USD"]] / dd[["EUR in GBP"]]
dd[["GBP in EUR"]] <- 1/dd[["EUR in GBP"]]
# dd$usd_eur <- 1/dd$eur_usd
dd[["EUR in GBP"]] <- NULL
# dd$eur_usd <- NULL
mm <- melt(dd, id.vars = "date")
from_to <- paste0("(",
format(min(dd$date), "%b. %Y"),
" - ",
format(max(dd$date), "%b. %Y"),
")"
)
g <- ggplot(data = mm, aes(x = date, y = value, col = variable)) +
geom_line() +
theme_bw() +
theme(text = element_text(family = "DejaVu Math TeX Gyre")) +
labs(title = "Exchange rates GBP, EUR and USD",
subtitle = from_to,
caption = "Data Source: European Central Bank",
x = "Time",
y = "Exchange rates",
colour = "Currencies") +
geom_vline(
xintercept = c(as.Date("2016-06-23"),
as.Date("2017-07-08"),
as.Date("2019-12-12")),
linetype = c("dotted", "dashed", "dashed")) +
annotate(geom = "text",
label = c(as.character("Brexit Referendum (2016-06-23)"),
as.character("General Election (2017-07-08)"),
as.character("General Election (2019-12-12)")),
x = c(as.Date("2016-06-23"),
as.Date("2017-07-08"),
as.Date("2019-12-12")),
y = c(1.7, 1.7, 1.7),
angle = 90,
vjust = 1.5)
agg_png("exchange_rates.png", width = 4000, height = 1700, res = 300)
print(g)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment