Dynamic graphs with plotly and ggvis (using BAMC data)

I will look at two ways to generate dynamic graphs from the data scraped from the BAMC about “bad” credits. The first is the plotly R package which uses the ggplot2 package and extends it with some “dynamic” features. And the second is the freshly new and under development ggvis R package.

The following code scrapes the data form the website of BAMC and saves it into data frames. Prerequisites: load the function getCreditData() and the required libraries mentioned in the previous post.

# get data from website
dataNLB <- getCreditData("NLB")
dataNKBM <- getCreditData("NKBM")

First the plotly package.

plotly

library(plotly)
library(ggplot2)
library(scales)
# set the credentials (commented out beacase they are made up)
# set_credentials_file(username="username", api_key="99999999")
py <- plotly()

# function to get top x contracts by value ####
gettopContracts <- function (data, topN) {
  dataSubset <- head(data[order(data[,c("ZnesekEUR")], decreasing = TRUE),], topN) # top contracts dataset
  dataSubset <- dataSubset[!duplicated(dataSubset[,"St.pogodbe"]),] # remove duplicates of contract numbers
  return(dataSubset)
}

# helper function to format decimal separator ####
dot <- function(x) {gsub(pattern=",", replacement=".", x=as.character(comma(x, decimal.mark=",")))}

# get data for top N contracts
N <- 100
dataSubsetNLB <- gettopContracts(dataNLB, N)
dataSubsetNKBM <- gettopContracts(dataNKBM, N)

# plot NLB top contracts
ggPlot <- ggplot(data=dataSubsetNLB, aes(x=Naziv, y=ZnesekEUR)) + geom_bar(stat="identity") + theme_bw() +
  scale_x_discrete(limits=unique(dataSubsetNLB[,"Naziv"])) + scale_y_continuous(labels=dot) + 
  ggtitle(paste("Just an example plot (NLB)")) + theme(axis.text.x = element_text(angle = 90, hjust = 1))
print(ggPlot) # print the "original" ggplot2

plot of chunk unnamed-chunk-4

# Try to print a dynamic ggplot in plotly
# r <- py$ggplotly(ggPlot)
# r$response$url # go to this URL for the result

The ggplot2 “extension” does not seem to work as I would expect… The first time the plot [was weird][https://plot.ly/~crtahlin/2], now it doesn’t finish in decent time. That’s why above code is commented out. I won’t spend more time on the plotly package, but will rather invest it in investigating the ggvis package.

ggvis

library(ggvis)

# drop levels that have no values from the data subset
# (otherwise ggvis complains)
dataSubsetNLB <- (droplevels(dataSubsetNLB))

# function to return the names of companies for the hover tooltip
comapanyNames <- function(x) {
  if(is.null(x)) return(NULL)
  paste0(x[1], collapse = "<br />")
}

# plot a ggvis graphic similar to the ggplot one; add a simple hover tooltip
ggvisObject <- dataSubsetNLB %>% ggvis(~Naziv, ~ZnesekEUR) %>% layer_bars() %>%
  add_axis("x",properties = axis_props(labels=list(angle=90, align="left")) ) %>% add_tooltip(comapanyNames, "hover")

# knit this object (neccessary for HTML blog post)
knit_print.ggvis(ggvisObject)

It’s relatively easy to make a plot comparable to ggplot2, with the added functionality of basic tooltip on mouse hover (not visible in blog post - it needs a running R session). Except for adding the title of the plot, which seems not to be implemented yet :) Note the option to resize the plot via the button in bottom right corner and the option to save the plot in SVG or PNG format.

Note: I have decided to post on RPubs, since it is easiest. I have tried embedding graphs in Blogger in on github.io, but had little success.