Site icon R-bloggers

My first… plot (.ly): beautiful plots with Plotly

Dear R-enthusiastic,
I discovered Plotly some days ago, and I was fascinated by it.

What is Plotly?
Plotly is a service for creating and sharing data visualizations that also offers statistical analysis tools plus a robust API, the ability to graph custom functions and a built-in Python shell. Among its APIs, there is the R one: Plotly interactive visualization can be created directly from R.

Why should I use Plotly?
With Plotly, R users can easily create a plot within R with a few lines of code and refine it with the online tool. Indeed, it took me a bit of clicking around to figure out how to do things like add a new visualization to existing data or group data by a specific factor, but many tasks were quite doable without having to read instructions or watch tutorial videos. In general, though, it did not take too much work to figure out a good portion of Plotly’s capabilities.

Why should I not use Plotly?
Some tasks are less intuitive, such as embedding a visualization into an external Web site, and also some workaround I found on the net doesn’t work for me. By the way, interactive plots can be embedded, like in this Washington Post article.
Another drawback of Plotly concerns privacy: it is not clear if I can produce a plot, embed it on my website but not show it to the world through the Plotly website. This will be fixed soon – do not forget this is a beta version – and Plotly will assume a Github like freemium model.

My first plot
The marketing department of a company wants to know how many records of its data base refer to customers and how many to prospect customers. Customers are split into (current) customers and dormant (former) customer.

First of all, I need to install the plotly package and its dependencies.

?View Code RSPLUS
# Required packages from CRAN
.pkgs = c("RJSONIO", "RCurl", "devtools")
 
# Install required packages from CRAN (if not)
.inst <- .pkgs %in% installed.packages()
if(length(.pkgs[!.inst]) > 0) install.packages(.pkgs[!.inst])
 
# Install plotly package from Github
if(! "plotly" %in% installed.packages()) {
  require(devtools)
  devtools::install_github("plotly/R-api")
}
 
# Load package
library(plotly)

At this point, I should sign up to Plotly and get an API key. Then, I connect to Plotly from R.

?View Code RSPLUS
py <- plotly(username = usr, key = api)

where usr and < api>api are strings containing my username and API key, respectively.

Now I have to build some lists with data to be plotted and plot layout

?View Code RSPLUS
customer <- list(
  name = 'Customer',
  x = c('All Customers'),
  y = c(5000),
  type = 'bar',
	marker = list(
    color = 'rgb(9,173,66)',
    line = list(color = 'grey', width = 3)
	)
)
 
dormant <- list(
  name = 'Dormant',
  x = c('All Customers'),
  y = c(3000),
  type = 'bar',
  marker = list(
    color = 'rgb(255,165,0)',
    line = list(color = 'grey', width = 3)
	)
)
 
prospect <- list(
  name = 'Prospect',
  x = c('Prospect'),
  y = c(4000),
  type = 'bar',
  marker = list(
    color = 'rgb(255,0,0)',
    line = list(color = 'grey', width = 3)
  )
)
 
l <- list(
  title = 'End User',
  autosize = FALSE,
  width = 550,
  height = 550,
  showlegend = TRUE,
  xaxis = list(
    zeroline = FALSE
  ),
  yaxis = list(
    type = 'category',
    zeroline = FALSE
  ),
  categories = categories,
  barmode = 'stack',
  bargap = 0.25,
  bargroupgap = 0.3,
  bardir = 'v'
)

At this point, I pass all these information to Plotly.

?View Code RSPLUS
response <- py$plotly(list(customer, dormant, prospect), kwargs = list(layout=l))

Within my R, I can open my favorite browser to see the results.

?View Code RSPLUS
system(paste("google-chrome", response$url))

Blog readers can view the results below, as exported to PNG, or the interactive version in the Plotly website.

View the interactive version in the Plotly website

How much time consuming is Plotly?
I needed about half an hour to learn how Plotly works and get the API, another half an hour to produce the plot and more than an hour to learn how embed and/or share the plot.

Finally, I am sure I will spend some words about Plotly on the “Programming with R” course that Quantide has organized in Milan, on January 30-31. For more information about this course, please visit the Quantide website.