TrelliscopeJS with Plotly

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.

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:

gapminder_lifeexp
life expectancy vs. year by country using Gapminder data
1 - 12 of 142
Prev
Next
First
Last
196019802000255075
yearlifeExp
country
Afghanistan
continent
Asia
196019802000255075
yearlifeExp
country
Albania
continent
Europe
196019802000255075
yearlifeExp
country
Algeria
continent
Africa
196019802000255075
yearlifeExp
country
Angola
continent
Africa
196019802000255075
yearlifeExp
country
Argentina
continent
Americas
196019802000255075
yearlifeExp
country
Australia
continent
Oceania
196019802000255075
yearlifeExp
country
Austria
continent
Europe
196019802000255075
yearlifeExp
country
Bahrain
continent
Asia
196019802000255075
yearlifeExp
country
Bangladesh
continent
Asia
196019802000255075
yearlifeExp
country
Belgium
continent
Europe
196019802000255075
yearlifeExp
country
Benin
continent
Africa
196019802000255075
yearlifeExp
country
Bolivia
continent
Americas
Sorting on:
country
continent

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.

Avatar
Ryan Hafen
Data Scientist, Statistical Consultant

Related