rOpenSci and Plotly: Make ggplots shareable, interactive and with D3

Ggplotly and rOpenSci's Plotly API let you make ggplots, add py$ggplotly(), and get an interactive, shareable, online plot. It's drawn with D3. In this RPub, we'll show you how it works. Thanks to Joe and the Revolution Analytics team for letting us guest post.

I. Getting Started

install.packages("devtools")
library(devtools)
install_github("plotly", "ropensci")

Install:

library(plotly)
## Loading required package: RCurl
## Loading required package: bitops
## Loading required package: RJSONIO
## Loading required package: ggplot2
library(ggplot2)

Sign up on Plot.ly or like this:

signup("new_username", "your_email@domain.com")

Use your account or our “RgraphingAPI” test account and key:

py <- plotly("RgraphingAPI", "ektgzomjbx")

II. Interactive graphs, in Plotly

First up, we'll draw a basic graph from the CO2 dataset.

c <- qplot(conc, uptake, data = CO2, colour = Type) + scale_colour_discrete(name = "")
py$ggplotly(c)

When you run the plot, it will call it in your browser. It will make a graph, visible at this URL:

https://plot.ly/~RgraphingAPI/1000/

We can also go in and see the data. I forked the graph, and shared the grid here:

https://plot.ly/~MattSundquist/1192

Now you can also use Plotly's other APIs for MATLAB, R, Perl, Julia, and REST to write to graphs.

That means you and I could edit the same graph with any language. We can even edit the graph and data from the GUI, so technical and non-technical teams can work together. And all the graphs go to your profile, like this: https://plot.ly/~RgraphingAPI.

You also control the privacy by setting world_readable to false or true, and can control your sharing.

Second, thanks to Aaron Gonzales for his examples.

ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))

values <- data.frame(id = ids, value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5))

positions <- data.frame(id = rep(ids, each = 4), x = c(2, 1, 1.1, 2.2, 1, 0, 
    0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3, 0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 
    0.5, 0.6, 1.3), y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 
    1, 1.5, 2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2))

# Currently we need to manually merge the two together
datapoly <- merge(values, positions, by = c("id"))

p <- ggplot(datapoly, aes(x = x, y = y)) + geom_polygon(aes(fill = value, group = id))
py$ggplotly(p)

Plotly's API is part of rOpenSci, being developed by Toby Hocking, and on GitHub. The call opens a browser tab. Or in an .Rmd document, the plot is embedded if you specify the plotly=TRUE chunk option (see source).

It's free, online, you own your data, and you control the data privacy. We welcome your thoughts, issues, and pull requests.

Next up, suppose someone asks you what country you're from, and tells you have to use ggplot2 to draw a map and highlight your country. You can also use Plotly to create your map. Here, we'll create a choropleth map of Virginia with ggplot2 and highlight Rockingham county.

require(ggplot2)
require(maps)
## Loading required package: maps
va <- map_data("county", region="virginia")
cty <- data.frame(subregion=unique(va$subregion))
cty$stat <- sample.int(nrow(cty))
va <- merge(va, cty, by="subregion")

m <- ggplot(va, aes(long, lat)) + coord_map() + 
    geom_polygon(aes(group=group, fill=stat), color="grey") +
    geom_path(data=subset(va, subregion %in% c("rockingham")), aes(group=group), size=4, color="white")

py$ggplotly(m)