Create Interactive Web Graphics via 'plotly.js'
Easily translate 'ggplot2' graphs to an interactive web-based version and/or create custom web-based visualizations directly from R. Once uploaded to a 'plotly' account, 'plotly' graphs (and the data behind them) can be viewed and modified in a web browser.
Install from CRAN:
Or install the latest development version (on GitHub) via devtools:
NOTE: The CRAN version of plotly is designed to work with the CRAN version of ggplot2, but at least for the time being, we recommend using the development versions of both plotly and ggplot2 (
Web-based ggplot2 graphics
If you use ggplot2,
ggplotly() converts your static plots to an interactive web-based version!
library(plotly) g <- ggplot(faithful, aes(x = eruptions, y = waiting)) + stat_density_2d(aes(fill = ..level..), geom = "polygon") + xlim(1, 6) + ylim(40, 100) ggplotly(g)
ggplotly() tries to replicate the static ggplot2 version exactly (before any interaction occurs), but sometimes you need greater control over the interactive behavior. The
ggplotly() function itself has some convenient "high-level" arguments, such as
dynamicTicks, which tells plotly.js to dynamically recompute axes, when appropriate. The
style() function also comes in handy for modifying the underlying traces attributes used to generate the plot:
gg <- ggplotly(g, dynamicTicks = "y") style(gg, hoveron = "points", hoverinfo = "x+y+text", hoverlabel = list(bgcolor = "white"))
ggplotly() returns a plotly object, you can apply essentially any function from the R package on that object. Some useful ones include
layout() (for customizing the layout),
add_traces() (and its higher-level
add_*() siblings, for example
add_polygons(), for adding new traces/data),
subplot() (for combining multiple plotly objects), and
plotly_json() (for inspecting the underlying JSON sent to plotly.js).
ggplotly() function will also respect some "unofficial" ggplot2 aesthetics, namely
text (for customizing the tooltip),
frame (for creating animations), and
ids (for ensuring sensible smooth transitions).
Using plotly without ggplot2
plot_ly() function provides a more direct interface to plotly.js so you can leverage more specialized chart types (e.g., parallel coordinates or maps) or even some visualization that the ggplot2 API won't ever support (e.g., surface, mesh, trisurf, or sankey diagrams). The cheatsheet is a nice quick reference for this interface, but the plotly cookbook has more complete overview of the philosophy behind this "non-ggplot2" approach.
plot_ly(z = ~volcano, type = "surface")
The R package has special support for linking/highlighting/filtering views that is not (yet) available outside of the R package. This functionality is built upon the crosstalk package, which distinguishes between two event classes: select and filter. The plotly package interprets these classes in the following way:
- Select: add new "selection" trace(s) (i.e., graphical marks) and dim the other traces. Some people refer to this as "brushing" or "highlighting".
- Filter: retain "selection" trace(s), but remove other traces, and update the layout accordingly. Some people refer to this as "crossfilter" or "drill-down".
The following gif helps to demonstrate the difference -- see here for the code used to generate it.
Like other crosstalk enabled widgets, plotly responds to filter events, but you can't (yet) emit a filter event via direct manipulation of a plotly graph. Unlike (some) other crosstalk enabled widgets, plotly has advanced support for select (a much more broad class than filter) events, like persistent/dynamic brushing, brushing via indirect manipulation, and even ways to completely control the appearance new selection traces. In other words, these "special" brushing features work when linking multiple plotly graphs, but may not when linking to other crosstalk enabled widgets.
To date, this slide deck is the most comprehensive, yet somewhat thorough, walk-through of this framework, but the linking views without shiny chapter of the plotly book provides even more background. There are also numerous demos shipped with the package which provide nice examples (list all the demos via
demo(package = "plotly")).
plotly's crosstalk functionality aims to provide tools for interactively exploring subsets of your data with a fixed definition from data to plot. If you need more flexibility, you can always embed crosstalk plots within a larger shiny app, or even access and respond to any plotly event within shiny, but adding shiny into the equation comes with a cost -- the result is no longer standalone HTML (harder to share/host) and linking views requires fairly sophicated knowledge/use of shiny's reactive programming framework.
We have lots of examples on https://plot.ly/r/ and https://plot.ly/ggplot2/, but a more comprehensive review is also available at https://cpsievert.github.io/plotly_book/. I also have a number of slide decks that also have useful demos.
Please read through our contributing guidelines. Included are directions for opening issues, asking questions, contributing changes to plotly, and our code of conduct.
Functions in plotly
|as.widget||Convert a plotly object to an htmlwidget object|
|as_widget||Convert a list to a plotly htmlwidget object|
|add_fun||Apply function to plot, without modifying data|
|add_trace||Add trace(s) to a plotly visualization|
|attrs_selected||Specify attributes of selection traces|
|bbox||Estimate bounding box of a rotated string|
|animation_opts||Animation configuration options|
|api_create||Tools for working with plotly's REST API (v2)|
|get_x||Obtain x coordinates of sf geometry/geometries|
|get_y||Obtain y coordinates of sf geometry/geometries|
|get_figure||Request a figure object|
|get_l||Obtain number of points comprising a geometry|
|highlight||Query graphical elements in multiple linked views|
|add_annotations||Add an annotation(s) to a plot|
|add_data||Add data to a plotly visualization|
|embed_notebook||Embed a plot as an iframe into a Jupyter Notebook|
|event_data||Access plotly user input event data in shiny|
|colorbar||Modify the colorbar|
|config||Set the default configuration for plotly|
|group2NA||Separate groups with missing values|
|hide_colorbar||Hide color bar(s)|
|plot_geo||Initiate a plotly-geo object|
|plot_ly||Initiate a plotly visualization|
|plotly||Main interface to plotly|
|hide_guides||Hide guides (legends and colorbars)|
|layout||Modify the layout of a plotly visualization|
|knit_print.api_grid||Embed a plotly grid as an iframe in a knitr doc|
|knit_print.api_grid_local||Embed a plotly grid as an iframe in a knitr doc|
|knit_print.api_plot||Embed a plotly figure as an iframe in a knitr doc|
|last_plot||Retrieve the last plot to be modified or created.|
|plotly_IMAGE||Create a static image|
|plotly_POST||Create/Modify plotly graphs|
|plot_dendro||Plot an interactive dendrogram|
|plotly_json||Inspect JSON sent to plotly.js|
|print.api||Print method for a 'generic' API response|
|raster2uri||Convert a raster object to a data URI|
|export||Export a plotly graph to a static file|
|geom2trace||Convert a "basic" geoms to a plotly.js trace.|
|gg2list||Convert a ggplot to a list.|
|ggplotly||Convert ggplot2 to plotly|
|plotlyProxy||Modify a plotly object inside a shiny app|
|print.api_plot||Print a plot on plotly's platform|
|rangeslider||Add a range slider to the x-axis|
|plot_mapbox||Initiate a plotly-mapbox object|
|plotly-shiny||Shiny bindings for plotly|
|plotly_build||'Build' (i.e., evaluate) a plotly object|
|plotly_data||Obtain data associated with a plotly graph|
|showRGB||View colors already formatted by toRGB()|
|signup||Create a new plotly account.|
|mutate||Objects exported from other packages|
|subplot||View multiple plots in a single view|
|remove_typedarray_polyfill||Remove TypedArray polyfill|
|schema||Acquire (and optionally display) plotly's plot schema|
|to_basic||Convert a geom to a "basic" geom.|
|plotly_empty||Create a complete empty plotly graph.|
|plotly_example||Run a plotly example(s)|
|print.api_grid||Print a plotly grid object|
|print.api_grid_local||Print a plotly grid object|
|toRGB||Convert R colours to RGBA hexadecimal colour values|
|toWebGL||Convert trace types to WebGL|
Last month downloads
|License||MIT + file LICENSE|
|URL||https://plot.ly/r, https://cpsievert.github.io/plotly_book/, https://github.com/ropensci/plotly|
|Packaged||2017-07-28 15:49:30 UTC; cpsievert|
|Date/Publication||2017-07-29 05:16:25 UTC|
|imports||base64enc , crosstalk , data.table , digest , dplyr , hexbin , htmltools , htmlwidgets (>= 0.9) , httr , jsonlite , lazyeval (>= 0.2.0) , magrittr , purrr , RColorBrewer , scales , tibble , tidyr , tools , viridisLite|
|suggests||broom , Cairo , curl , dendextend , devtools , GGally , ggthemes , IRdisplay , knitr , listviewer , maps , MASS , png , rmarkdown , RSclient , RSelenium , Rserve , sf , shiny (>= 0.14) , testthat , webshot|
|depends||ggplot2 (>= 2.2.1) , R (>= 3.2.0)|
|Contributors||Karthik Ram, Scott Chamberlain, Toby Dylan Hocking, Chris Parmer, Marianne Corvellec, Pedro Despouy, Plotly Technologies Inc.|
Include our badge in your README