by Matt Sundquist, Plotly Co-founder
It's delightfully smooth to publish R code, plots, and presentations to the web. For example:
- Shiny makes interactive apps from R.
- Pretty R highlights R code for HTML.
- Slidify makes slides from R Markdown.
- Knitr and RPubs let you publish R Markdown docs.
- GitHub and devtools let you quickly release packages and collaborate.
Now, Plotly lets you collaboratively edit and publish interactive ggplot2 graphs using these tools. This post shows how. Find us on GitHub, at feedback at plot ly, and @plotlygraphs. For more on our ggplot2 and R support, see our API docs.
You can copy and paste the code below — highlighted with Pretty R — into your R console to install Plotly and make an interactive, web-based plot. Or sign-up and generate your own key to add to the script. You control the privacy of your data and plots, own your work, and public sharing is free and unlimited.
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 ggiris <- qplot(Petal.Width, Sepal.Length, data = iris, color = Species) py$ggplotly(ggiris) # send to plotly
Adding
py$ggplotly()
to your ggplot2 plot creates a Plotly graph online, drawn with D3.js, a popular JavaScript visualization library. The plot, data, and code for making the plot in Julia, Python, R, and MATLAB are all online and editable by you and your collaborators. In this case, it's here: https://plot.ly/~r_user_guide/2; if you forked the plot and wanted to tweak and share it, a new version of the plot would be saved into your profile. 
We can share the URL over email or Twitter, add collaborators, export the image and data, or embed the plot in an iframe in this blog post. Click and drag to zoom or hover to see data.
Our iframe points to https://plot.ly/~r_user_guide/1.embed. For more, here is how to embed plots.
Plotting is interoperable, meaning you can make a plot with ggplot2, add data with Python or our Excel plug-in, and edit the plot with someone on your team who uses MATLAB. Your iframe will always show the most up to date version of your plots. For all plots you can edit, share, and download data and plots from within a web GUI, adding fits, styling, and more. Thus, if your ggplot2 plot doesn't precisely translate through to Plotly, you and your team can use the web app to tweak, edit, and style.



Now let's make a plot in a knitr doc (here's a knitr and RPubs tutorial). First, you'll want to open a new R Markdown doc within RStudio.

You can copy and paste this code into RStudio and press "Knit HTML":
## 1. Putting Plotly Graphs in Knitr ```{r} library("knitr") library("devtools") url<-"https://plot.ly/~MattSundquist/1971" plotly_iframe <- paste("<center><iframe scrolling='no' seamless='seamless' style='border:none' src='", url, "/800/1200' width='800' height='1200'></iframe><center>", sep = "") ``` `r I(plotly_iframe)`
You'll want to press the "publish" button on the generated RPub preview to push the RPub online with a live graph. A published RPub from the code above is here. You can see how the embedded plot looks in the screenshot below. The RPub shows a Plotly graph of the ggplot2 NBA heatmap from a Learning R post.

Thus, we have three general options to publish interactive plots with your favorite R tools. First, use iframes to embed in RPubs, blogs, and on websites. Or in slides, as seen in Karthik Ram's Slidify presentation from useR 2014.
Second, you can make plots as part of an `.Rmd` document or in IPython Notebooks using R. For a `.Rmd` doc, you specify the
`plotly=TRUE`
chunk option. Here is an example and source to see the process in action. Third, you can publish a plot in an iframe in a Shiny app, defining how users interact with your plot. Here is an example with the same plot, and here's how it looks:

A final note. For any Plotly graph, you can call the figure:
py <- plotly("ggplot2examples", "3gazttckd7") # key and username for your call figure <- py$get_figure("r_user_guide", 1) # graph id for plot you want to access str(figure)
or the data. That means you don't have to store data, plots, and code in different places. It's together and editable on Plotly.
figure$data[]
If plotly handled coord_flip() for horizontal bar charts it'd be my go-to display medium for publicly shareable data.
Posted by: Hrbrmstr | September 23, 2014 at 10:39
Wow! This is impressive. As much as I love R, I usually have to rely on PDFs to share the results, but Plotly could change that. Thanks for creating this package and posting this tutorial.
Posted by: n_ashutosh | September 23, 2014 at 21:32
I would love to use something like this and be able to send analyses to my boss with just a web link.
Alas, my company would really frown about moving data outside our firewall and hosting it on external servers, no matter how secure, without proper legal agreements in place.
Is there a good way to create interactive via web browser work like that without needing to put data on someone elses server?
Posted by: bob | September 28, 2014 at 22:02
Hey bob, thanks for checking it out. If you're interested, Plotly does private servers you can place behind your own firewall and host. Then your data never leaves your servers.
See https://plot.ly/product/enterprise/. Does that do it?
Feel free to email me at matt at plot dot ly if you'd like to chat more.
Thanks!
Posted by: Matthew Sundquist | September 29, 2014 at 01:43