Interactive 2D & 3D Plots with Plotly and ggplot2

December 16, 2014

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

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
install_github("ropensci/plotly")  # plotly is part of ropensci
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()

The API response is a URL where the data, code, and plot reside:

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.

The plot is rendered using D3.js, a JavaScript visualization library developed by Mike Bostock of The New York Times. You can use the URL to embed plots using knitr and Shiny, or in iframes with a bit of HTML:

 width="640" height="480" frameborder="0" seamless="seamless" scrolling="no" src="">

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––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: 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.

To leave a comment for the author, please follow the link and comment on their blog: Revolutions. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)