Skip to content

Commit

Permalink
more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Jun 16, 2016
1 parent ed1e4fc commit 5e3f011
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
1 change: 0 additions & 1 deletion inst/htmlwidgets/plotly.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ HTMLWidgets.widget({

// if no plot exists yet, create one with a particular configuration
if (!instance.plotly) {
console.log(x);
var plot = Plotly.plot(graphDiv, x.data, x.layout, x.config);
instance.plotly = true;
instance.autosize = x.layout.autosize;
Expand Down
23 changes: 12 additions & 11 deletions vignettes/intro.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,45 @@ To create a plotly visualization, start with `plot_ly()`.

```{r}
library(plotly)
plot_ly(economics, x = date, y = unemploy / pop)
plot_ly(economics, x = ~date, y = ~unemploy / pop)
```

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)
plot_ly(economics, x = date, y = unemploy / pop,
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, x = date, y = fitted(m), name = "loess")
p <- plot_ly(economics, x = ~date, y = ~unemploy / pop, name = "raw")
add_lines(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 create a visualization pipeline:

```{r}
economics %>%
plot_ly(x = date, y = unemploy / pop) %>%
add_trace(x = date, y = fitted(m)) %>%
plot_ly(x = ~date, y = ~unemploy / pop) %>%
add_lines(y = ~fitted(m)) %>%
layout(showlegend = F)
```

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.
TODO: talk about dplyr verbs!

```{r}
library(dplyr)
economics %>%
transform(rate = unemploy / pop) %>%
plot_ly(x = date, y = rate) %>%
subset(rate == max(rate)) %>%
mutate(rate = unemploy / pop) %>%
plot_ly(x = ~date, y = ~rate) %>%
filter(rate == max(rate)) %>%
layout(
showlegend = F,
annotations = list(x = date, y = rate, text = "Peak", showarrow = T)
annotations = list(x = ~date, y = ~rate, text = "Peak", showarrow = T)
)
```

Expand Down

0 comments on commit 5e3f011

Please sign in to comment.