Interactive Data Visualization with R and Plotly

Authors: 
Megan A. Jones, Leah A. Wasser
Table of Contents

Plotly - Interactive (and Online) Plots

Plotly bills itself as "a collaborative platform for modern data science". You can use it to build interactive plots that can easily be shared with others (like the Quantifying The Drivers and Impacts of Natural Disturbance Events – The 2013 Colorado Floods lessons).

You will need an free online Plotly account to post & share you plots online. But you can create the plots and use them on your local computer without an account. If you do not wish to share plots online you can skip to Step 3: Create Plotly plot.

Additional information on the plotly R package can be found on the Plotly R Getting Started page.

Note: Plotly doesn't just work with R -- other programs include Python, MATLAB, Excel, and JavaScript.

Step 1: Create account

If you do not already have an account, you need to set up an account by visiting the Plotly website and following the directions there.

Step 2: Connect account to R

To share plots from R (or RStudio) to Plotly, you have to connect to your account. This is done through an API (Application Program Interface). You can find your username & API key in your profile settings on the Plotly website under the "API key" menu option.

To link your account to your R, use the following commands, substituting in your own username & key as appropriate.

# set plotly user name
Sys.setenv("plotly_username"="YOUR_plotly_username")
# set plotly API key
Sys.setenv("plotly_api_key"="YOUR_api_key")

Step 3: Create Plotly plot

There are lots of ways to plot with the plotly package. We briefly describe two basic functions plotly() and ggplotly(). For more information on plotting in R with Plotly, check out the Plotly R library page.

Here we use the example dataframe economics that comes with the package.

# load packages
library(plotly)  # to create interactive plots
library(ggplot2) # to create plots and feed to ggplotly()

# view str of example data set
str(economics)

## Classes 'tbl_df', 'tbl' and 'data.frame':    574 obs. of  6 variables:
##  $ date    : Date, format: "1967-07-01" "1967-08-01" ...
##  $ pce     : num  507 510 516 513 518 ...
##  $ pop     : int  198712 198911 199113 199311 199498 199657 199808 199920 200056 200208 ...
##  $ psavert : num  12.5 12.5 11.7 12.5 12.5 12.1 11.7 12.2 11.6 12.2 ...
##  $ uempmed : num  4.5 4.7 4.6 4.9 4.7 4.8 5.1 4.5 4.1 4.6 ...
##  $ unemploy: int  2944 2945 2958 3143 3066 3018 2878 3001 2877 2709 ...

# plot with the plot_ly function
unempPerCapita <- plot_ly(x =economics$date, y = economics$unemploy/economics$pop)
unempPerCapita 

## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plot.ly/r/reference/#scatter

## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode

Note: This plot is interactive within the R environment but is not as posted on this website.

If you already use ggplot to create your plots, you can directly turn your ggplot objects into interactive plots with ggplotly().

## plot with ggplot, then ggplotly

unemployment <- ggplot(economics, aes(date,unemploy)) + geom_line()
unemployment

ggplotly(unemployment)

Note: This plot is interactive within the R environment but is not as posted on this website.

Step 4: Publish to Plotly

The function plotly_POST() allows you to post any plotly plot to your account.

# publish plotly plot to your plotly online account
api_create(unemployment)

Examples

The plots below were generated using R code that harnesses the power of the ggplot2 and the plotly packages. The plotly code utilizes the RopenSci plotly packages - check them out!

Data Tip Are you a Python user? Use matplotlib to create and publish visualizations.

Get Lesson Code: 

DataVis-plotly-R.R

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Dialog content.