Plot_ly Package

Plotly is a platform to share and edit plots in a modular way trough the web. It’s on different programming language as javascript, python, R, and other APIs, all of this trough libraries adapted to the programming langanguage. More info here.

Instruction

Create a web page presentation using R Markdown that features a plot created with Plotly. Host your webpage on either GitHub Pages, RPubs, or NeoCities. Your webpage must contain the date that you created the document, and it must contain a plot created with Plotly. We would love to see you show off your creativity!

Context

For Colombia, the impresive changes in the oil price over the last years, has impacted strongly over the Representative Exchange Rate of US Dollar and Colombian Peso, at a point that has impacted negatively in the economy, because importing the goods that Colombia is used to import, has increase the price, and so on, the adquisition power for a colombian has decreased, leading to a desacceleration of the economy. In this order, we would like to explore the time serie of the oil price and the GSRER and look at the effect of the changing price of the oil. ### Loading libraries

suppressPackageStartupMessages(library(plotly))
suppressPackageStartupMessages(library(dplyr))

Loading the databases

trm <- read.csv("trm_diaria.csv", 
                col.names = c("Year", "Date", "TRM", "day", "Month", "MonthId"))
crudo <- read.csv("crudo.csv",
                  col.names = c("Date", "Last", "Open", "Max", "Min", "Vol", "Var"),
                  colClasses = c(rep("character", 7)))

preproccesing the data

trm$Date <- as.Date(trm$Date)
trm$TRM <- as.character(trm$TRM)
trm$TRM <- as.numeric(gsub(",",".", trm$TRM))
# Function to change the "," for a "."

commatoperiod <- function(x, n_col){
      if(!is.numeric(n_col)){
            stop("n_col must be numeric")
      }
      for(i in n_col:length(x)){
            x[,i] <- gsub(",", ".", x[,i])
      }
      return(x)
}

Turning some variables into numeric type variables

crudo <- commatoperiod(crudo, 2)
crudo$Vol <- as.numeric(gsub("K", "", crudo$Vol))*1000
## Warning: NAs introducidos por coerción
for(i in 2:length(crudo)){
      crudo[,i] <- as.numeric(crudo[,i])
}
## Warning: NAs introducidos por coerción

## Warning: NAs introducidos por coerción

## Warning: NAs introducidos por coerción

## Warning: NAs introducidos por coerción

Removing NANs

which(is.na(crudo$Last)) # Looking for the Nans
## [1] 5001 5002
crudo <- crudo[1:5000,]  # Removing the Nans

Changing date type variable

crudo$Date <- gsub("\\.","-",crudo$Date)
crudo$Date <- as.Date(crudo$Date, "%d-%m-%Y")

Plot_ly graph

BD <- merge(crudo, trm, by = "Date")

param <- list(
      tickfont = list(color = "black"),
      overlaying = "y",
      side = "right",
      title = "COP/USD"
)

p <- plot_ly(data = BD) %>%
      add_lines(x = ~Date, y = ~Open, name = "Oil price") %>%
      add_lines(x = ~Date, y = ~TRM, name = "GSRER COP/USD", yaxis = "y2")%>%
      layout(
            title = "Oil vs GSRER COP/USD", yaxis2 = param,
            xaxis = list(title="Date")
            )

Plottint

p