Package Overview

Summary

As we all know, data visualization is critical in understanding and communicating about data. While static graphs are effective in conveying the thought processes and conclusions of the data scientist who made them, they could potentially miss the mark in addressing questions that an end user needs answered. However, what if we could make graphs that were interactive, allowing a user to personally dive deeper into the data? This is the goal of the R package plotly.

plotly is a data visualization tool that focuses on creating interactive, accessible, and visually appealing data models. It extends the functionality of the ggplot2 package by creating more engaging visuals with roughly the same level of syntax. It also has many advanced features that base ggplot2 does not have, which will be covered later.

Background

“Plotly” is actually also the name of the company, which makes the JavaScript graphing library plotly.js. The R package plotly simply interfaces with this library, allowing it to be used with R data/syntax. Naturally, this means that the functionality of plotly.js is also accessible in other languages, including its native JavaScript, Python, and even MATLAB.

Version History

Current version: 4.9.2 (last updated 2/12/20)

The earliest available version in the CRAN package archive is version 2.0.2 (released 11/17/15)

Dependencies

R (≥ 3.2.0), ggplot2 (≥ 3.0.0)

Usage

Forms of interaction:

  • Zoom in/out
  • Adjust axes
  • Select only certain data points for closer inspection
  • Hide irrelevant data points from the graph

Supported visual formats:

  • Basic charts (scatter plots, bar charts, pie charts. etc.)
  • Statistical charts (histograms, box plots, etc.)
  • Scientific charts (contour plots, heatmaps, etc.)
  • Maps (choropleth, geospatial graphs)
  • 3D charts (lines, scatter, surface, etc.)
  • Custom controls (buttons, dropdowns, sliders)
  • Animations

Examples of Usage

Scatterplot

This example uses the dataset from the Economist used in the lab.

Making a graph from scratch

Converting from ggplot2

Original ggplot2:

plotly version:

Map

Animations

Similar Packages

ggplot2

The most obvious comparison can be drawn between plotly and ggplot2, considering that you can literally convert from ggplot2 to plotly. In our experience, simply plotting a graph with either package is roughly the same in difficulty However, plotly offers a wider range of features and better aesthetic customization. More subjectively, we think that graphs generated with plotly tend to be more visually appealing than ggplot2.

leaflet

In our plotly map, there were not many customization options for the scaling. As far as we could tell, out of the box there were only options for the US, continents, and the entire world. If there were more customization options, the documentation did not clearly state it, and there weren’t any sources we could find that indicated that there were other options. leaflet, on the other hand, is a package dedicated to creating interactive mapping visuals. Like plotly, it features interactable maps, but also contains numerous additional layers which can be added with similar syntax to ggplot2, such as:

  • Map tiles
  • Markers
  • Polygons
  • Lines
  • Popups

Reflection

Some features can be finicky — for example, when figuring out how to set up the tooltips for converting the Economist graph, changing the hovertext on the data points had to be done in 2 different ways, depending on if the graph was made entirely with plotly or by converting from ggplot2.

However, by far our biggest struggle with plotly was poor documentation. Documentation for plotly is hosted on their own website, which is split into two different sections. The main documentation is composed of a series of brief examples for the various graphs. While this can be helpful for simple plots, the examples don’t convey enough information to be useful for advanced customization and features.

At the same time, there’s a separate reference page that solely lists all the parameters without any examples, so it is hard to understand what exactly the parameters should be set to. Additionally, some fields like color scale on the choropleth have certain presets like “YlGnBu” that you can’t use unless you happen to know exactly what string the preset is. Of course, this information is not particularly easy to come by. It also doesn’t help that the developers of plotly use the exact same name for their package on every platform, so oftentimes searching for guidance using the R package plotly yielded completely unhelpful advice on how to use plotly in Python.

The main benefits of plotly are obviously its beautiful graphs and interactability. For simple visualizations, it is about as easy to use as ggplot2 itself. And even though the more advanced features were somewhat difficult to implement, they are a nice option to provide even more compelling visuals that engage the viewer.