by Matt Sundquist
co-founder of Plotly
R, Plotly, and ggplot2 let you make, share, and collaborate on beautiful, interactive plots online. Let's see what we can do with the topographic data from Auckland's Maunga Whau Volcano that comes with R.
Copy and paste this R code to make your first plot. The basic idea is: use ggplot2 code, add
py$ggplotly() to call the Plotly API, and make an interactive, web-based plot for sharing and collaboration.
install.packages("devtools") # so we can install from github library("devtools") install_github("ropensci/plotly") # plotly is part of ropensci library(plotly) py <- plotly(username="r_user_guide", key="mw5isa4yqp") # open plotly connection # Generate data library(reshape2) # for melt volcano3d <- melt(volcano) names(volcano3d) <- c("x", "y", "z") # Basic plot v <- ggplot(volcano3d, aes(x, y, z = z)) v + geom_tile(aes(fill = z)) + stat_contour() py$ggplotly()
The API response is a URL where the data, code, and plot reside: https://plot.ly/~r_user_guide/346.
If you ran the code you'll notice the colors are different. We've applied a theme from within the Plotly GUI to change the colors and layout. You can save themes--styles, colors, layouts--from plots and apply them to new plots. Hover to see data points--you can customize the hover text--or click and drag to zoom.
We can add collaborators online and control the privacy. But not all our work is online; we generate reports, write papers, and give presentations with slides. Thus, for offline use, we can can export an image (SVG, EPS, PNG, or PDF), and include a link to the plot in our image. E.g.:
When you share the URL, you're sharing a fully reproducible version of your plot. The URL hosts your data, code to reproduce the plot, and exportable versions.
Others can fork your plot and make their own version, allowing for lightweight collaboration. That means no more emailing or searching around for data, plots, and code. It's all here.
Plotly graphs are represented using JSON, a syntax for storing and exchanging data. The .json version of each plot contains the data and a full description of the plot. The framework allows interoperability between Python, R, MATLAB, and other languages.
We can also make 3D plots rendered with WebGL. The iframe below says "source: from api (254)." That's a link to our original plot. The forked 3D plot is at a new URL--https://plot.ly/~MattSundquist/2444--and shows the fork history.
If you click and hold the plot, you can rotate it, or scroll in and out on the plot to zoom in and out. If you hover, you can see dynamic contour lines. You can fork the plot and see it in full-screen here: https://plot.ly/~MattSundquist/2444. Check out our 3D collection for more.
We're on Twitter and GitHub, and would love to hear your feedback, thoughts, and suggestions. We plan to continue expanding our maps coverage, so your ideas are most welcome. If our free cloud-based product doesn't work for you, contact us about getting Plotly on-premise.