Tutorial
Interactive Data Vizualization with R and Plotly
Last Updated: Jan 24, 2024
Authors: Megan A. Jones, Leah A. Wasser
Last Updated: Jan 24, 2024
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.
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.
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_USERNAME")
# set plotly API key
Sys.setenv("plotly_api_key"="YOUR_KEY")
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(ggplot2) # to create plots and feed to ggplotly()
library(plotly) # to create interactive plots
# view str of example dataset
str(economics)
## tibble [574 × 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ date : Date[1:574], format: "1967-07-01" "1967-08-01" ...
## $ pce : num [1:574] 507 510 516 512 517 ...
## $ pop : num [1:574] 198712 198911 199113 199311 199498 ...
## $ psavert : num [1:574] 12.6 12.6 11.9 12.9 12.8 11.8 11.7 12.3 11.7 12.3 ...
## $ uempmed : num [1:574] 4.5 4.7 4.6 4.9 4.7 4.8 5.1 4.5 4.1 4.6 ...
## $ unemploy: num [1:574] 2944 2945 2958 3143 3066 ...
# plot with the plot_ly function
unempPerCapita <- plot_ly(x =economics$date, y = economics$unemploy/economics$pop)
To make your plotly plot in R, run the following line:
unempPerCapita
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
To make your plotly plot in R, run the following line:
ggplotly(unemployment)
Note: This plot is interactive within the R environment but is not as posted on this website.
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)
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.