1 Two approaches, one object
There are two main ways to initiate a plotly object in R. The
plot_ly() function transforms data into a plotly object, while the
ggplotly() function transforms a ggplot object into a plotly object (Wickham 2009); (Sievert et al. 2016). Regardless of how a plotly object is created, printing it results in an interactive web-based visualization with tooltips, zooming, and panning enabled by default. The R package also has special semantics for arranging, linking, and animating plotly objects. This chapter discusses some of the philosophy behind each approach, explores some of their similarities, and explains why understanding both approaches is extremely powerful.
The initial inspiration for the
plot_ly() function was to support plotly.js chart types that ggplot2 doesn’t support, such as 3D surface and mesh plots. Over time, this effort snowballed into an interface to the entire plotly.js graphing library with additional abstractions inspired by the grammar of graphics (Wilkinson 2005). This newer “non-ggplot2” interface to plotly.js is currently not, and may never be, as fully featured as ggplot2. Since we can already translate a fairly large amount of ggplot objects to plotly objects, I’d rather not reinvent those same abstractions, and advance our ability to link multiple views.
The next section uses a case study to introduce some of the similarities between
plot_ly(), introduces the concept of a data-plot-pipeline, and also demonstrates how to extend
ggplotly() with functions that can modify plotly objects.
Wickham, Hadley. 2009. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. http://ggplot2.org.
Sievert, Carson, Chris Parmer, Toby Hocking, Scott Chamberlain, Karthik Ram, Marianne Corvellec, and Pedro Despouy. 2016. Plotly: Create Interactive Web Graphics via ’Plotly.js’.
Wilkinson, Leland. 2005. The Grammar of Graphics (Statistics and Computing). Secaucus, NJ, USA: Springer-Verlag New York, Inc.