A Plotly

In this module, you’ll start building visualizations using the Plotly API. Plotly is a visualization software that recently open-sourced it’s API to JavaScript, MatLab, Python, and R, making it quite valuable to learn. Plotly graphs are fairly customizable, and (by default) have a variety of interactive methods with each chart (i.e., hover, brush to zoom, pan, etc.). Many of these events are fairly cumbersome to build programmatically, which makes a library like Plotly quite attractive.

A.1 Getting Started

The Plotly API is an R package that you’ll use to build interactive graphics. Like other open-source that we’ve used in this course, we’ll load this API as a R package as follows:

# Install package
install.packages("plotly")

# Load library
library(plotly)

Then, the plot_ly object will be accessible to you to build graphics on your page.

Note: sometimes RStudio fails to show your plotly graph in a website preview when you use plotly in an RMarkdown documnent. However, if you click on the Open in Browser button, you should be able to interact with your chart as it will show in a web browser. This isn’t your fault, and doesn’t need to be de-bugged.

A.2 Basic Charts

One of the best ways to start building charts with Plotly is to take a look at a basic example of your choice, and explore the syntax. In general, to build a Plotly object (graph) you’ll pass a dataframe into the plot_ly function, then adjust the parameters to specify what you want to visualize. For example, here is the basic example of a scatterplot from the documentation:

# Make a scatterplot of the iris data
plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, mode = "markers", type = "scatter")

The approach seems pretty straightforward – in fact, if you exclude type = "scatter" and mode = "markers", Plotly will make an educated guess about what type of plot you want (and in this case, it will in fact create a scatterplot!). The only syntax that looks a bit strange is the tilde character (~). In R, the tilde designates a variable as a formula, which was a design choice of the developers of the API.

A.3 Layout

While the plot_ly function controls the data that is being visualized, additional chart options such as titles and axes are controlled by the layout function. The layout function accepts as a parameter a plotly object, and manipulates that object. Again, I think a great place to start is an example in the documentation:

# From documentation

# Create a plotly object `p`
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, type = "scatter", mode = "markers",
        marker = list(size = 10,
                       color = 'rgba(255, 182, 193, .9)',
                       line = list(color = 'rgba(152, 0, 0, .8)',
                                   width = 2))) %>%
  # Use pipe function to pass the plotly object into the `layout` function                           
  layout(title = 'Styled Scatter',
         yaxis = list(zeroline = FALSE),
         xaxis = list(zeroline = FALSE))

# Show chart
p

This example uses the pipe operator (%>%) to pass the plotly object into the layout function as the first argument. We can then infer the structure of the other parameters, which you can read about more in the API Documentation:

A.4 Hovers

By default, plotly will provide information on each element when you hover over it (which is awesome). To manipulate the information in a hover, you can modify the text attribute in the plot_ly function, and you can use your data to populate the information on hover:

# From documentation

# Create data
d <- diamonds[sample(nrow(diamonds), 1000), ]

# Create plot, specifying hover text
p <- plot_ly(
  d, x = ~carat, y = ~price, mode = "markers", type = "scatter",
  # Hover text:
  text = ~paste0("Price:$", price, '<br>Cut: ', cut),
  color = ~carat, size = ~carat
)

# Show chart
p

Note, plotly allows you to specify HTML syntax in the text formula. In this case, it uses a line break <br> to improve the layout.