A first look at htmlwidgets

[This article was first published on Revolutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

by Joseph Rickert

A strong case can be made that base R graphics supplemented with either the lattice library or ggplot2 for plotting by subgroups provides everything a statistician might need for both exploratory data analysis and for developing clear, crisp for communicating results. However, it is abundantly clear that web based graphics, driven to a large extent by JavaScript enhanced web design, is opening up new vistas for data visualizations. The ability to interact with graphs, view them from different points of view, establish real-time relationships between different plots and other graphical elements provides opportunities to extract new insights from data. To be fair, many of these capabilities have existed in R for quite some time, some from the very beginning. For example, the identify() function in the graphics package lets you mouse over a point on a plot and click to determine the associated value, and what could be easier than the plot3d() function in the rgl package that uses OpenGL technology to let  you grab a #D scatter plot with your mouse and rotate it any which way. Run this code to see how it works.

library(rgl)
open3d()
attach(mtcars)
#head(mtcars)
plot3d(disp,wt,mpg, col = rainbow(10))

Developers are continuing to build out the infrastructure of web based graphics, and now it is possible to select environments that offer a rich set of features all in one place.

Until recently, however, making use of web based graphics directly from R required a basic knowledge of web based development and some JavaScript programming skills. If you have these skills, or want to acquire them, have a look at the V8 package which provides an R interface to Google's open source JavaScript engine, but if JavaScript programming is not going to be your thing then htmlwidgets is the way to go.

An R user can load a htmlwidgets library and generate a web based plot by calling a function that looks like any other R plotting function. For example, after installing and loading the three.js library, a few lines of code will produce an interactive 3D scatter plot that can be displayed in a webpage, a markdown document or in the RStudio plot window. The following code generates a more contemporary version of the rotating 3D scatterplot.

library(stringr)
library(htmltools)
install.packages("devtools",repos="http://cran.rstudio.com/")
 
devtools::install_github("bwlewis/rthreejs")
library(threejs)
 
data(mtcars)                 # load the mtcars data set
 
data <- mtcars[order(mtcars$cyl),] #sort the data set for plotting
head(data)
 
uv <- tabulate(mtcars$cyl)   # figure our how many observarions for each cylindar type
col <- c(rep("red",uv[4]),rep("yellow",uv[6]),rep("blue",uv[8])) #set the colors
 
row.names(mtcars)            #  see what models of cars are in the data set
 
scatterplot3js(data[,c(3,6,1)],
               labels=row.names(mtcars),    # mousing over a point will show what model car it is
               size=mtcars$hp/100,          # the size of a point maps to horsepower
               flip.y=TRUE,
               color=col,renderer="canvas") # point color indicates number of cylindars 

 

This kind of visualization packs a lot of information into a relatively small space. Not only does the ability to rotate the plot produce a satisfying 3 dimensional rendering, but using color, size and mouse movement to convey information provides three additional dimensions.

As exciting as this kind of visualization is, however, I don’t mean to imply that it is somehow going to make static graphics obsolete. Rob Kabacoff's 2012 post using the scatterplot3d package provides an example of a 3D scatterplot of the mtcars data that has a timeless, elegant look and clearly displays the data without distraction.

Nevertheless, I am betting on htmlwidgets moving forward to be the next big thing. Not only are they easy to use, but the developers have created a framework for developing new widgets that hides most of the details of JavaScript bindings and the like. Currently, there are only a few ready to use widgets listed at the htmlwidgets.org showcase. so we will have to see if the R community embraces this technology.

In the meantime, for inspiration, have a look at Bryan Lewis' presentation at the recent NY R conference and the examples of widgets listed on his last slide.

 

To leave a comment for the author, please follow the link and comment on their blog: Revolutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)