Site icon R-bloggers

TrelliscopeJS with Plotly

[This article was first published on Ryan Hafen, 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.

In response to a user’s request and after a short conversation with Carson Sievert (creator / maintainer of the plotly R package), I recently made a small tweak to TrelliscopeJS to make it very easy to create Trelliscope displays with interactive Plotly panels. A simple example is shown in this post.

< !--more-->

If you missed earlier posts on TrelliscopeJS, it is an R package that brings faceted visualizations to life while plugging in to common analytical workflows like ggplot2 or the “tidyverse”, allowing full latitude in what underlying plotting library is used (ggplot2, lattice, htmlwidgets). Since the R plotly package is an htmlwidget, it plugs in very easily.

To run this example, you’ll need the following packages installed. Note that you reinstall TrelliscopeJS to get the updated functionality.

devtools::install_github("hafen/trelliscopejs")
install.packages(c("gapminder", "plotly", "ggplot2"))

library(trelliscopejs)
library(ggplot2)
library(plotly)
library(gapminder)

There is a new parameter in facet_trelliscope(), as_plotly, that if set to TRUE will simply convert each ggplot2 panel to an interactive Plotly plot.

qplot(year, lifeExp, data = gapminder) +
  xlim(1948, 2011) + ylim(10, 95) + theme_bw() +
  facet_trelliscope(~ country + continent,
    nrow = 2, ncol = 6, width = 300, as_plotly = TRUE)

Pretty simple. The output will look like this:

If this display doesn’t appear correctly for you (because of blog aggregators, etc.), you can follow this link to the display in a dedicated window. For better viewing, you can also click the bottom right “fullscreen” button to expand the display to fill the window.

Note that the code above is identical to what you would write with ggplot2 except that you simply swap facet_wrap() for facet_trelliscope() and add a few extra parameters to the facet command to specify the initial layout of the display.

Also note that for even more flexibility you can also create Trelliscope displays with Plotly panels by adding a Plotly object list-column to a data frame and using the trelliscope() interface. You can see an example of this idea using rbokeh in my first blog post on TrelliscopeJS.

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

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.