How to publish R and ggplot2 to the web

[This article was first published on Revolutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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[]

To leave a comment for the author, please follow the link and comment on their blog: Revolutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)