Ggplotly: Make ggplot2 graphs into shareable D3 graphs

Ggplotly and Plotly's R API let you make ggplots, add py$ggplotly(), and get a Plotly graph. That means for no extra effort, your ggplots can be interactive, online, and drawn with D3. Plotly is also free, collaborative (editable together), stores graphs and data together online, and you own your data. In this RPub, we'll show you how it works.

I. Getting Started

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

Install:

library(plotly)
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. Ggplot gallery graphs, in Plotly

Want to make a scatter and add a smoothed conditional mean? Here's how to do it in Plotly.

qplot(wt, mpg, data=mtcars, colour=factor(cyl))

plot of chunk unnamed-chunk-6

model <- lm(mpg ~ wt + factor(cyl), data=mtcars)
grid <- with(mtcars, expand.grid(
    wt = seq(min(wt), max(wt), length = 20),
    cyl = levels(factor(cyl))
))

grid$mpg <- stats::predict(model, newdata=grid)

firstplot <- qplot(wt, mpg, data=mtcars, colour=factor(cyl)) + geom_line(data=grid)
print(firstplot)

plot of chunk unnamed-chunk-6

Now we call Plotly:

py$ggplotly(firstplot)

Like GitHub for sharing data and graphs

The URL is shareable. You can set the privacy of your graph by changing world_readable to false, or leaving it at true (default). You can also access your data on Plotly, analyze it in the grid, and collaborate. So like we might work together on code on GitHub or a Google Doc, we can edit graphs and data together on Plotly. And like GitHub, you get a public profile of your graphs, like Rhett Allain from Wired Science.

You can edit your graph and add data with code or the GUI. The same is true of your collaborators, who can edit, add, or stream data from the GUI or our APIs for Python, MATLAB, Julia, Perl, and REST.

You can use ggplotly to draw a frequency polygon.

qplot(price, ..density.., data = diamonds, geom = "freqpoly", binwidth = 1000, 
    colour = color)

plot of chunk unnamed-chunk-8

py$ggplotly()
## Error: conversion not implemented for geom_freqpoly (basic geom_freqpoly)

Like to alpha blend your big data? So do we. Try this one out:

d <- ggplot(diamonds, aes(carat, price))
d + geom_point(alpha = 1/10)

plot of chunk unnamed-chunk-9

py$ggplotly()

Or, make plots beautiful.

ggplot(data = diamonds, aes(x = carat, y = price, colour = clarity)) + geom_point(alpha = 0.1)

plot of chunk unnamed-chunk-10

Want to connect your observations and order by x value?

mry <- do.call(rbind, by(movies, round(movies$rating), function(df) {
    nums <- tapply(df$length, df$year, length)
    data.frame(rating = round(df$rating[1]), year = as.numeric(names(nums)), 
        number = as.vector(nums))
}))

p <- ggplot(mry, aes(x = year, y = number, group = rating))
p + geom_line(aes(colour = rating))

plot of chunk unnamed-chunk-11

You can also jitter your points.

ggplot(mtcars, aes(x = am, y = vs)) + geom_point(position = position_jitter(w = 0.1, 
    h = 0.1))

plot of chunk unnamed-chunk-12

py$ggplotly()

III. ggthemes and Plotly

Using ggthemes opens up another set of custom graph filters for styling your graphs. These are still in the early phases of development and don't perfectly translate, but you can always re-style them in the GUI if you'd like.

To get started, you'll want to install:

library("devtools")
install_github("ggthemes", "jrnold")

and load your data:

library("ggplot2")
library("ggthemes")
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]

Here is the Excel 2003 theme:

(qplot(carat, price, data = dsamp, colour = cut) + theme_excel() + scale_colour_excel())

plot of chunk unnamed-chunk-15

Inverse gray:

(qplot(carat, price, data = dsamp, colour = cut) + theme_igray())

plot of chunk unnamed-chunk-16

The Tableau scale:

(qplot(carat, price, data = dsamp, colour = cut) + theme_igray() + scale_colour_tableau())

plot of chunk unnamed-chunk-17

Stephen Few's scale:

(qplot(carat, price, data = dsamp, colour = cut) + theme_few() + scale_colour_few())

plot of chunk unnamed-chunk-18

IV. Classic R datasets graphs:

You can use ggplotly with ggplot and qplot plots. For example, here is a graph of the iris data set.

qplot(Sepal.Length, Petal.Length, data = iris, color = Species)

plot of chunk unnamed-chunk-19

py$ggplotly()

Or, take ggplotly for a spin with the organge dataset:

qplot(age, circumference, data = Orange, colour = Tree, geom = "line")

plot of chunk unnamed-chunk-20

And ggplotly also works with themes:

ggplot(diamonds, aes(carat, price)) + geom_point() + theme_grey()

plot of chunk unnamed-chunk-21

ggplot(diamonds, aes(carat, price)) + geom_point() + theme_bw()

plot of chunk unnamed-chunk-22

and with qplot

qplot(carat, price, data = diamonds)

plot of chunk unnamed-chunk-23

V. Stack Overflow problems solved

Want to draw functions with curve?

qplot(x, y, data = as.data.frame(curve(eq)), geom = "line")
## Error: could not find function "eq"
py$ggplotly()

Or, you can write just by specifying the xlim:

qplot(c(0, 4), stat = "function", fun = exp, geom = "line")

plot of chunk unnamed-chunk-25

py$ggplotly()

You can also draw 2 graphs in the same plot.

x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x, 1, 1)
df <- data.frame(x, y1, y2)

ggplot(df, aes(x)) + geom_line(aes(y = y1), colour = "royalblue") + geom_line(aes(y = y2), 
    colour = "tomato")

plot of chunk unnamed-chunk-26

py$ggplotly()

V. Conclusion

Plotly's API is part of rOpenSci, being developed by Toby Hocking, and on GitHub. The project was inspired by Hadley Wickham's work and ggplot2. We welcome your thoughts, issues, and pull requests.