Publishishing R and ggplot2 to the web

Original Post at R-bloggers

Some of many ways to publish R code, plots, and presentations to the web are:

  • 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.

Plotly lets us publish interactive ggplot2 graphs using these tools. Plotly on GitHub, and @plotlygraphs. For more see API docs.

You can copy and paste the code below in 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
install_github("ropensci/plotly")  # plotly is part of ropensci
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 ggplot2 plot creates a Plotly graph online, drawn with D3.js. 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:; if you forked the plot, 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.

The iframe points to For more, here is how to embed plots.

Now let’s make a plot in a knitr doc (knitr and RPubs tutorial). First, 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
plotly_iframe <- paste("<center>http://,%20url,%20%20%20%20%20/800/1200<center>", sep = "")
`r I(plotly_iframe)`

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.

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.

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

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.