Skip to content

Instantly share code, notes, and snippets.

@anbento0490
Last active January 19, 2019 19:01
Show Gist options
  • Save anbento0490/11c4d1bf3efb86def3858d15c1dfc9fb to your computer and use it in GitHub Desktop.
Save anbento0490/11c4d1bf3efb86def3858d15c1dfc9fb to your computer and use it in GitHub Desktop.
Extract EUR/GBP & EUR/USD rates from ECB website, combine them and plot with ggplot2
#1. Install and load packages
install.packages("rsdmx")
library(rsdmx)
library(dplyr)
library(ggplot2)
----------------------------------------------
#2. Get ECB EUR/GBP exchange rates
ecb_eur_to_gbp <- readSDMX("https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/gbp.xml")
ecb_eur_to_gbp <- as.data.frame(ecb_eur_to_gbp)
View(ecb_eur_to_gbp)
ecb_eur_to_gbp_v1 <- ecb_eur_to_gbp %>% select(TIME_PERIOD, OBS_VALUE) %>%
arrange(desc(TIME_PERIOD)) %>% rename(Date = TIME_PERIOD, Value = OBS_VALUE)
View(ecb_eur_to_gbp_v1)
ecb_eur_to_gbp_v1$Date <- as.Date(ecb_eur_to_gbp_v1$Date)
ecb_eur_to_gbp_v1$Value <- as.numeric(ecb_eur_to_gbp_v1$Value)
str(ecb_eur_to_gbp_v1)
ggplot(ecb_eur_to_gbp_v1, aes(x = Date, y = Value)) + geom_line(col = "blue") +
labs(title = "ECB EUR/GBP Rates 1999-2019") +
theme(plot.title = element_text(size = 15, face = "bold"))
#3. Get ECB EUR/USD exchange rates
ecb_eur_to_usd <- readSDMX("https://www.ecb.europa.eu/stats/policy_and_exchange_rates/euro_reference_exchange_rates/html/usd.xml")
ecb_eur_to_usd <- as.data.frame(ecb_eur_to_usd)
View(ecb_eur_to_usd)
ecb_eur_to_usd_v1 <- ecb_eur_to_usd %>% select(TIME_PERIOD, OBS_VALUE) %>%
arrange(desc(TIME_PERIOD)) %>% rename(Date = TIME_PERIOD, Value = OBS_VALUE)
View(ecb_eur_to_usd_v1)
ecb_eur_to_usd_v1$Date <- as.Date(ecb_eur_to_usd_v1$Date)
ecb_eur_to_usd_v1$Value <- as.numeric(ecb_eur_to_usd_v1$Value)
str(ecb_eur_to_usd_v1)
ggplot(ecb_eur_to_usd_v1, aes(x = Date, y = Value)) + geom_line(col = "red") +
labs(title = "ECB EUR/USD Rates 1999-2019") +
theme(plot.title = element_text(size = 15, face = "bold"))
#4.Creates combined dataset with both rates
ecb_rates_combined <- ecb_eur_to_gbp_v1 %>% left_join(ecb_eur_to_usd_v1, by = "Date") %>%
rename(eur_to_gbp_value = Value.x, eur_to_usd_value = Value.y)
View(ecb_rates_combined)
#5. Plot combined dataset filtering by date directly with ggplot2
ggplot(ecb_eur_to_usd_v1, aes(x = Date, y = Value)) + geom_line(col = "red") +
geom_line(data = ecb_eur_to_gbp_v1 , col = "blue") +
labs(title = "EUR/USD & EUR/GBP between 1999 and 2019") +
theme(plot.title = element_text(size = 15, face = "bold")) +
scale_x_date(date_breaks = "1 year",
limits = as.Date(c('2017-01-01','2019-01-01')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment