As readers likely know putting your data online as interactive visualizations can be a lot of work. We like to use D3, Highcharts and the Google visualization API but all of these tools require some serious programming. When you’re building a website with custom data visualization the effort might make sense, but when you have data you want to share quickly and elegantly a new data visualiation tool, Plot.ly, is a nice option. You can create graphics by uploading data and manually setting plot options or you can create and upload directly from Python, R or other environments.
Here I show a quick example using the Plot.ly API for the R package ggplot2
. I am assuming you have already signed up for a free account and have a username and API key.
1) Install the Plot.ly package and dependencies
Updated October 10, 2014
Installation in R is straightforward and described on Plot.ly’s website. You should be able to simply use the code below. The devtools package gives you the tools you need to download a package from GitHub.
install.packages("devtools")
library("devtools")
install_github("ropensci/plotly")
Note that when I originally ran this code I ran into errors and instead used code directly from this site (which worked for me). But when I re-tested the simple code above (after a comment from a user) I had no problems.
2) Create your ggplot2 graphics
You can find the data setup (and plot beautification tips) on our ggplot2 post. Here is the code we used for this plot:
ggplot(nmmaps, aes(date, temp, color=factor(season)))+ geom_point() +
scale_color_manual(values=c("dodgerblue4", "darkolivegreen4",
"darkorchid3", "goldenrod1"))
3) Send to Plot.ly
From within R you simply make the connection to plotly using the plotly
function and then send your data with the ggplotly
function:
py
This creates the graphic in your Plot.ly account where you can send colleagues a link or, as in the case below, you can simply use an iFrame and embed in a website.
Be sure to test the zoom functionality (double click to reset the zoom).
Note that if the plot is not visible it is being throttled by plot.ly which limits free plot hosting to 500 views per day.
4) Not everything went smoothly
What you see above worked smoothly but I had some trouble setting things up in Python, particularly with matplotlib
. I’m guessing the issue was with the set-up on my end (rather than a problem with Plot.ly) but I couldn’t track down the issues. I was able to create a plot from Python without matplotlib
using this code:
import plotly.plotly as py
from plotly.graph_objs import *
py.sign_in('USERNAME', 'APIKEY')
temperature = []
ozone = []
with open("chicago-nmmaps.csv", 'rb') as csvfile:
myreader = csv.reader(csvfile, delimiter=',')
myreader.next()
for row in myreader:
temperature.append(float(row[4]))
ozone.append(float(row[6]))
trace0 = Scatter(
x=temperature, y=ozone, mode='markers'
)
data = Data([trace0])
py.plot(data, filename = 'basic-scatter')
I also found that after manually creating a graphic, the website was giving me errors when I tried to embed the graphic or tried to view the graphic outside of a log-in session. This issues seems to have disappeared for me.
Finally, you’ll note that the X-axis of the first graphic above is odd. In the R version (which you can see here) this is a date. There is likely a simple solution to this, in fact, when I created this graphic manually by uploading the data the date looked fine but some tweaking will likely be required.
The folks at Plot.ly responded to an issue with date handling right away and fixed the R library. I updated my library and the date handling issue was fixed. Nice work Plot.ly!
5) Summary
I find that Plot.ly is a pretty incredible tool for sharing your data in interactive plots. The manual creation of plots can be done very easily and the APIs are powerful tools for programmatically creating and uploading your graphics. And, of course, it doesn’t hurt that Plot.ly’s headquarters are in one of our favorite cities, Montreal, Quebec!
Hello,
Great post! Greetings from Montreal!
We have added support for dates of class “Date” in R, so please reinstall your “plotly” package to take advantage of it.
Running your code… https://plot.ly/~marianne2/1 🙂
Cheers,
Marianne
Very impressive Marianne, thank you! I’ve updated the library and I’ve updated the text to reflect the improved date handling.
I got a 404 error on the plotly install with your code in step 1, but on plotly’s website they had it like this:
install_github(“ropensci/plotly”)
which worked for me.
Thanks for the comment. That code still works for me. But the simple code on Plot.ly’s website that you point to also now works! I updated step 1 to reflect this, thanks.