Posted in R

Oil Price Trend in South African Rands – R

Rplot.pngSouth Africa has recently being experiencing some hikes in the petrol price. We're paying more for petrol now that oil is under $50 a barrel then we were when it was over a $100 a barrel.

We haven’t been able to take advantage of the low oil prices because our currency has weakened considerably since last year.

Below is an R script for plotting the oil price in ZAR using the Quandl package for exchange rates and oil prices. The output shows that, even factoring in the weakened currency, our petrol price should look better than it did in 2014. This gives an indication of the effect of taxes and levies on the petrol price, and the risk that the country is in if the oil price goes back to 2014 levels.

First the packages

Quandl has a great repository of free and paid data sets, which can be consumed via Python, R, Excel, and even through your browser. I used this package to source historic exchange rates and oil prices.

The Lucr package accesses more recent exchange rate information. It uses the Open Exchange Rates site. You need a key. Luckily there’s a free option.

The ggplot2 package provides the graph plotting.



Get the exchange rates

lucr.key <- "GeTyOurOwNkEy"

###Get Historic exchange data <- Quandl("FRED/DEXSFUS")

#Add latest exchange rate <- currency_convert(1, from = "USD", to = "ZAR", key = lucr.key) <- data.frame(Sys.Date(),
names( <- c("DATE","VALUE") <- rbind(, <-[order($DATE),]



Get the oil price in USD

###Get historic oil prices
stg.oil.prices <- Quandl("CHRIS/CME_CL1") #Quandl("OPEC/ORB")
stg.oil.prices <- data.frame(stg.oil.prices$Date,stg.oil.prices$Last)
names(stg.oil.prices) <- c("Date","Value")

#Carry latest oil price to latest exchange rate

stg.oil.price.carryforward <- data.frame($DATE,
                                          stg.oil.prices[stg.oil.prices$Date == max(stg.oil.prices$Date),2]
names(stg.oil.price.carryforward) <- c("Date","Value")

oil.prices <- rbind(stg.oil.prices, stg.oil.price.carryforward)


Because Quandl exchange rates for South Africa are a bit behind (about a week),
I took the latest currency from lucr and applied that to the latest oil price
that I received from Quandl (the previous day).

###Oil Price in ZAR
stg.oil.prices.ZAR <- merge(x = oil.prices, y =, by.x = "Date", by.y = "DATE")

names(stg.oil.prices.ZAR) <- c("Date", "Oil.Price","ZAR.per.Dollar")

oil.prices.ZAR <- data.frame(
  stg.oil.prices.ZAR$Oil.Price * stg.oil.prices.ZAR$ZAR.per.Dollar,
names(oil.prices.ZAR) <- c("Date","Oil.Price.ZAR","Oil.Price.USD","Exchange.Rate")

Here’s a sample set of rows:
Then we’ll use the ggplot2 library to plot our graphs: <- "2005-01-01"

display.oil.prices.ZAR.long <- oil.prices.ZAR[oil.prices.ZAR$Date >=,] <- "2013-01-01"

display.oil.prices.ZAR.short <- oil.prices.ZAR[oil.prices.ZAR$Date >=,]

qplot(x = display.oil.prices.ZAR.long$Date, y = display.oil.prices.ZAR.long$Oil.Price.ZAR,
      xlab = "Date", ylab = "Oil Price per Barrel (ZAR)", main = "Oil Price Over Time",
      data = display.oil.prices.ZAR.long,
      colour = -Exchange.Rate,
      geom = "line")

qplot(x = display.oil.prices.ZAR.short$Date, y = display.oil.prices.ZAR.short$Oil.Price.ZAR,
      xlab = "Date", ylab = "Oil Price per Barrel (ZAR)", main = "Oil Price Over Time",
      data = display.oil.prices.ZAR.short,
      colour = -Exchange.Rate,
      geom = "line")

The “long” plot


The “short” plot


It looks like levies and taxes are playing a large part on the petrol price. Even with the South African Rand’s weak rate, we’re in a better position than we were in 2014.

Next, I’ll take advantage of PowerBI’s R capabilites by using the R script as a data layer, and leveraging PowerBI’s visuals to communicate the information.