by Matt Sundquist, Plotly co-founder
In a recent post we showed how to use ggplot2 and Plotly’s R API to make shareable, interactive graphs. This week, in response to questions and requests, we’ll highlight how to use Plotly for your workflow. We assume that you are working with Plotly from within R. Let’s get started.
Setting up Plotly and ggplot2
First you need to sign in to Plotly and set up. You can copy and paste this code, and either use our API key or sign-up for your own account. Public sharing is free and you own your own data (ilke GitHub), so feel free to plug in as many scripts as you’d like.
# install.packages("devtools") #so we can use install_github
# install_github("plotly", "ropensci")
#Plotly is part of the rOpenSci project.
Then you can sign-up on the Plot.ly site sign-up or running the following function from R.
To start running plots, you’ll want to create a Plotly object. You can use our ggplot2 sandbox, or use your account.
py <- plotly("ggplot2examples", "3gazttckd7")
Grouped Bar Chart With Error Bars
Next let’s look at a simple project to show how Plotly can be a working tool. Say you find a grouped bar chart you like on Stack Overflow. You load the data.
raw <- read.csv("http://pastebin.com/raw.php?i=L8cEKcxS",sep=",")
raw[,2]<-factor(raw[,2],levels=c("Very Bad","Bad","Good","Very Good"),ordered=FALSE)
raw[,3]<-factor(raw[,3],levels=c("Very Bad","Bad","Good","Very Good"),ordered=FALSE)
raw[,4]<-factor(raw[,4],levels=c("Very Bad","Bad","Good","Very Good"),ordered=FALSE)
freq=table(col(raw), as.matrix(raw)) # get the counts of each factor level
You then melt it, creating a plot:
Names=c("Food","Music","People") # create list of names
data=data.frame(cbind(freq),Names) # combine them into a data frame
data=data[,c(5,3,1,2,4)] # sort columns
# melt the data frame for plotting
data.m <- melt(data, id.vars='Names')
# plot everything
n <- ggplot(data.m, aes(Names, value)) +
geom_bar(aes(fill = variable), position = "dodge", stat="identity")
Appending py$ggplotly(n) is what calls the Plotly API. If you’re running it in an .Rmd document, the plot is embedded if you specify the plotly=TRUE chunk option (here is an example source). You can zoom by clicking and dragging, toggling, and hovering for text. You can click through to Plotly and and add a new axis or subplot and error bars.
If you’re reading this over email or it looks odd online, you may not be able to see the final version through an iframe. But that’s life! You can export plot for reports, emails, or presentations as a PNG, PDF, or SVG file, and link back to the original version. In that case you could link to the live version here.
Here it is in an iframe:
<div class = "iframe_container">
<iframe src="https://plot.ly/~MattSundquist/1455/1232/650/550" width="650" height=550" frameBorder="0" seamless="seamless" scrolling="no"></iframe>
You can also interact with your data and plot with R. I am new to R, and recently discovered str() while browsing an incredibly helpful R Reference Card by Tom Short. I enjoy using it while setting up, making, and re-making plots (shown here from within an IPython Notebook running the R kernel).
Adding Data, Revisions, Reproducibility, & Collaboration
If you or your collaborators (you can share it like a Google Doc) want to edit or analyze the data, you can edit it in the grid, or add data with the R API, or by uploading data with Excel, Python, and MATLAB.
A note re: reproducibility. Plotly is a part of the rOpenSci project, an open science and open data project. We’re excited about that. On a personal note, I have recently been pained to find a few graphs I wanted to re-make or data in plots I wanted to access, only to find the code is calling a local file, CSV, or expired URL. Our hope is that plots and data will be shareable, open, and reproducible if we keep the JSON, plot, and data together.
The special component of this is collaboration. You can share the grid for that data, or share the plot. My collaborators can then add their own data, edit the data, and send in new data, from any language or from the web app by simply uploading.
We often have had the problem of not knowing which data we used to make a graph, or which version of a graph was my final one. With Plotly, you can revert to a previous version and discuss graphs in context with your team as the next two graphs illustrate.