Edit chart
link on the bottom right of the chart.
-
-
+As of version 2.0, plotly's R package utilizes the [htmlwidgets](http://www.htmlwidgets.org/) framework. As a result, it's easy to embed plotly graphs in [shiny](http://shiny.rstudio.com/) apps.
-
-
### Example 1 - Plotly's Native Syntax
+The key idea is to pass a plotly object to `renderPlotly()` on the server and place it in the UI with `plotlyOutput()`.
+
#### `ui.R`
-```{r, message=FALSE}
+```{r}
library(shiny)
library(plotly)
@@ -51,7 +47,7 @@ shinyUI(fluidPage(
```
#### `server.R`
-```{r, message=FALSE}
+```{r}
library(shiny)
library(plotly)
@@ -77,8 +73,10 @@ shinyServer(function(input, output) {
### Example 2 - Render `ggplotly`
+You can also convert ggplot2 objects to plotly objects with `ggplotly()`
+
#### `ui.R`
-```{r, message=FALSE}
+```{r}
library(shiny)
library(plotly)
@@ -94,7 +92,7 @@ shinyUI(fluidPage(
```
#### `server.R`
-```{r, message=FALSE}
+```{r}
library(shiny)
library(plotly)
diff --git a/_posts/r/2015-07-30-user-guide.Rmd b/_posts/r/2015-07-30-user-guide.Rmd
index 43686930d110..f9c1443ee9b7 100644
--- a/_posts/r/2015-07-30-user-guide.Rmd
+++ b/_posts/r/2015-07-30-user-guide.Rmd
@@ -9,65 +9,96 @@ page_type: example_index
has_thumbnail: false
---
-
-```{r, echo = FALSE, message=FALSE}
-knitr::opts_chunk$set(message = FALSE)
-Sys.setenv("plotly_username"="RPlotBot")
-Sys.setenv("plotly_api_key"="q0lz6r5efr")
+```{r, echo = FALSE}
+knitr::opts_chunk$set(message = FALSE, warning = FALSE)
```
-# Plotly UseR Guide
-Version 1.0.0 of the [plotly R package](https://github.com/ropensci/plotly) introduces a new high-level interface for working with plotly's JavaScript graphing library from R. The aim of this vignette is to explain the semantics of this interface, but I also recommend perusing [plotly's R homepage](https://plot.ly/r/) for more examples.
+To create a plotly visualization, start with `plot_ly()`.
-To initiate a plotly object, use `plot_ly()`. Here we turn the `economics` data frame (from the ggplot2 package) into a plotly visualization and store it as the object `p`.
+```{r}
+library(plotly)
+plot_ly(economics, x = date, y = unemploy / pop)
+```
-```{r, message=FALSE}
+A plotly visualization is composed of one (or more) trace(s), and every trace has a `type` (the default type is 'scatter'). The arguments/properties that a trace will respect ([documented here](https://plot.ly/r/reference)) depend on it's type. A scatter trace respects `mode`, which can be any combination of "lines", "markers", "text" joined with a "+":
+
+```{r}
library(plotly)
-p <- plot_ly(economics, x = date, y = uempmed)
+plot_ly(economics, x = date, y = unemploy / pop,
+ type = "scatter", mode = "markers+lines")
+```
+
+You can manually add a trace to an existing plot with `add_trace()`. In that case, you'll want to either `name` your traces, or hide the legend by setting `showlegend = FALSE`
+
+```{r}
+m <- loess(unemploy / pop ~ as.numeric(date), data = economics)
+p <- plot_ly(economics, x = date, y = unemploy / pop, name = "raw")
+add_trace(p, y = fitted(m), name = "loess")
+```
+
+__plotly__ was designed with a [pure, predictable, and pipeable interface](https://dl.dropboxusercontent.com/u/41902/pipe-dsls.pdf) in mind, so you can also use the `%>%` operator to modify your plots:
+
+```{r}
+economics %>%
+ plot_ly(x = date, y = unemploy / pop) %>%
+ add_trace(y = fitted(m)) %>%
+ layout(showlegend = F)
```
-If you have a plotly account, printing plotly objects in the R console will create a new plotly figure, and open it in your web browser. If you're using knitr/R Markdown with HTML output (like [this vignette](https://github.com/ropensci/plotly/tree/master/vignettes/intro.Rmd)), printing not only creates the plot, but also embeds it as an HTML iframe.
+Furthermore, `plot_ly()`, `add_trace()`, and `layout()`, all accept a data frame as their first argument and output a data frame. As a result, we can inter-weave data manipulations and visual mappings in a single pipeline.
+
+```{r}
+economics %>%
+ transform(rate = unemploy / pop) %>%
+ plot_ly(x = date, y = rate) %>%
+ subset(rate == max(rate)) %>%
+ layout(
+ showlegend = F,
+ annotations = list(x = date, y = rate, text = "Peak", showarrow = T)
+ )
+```
-```{r, message=FALSE}
-p
+Although these functions output special "plotly data frames", `plot_ly()` doesn't require a data frame for input. This makes chart types that accept a `z` argument especially easy to use if you have a numeric matrix:
+
+```{r}
+plot_ly(z = volcano, type = "surface")
+plot_ly(z = volcano, type = "contour")
```
-`plot_ly()` has a number of arguments which are unique to the R package and make common visualizations a bit easier. These arguments are very much inspired by the semantics of ggplot2's `qplot()` in the sense that a scales are automatically applied these variables (i.e., they map data to visual properties).
+## Special arguments
+
+The __plotly__ function arguments used thus far are documented in [the figure reference](https://plot.ly/r/reference), but there are a handful of additional arguments not described in the reference that make common visualizations a bit easier. These arguments are very much inspired by the semantics of ggplot2's `qplot()` in the sense that a scales are automatically applied these variables.
+
+### The color argument
#### Qualitative color mappings
If a ordinal variable (aka a non-ordered factor variable) is assigned to color, then a qualitative color palette is used by default.
-```{r, message=FALSE}
-plot_ly(iris, x = Petal.Length, y = Petal.Width,
+```{r}
+plot_ly(iris, x = Petal.Length, y = Petal.Width,
color = Species, mode = "markers")
```
If you want to change the default palette, it's recommended that you provide a NEW: Plotly's R library v.1.0 renders Plotly graphs offline in RStudio.
- -