Skip to content

Instantly share code, notes, and snippets.

@msund
Created April 10, 2014 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msund/10359448 to your computer and use it in GitHub Desktop.
Save msund/10359448 to your computer and use it in GitHub Desktop.
RPub draft
Ggplotly: Interactive, online, and shareable plots
========================================================
[Ggplotly](https://github.com/ropensci/plotly) and [Plotly's R API](https://plot.ly/api/r) let you write ggplot2 syntax, add `py$ggplotly()`, and get an interactive, online, shareable Plotly graph. You can embed it in an iframe, save and share your data and graphs together online, and collaboratively graph and analyze your data. Here is an example:
In this RPub, we'll show you how it works. The package is being developed by [Toby Hocking](http://cbio.ensmp.fr/~thocking/), and is on [GitHub](https://github.com/ropensci/plotly), where we eagerly await your thoughts, issues, and pull requests. The project is the result of suggestions from the community and inspiration from Hadley Wickham and ggplot2.
## I. Getting Started
```
install.packages("devtools")
library(devtools)
install_github("plotly", "ropensci")
```
Install Plotly:
```
library(plotly)
```
Sign up on [Plot.ly](https://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](http://docs.ggplot2.org/current/geom_smooth.html)? Here's how to do it in Plotly.
```
library(ggplot2)
grid$mpg <- stats::predict(model, newdata=grid)
qplot(wt, mpg, data=mtcars, colour=factor(cyl)) + geom_line(data=grid)
```
Now we call Plotly:
```
py$ggplotly()
```
### Like GitHub for sharing data and graphs
It has `browseURL` baked in, and automatically calls Plotly, making the graph you see, but calling it here:
The URL is shareable. You can set [the privacy](http://plot.ly/api/r/docs/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](https://plot.ly/~RhettAllain/) from Wired Science.
Two other things make Plotly special. First, you can edit your graph with code or the GUI. So you don't need to code it after you make it to tweak it. Second, you can add data to the same graph now or later; and so can others, using [Python](plot.ly/api/python), [MATLAB](plot.ly/api/MATLAB), or the GUI.
# III. More ggplot2 gallery graphs
You can use ggplotly to draw a [frequency polygon](http://docs.ggplot2.org/current/geom_freqpoly.html).
```
qplot(carat, data = diamonds, geom = "freqpoly")
qplot(price, ..density.., data = diamonds, geom = "freqpoly",
binwidth = 1000, colour = color)
py$ggplotly()
```
Like to [alpha blend](http://docs.ggplot2.org/current/geom_point.html) your big data? So do we. Try this one out:
```
p <- ggplot(mtcars, aes(wt, mpg))
d <- ggplot(diamonds, aes(carat, price))
d + geom_point(alpha = 1/10)
py$ggplotly()
```
Or, make plots [beautiful](http://mandymejia.wordpress.com/2013/11/13/10-reasons-to-switch-to-ggplot-7/).
```
ggplot(data=diamonds, aes(x=carat, y=price, colour=clarity)) +
geom_point(alpha=0.1)
```
Want to [connect your observations](http://docs.ggplot2.org/0.9.3.1/geom_line.html) 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()
#Change scale
p + geom_line(aes(colour = rating))
py$ggplotly()
```
You can also [jitter your points](http://docs.ggplot2.org/current/position_jitter.html).
```
qplot(am, vs, data = mtcars)
ggplot(mtcars, aes(x = am, y = vs)) + geom_point(position = position_jitter(w = 0.1, h = 0.1))
py$ggplotly()
```
# IV. Classic graphs:
You can use `ggplotly` with `ggplot` and `qplot` plots. For example, here is a classic from the iris data set.
```
qplot(Sepal.Length, Petal.Length, data = iris, color = Species)
py$ggplotly()
```
Or, take `ggplotly` for a spin with the organge dataset:
```
qplot(age, circumference, data = Orange, colour = Tree, geom = "line")
```
# V. Stack Overflow problems solved
Want to [draw functions](http://stackoverflow.com/questions/1853703/plotting-functions-in-r) with `curve`?
```
qplot(x, y, data=as.data.frame(curve(eq)), geom="line")
py$ggplotly()
```
You can also draw 2 graphs [in the same plot](http://stackoverflow.com/questions/2564258/plot-2-graphs-in-same-plot-in-r/21274406#21274406).
```
x <- seq(-2, 2, 0.05)
y1 <- pnorm(x)
y2 <- pnorm(x,1,1)
df <- data.frame(x,y1,y2)
ggplot(df, aes(x)) + # basic graphical object
geom_line(aes(y=y1), colour="red") + # first layer
geom_line(aes(y=y2), colour="green")
py$ggplotly()
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment