library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.1 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(jsonlite)
##
## Attaching package: 'jsonlite'
## The following object is masked from 'package:purrr':
##
## flatten
makeRequest <- function(day) {
req <- str_glue(
"https://openexchangerates.org/api/historical/{day}.json?app_id={key}",
day = day, key = Sys.getenv("OER_KEY")
)
}
res <- fromJSON(makeRequest("2019-08-15"))
glimpse(res)
## List of 5
## $ disclaimer: chr "Usage subject to terms: https://openexchangerates.org/terms"
## $ license : chr "https://openexchangerates.org/license"
## $ timestamp : int 1565913599
## $ base : chr "USD"
## $ rates :List of 171
## ..$ AED: num 3.67
## ..$ AFN: num 78.4
## ..$ ALL: num 109
## ..$ AMD: num 476
## ..$ ANG: num 1.78
## ..$ AOA: num 362
## ..$ ARS: num 57.2
## ..$ AUD: num 1.48
## ..$ AWG: num 1.8
## ..$ AZN: num 1.7
## ..$ BAM: num 1.75
## ..$ BBD: int 2
## ..$ BDT: num 84.5
## ..$ BGN: num 1.76
## ..$ BHD: num 0.377
## ..$ BIF: num 1843
## ..$ BMD: int 1
## ..$ BND: num 1.35
## ..$ BOB: num 6.91
## ..$ BRL: num 3.99
## ..$ BSD: int 1
## ..$ BTC: num 9.7e-05
## ..$ BTN: num 71.3
## ..$ BWP: num 11.1
## ..$ BYN: num 2.05
## ..$ BZD: num 2.02
## ..$ CAD: num 1.33
## ..$ CDF: num 1654
## ..$ CHF: num 0.976
## ..$ CLF: num 0.0249
## ..$ CLP: num 711
## ..$ CNH: num 7.05
## ..$ CNY: num 7.03
## ..$ COP: num 3472
## ..$ CRC: num 571
## ..$ CUC: int 1
## ..$ CUP: num 25.8
## ..$ CVE: num 98.9
## ..$ CZK: num 23.3
## ..$ DJF: num 178
## ..$ DKK: num 6.71
## ..$ DOP: num 51.1
## ..$ DZD: num 120
## ..$ EGP: num 16.6
## ..$ ERN: num 15
## ..$ ETB: num 29.2
## ..$ EUR: num 0.9
## ..$ FJD: num 2.17
## ..$ FKP: num 0.827
## ..$ GBP: num 0.827
## ..$ GEL: num 2.92
## ..$ GGP: num 0.827
## ..$ GHS: num 5.42
## ..$ GIP: num 0.827
## ..$ GMD: num 50.1
## ..$ GNF: num 9185
## ..$ GTQ: num 7.67
## ..$ GYD: num 209
## ..$ HKD: num 7.84
## ..$ HNL: num 24.5
## ..$ HRK: num 6.65
## ..$ HTG: num 95
## ..$ HUF: num 293
## ..$ IDR: num 14354
## ..$ ILS: num 3.53
## ..$ IMP: num 0.827
## ..$ INR: num 71.4
## ..$ IQD: num 1193
## ..$ IRR: int 42105
## ..$ ISK: num 124
## ..$ JEP: num 0.827
## ..$ JMD: num 135
## ..$ JOD: num 0.707
## ..$ JPY: num 106
## ..$ KES: num 103
## ..$ KGS: num 69.6
## ..$ KHR: num 4080
## ..$ KMF: num 440
## ..$ KPW: int 900
## ..$ KRW: num 1212
## ..$ KWD: num 0.304
## ..$ KYD: num 0.833
## ..$ KZT: num 387
## ..$ LAK: num 8704
## ..$ LBP: num 1512
## ..$ LKR: num 177
## ..$ LRD: int 203
## ..$ LSL: num 15.3
## ..$ LYD: num 1.4
## ..$ MAD: num 9.6
## ..$ MDL: num 17.3
## ..$ MGA: num 3688
## ..$ MKD: num 55.1
## ..$ MMK: num 1517
## ..$ MNT: num 2665
## ..$ MOP: num 8.08
## ..$ MRO: int 357
## ..$ MRU: num 36.7
## ..$ MUR: num 36
## .. [list output truncated]
rates <- as_tibble(res$rates) %>%
mutate(date = as.POSIXct(res$timestamp, origin = "1970-01-01")) %>%
pivot_longer(cols = -date, names_to = "currency", values_to = "value")
rates
## # A tibble: 171 x 3
## date currency value
## <dttm> <chr> <dbl>
## 1 2019-08-16 09:59:59 AED 3.67
## 2 2019-08-16 09:59:59 AFN 78.4
## 3 2019-08-16 09:59:59 ALL 109.
## 4 2019-08-16 09:59:59 AMD 476.
## 5 2019-08-16 09:59:59 ANG 1.78
## 6 2019-08-16 09:59:59 AOA 362.
## 7 2019-08-16 09:59:59 ARS 57.2
## 8 2019-08-16 09:59:59 AUD 1.48
## 9 2019-08-16 09:59:59 AWG 1.80
## 10 2019-08-16 09:59:59 AZN 1.70
## # ... with 161 more rows
getDay <- function(day) {
url <- makeRequest(day)
res <- fromJSON(url)
rates <- as_tibble(res$rates)
rates <- mutate(rates,
date = as.POSIXct(res$timestamp, origin = "1970-01-01"))
rates <- pivot_longer(rates, cols = -date, names_to = "currency", values_to = "value")
rates
}
rates <- getDay("2019-08-15")
library(lubridate)
## Warning: package 'lubridate' was built under R version 4.0.5
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
# create a vector of all days in August 2018
august <- seq(as_date("2018-08-01"), as_date("2018-08-31"), by = 1)
aug_rates <- map_dfr(august, getDay)
some_currencies <- aug_rates %>%
filter(currency %in% c("AUD", "NZD", "JPY", "EUR", "GBP"))
library(ggplot2)
ggplot(data = some_currencies, aes(x = date, y = value, color = currency)) +
geom_line()
Create a line plot to compare the USD exchange rates for AUD, EUR, GBP, NZD, JPY over the month.
Create a facetted line plot to compare the USD exchange rates for AUD, EUR, GBP, NZD, JPY over the month.
library(ggplot2)
ggplot(data = some_currencies, aes(x = date, y = value)) +
geom_line() +
facet_wrap("currency")
## Remove JPY so that detail can be seen for the other currencies
ggplot(data = filter(some_currencies, currency != "JPY"), aes(x = date, y = value)) +
geom_line() +
geom_hline(yintercept = 1, linetype = "dashed", color = "darkgrey") +
facet_wrap("currency")