2016-02-18
library(plotly) p <- qplot(data = mtcars, x = wt, y = mpg, geom = c("point", "smooth")) ggplotly(p)
ggplotly()
is pragmatic, a custom interface can give us more control.library(plotly) plot_ly(economics, x = date, y = unemploy / pop)
p <- plot_ly(economics, x = date, y = unemploy / pop) str(p)
#> Classes ‘plotly’ and 'data.frame': 478 obs. of 6 variables: #> $ date : Date, format: "1967-06-30" "1967-07-31" ... #> $ pce : num 508 511 517 513 518 ... #> $ pop : int 198712 198911 199113 199311 199498 199657 199808 199920 200056 200208 ... #> $ psavert : num 9.8 9.8 9 9.8 9.7 9.4 9 9.5 8.9 9.6 ... #> $ uempmed : num 4.5 4.7 4.6 4.9 4.7 4.8 5.1 4.5 4.1 4.6 ... #> $ unemploy: int 2944 2945 2958 3143 3066 3018 2878 3001 2877 2709 ... #> - attr(*, "plotly_hash")= chr "f638d391dcf53809b8426325a842a091#8"
str(plotly_build(p))
#> List of 2 #> $ data :List of 1 #> ..$ :List of 5 #> .. ..$ type : chr "scatter" #> .. ..$ x : Date[1:478], format: "1967-06-30" ... #> .. ..$ y : num [1:478] 0.0148 0.0148 0.0149 0.0158 0.0154 ... #> $ layout:List of 2 #> ..$ xaxis:List of 1 #> .. ..$ title: chr "date" #> ..$ yaxis:List of 1 #> .. ..$ title: chr "unemploy/pop"
%>%
is known as a "pipeline operator"
# f(x, y) becomes x %>% f(y) economics %>% transform(rate = unemploy / pop) %>% plot_ly(x = date, y = rate)
economics %>% transform(rate = unemploy / pop) %>% plot_ly(x = date, y = rate, name = "raw") %>% loess(rate ~ as.numeric(date), data = .) %>% broom::augment(economics) %>% add_trace(x = date, y = .fitted, name = "smooth")
economics %>% transform(rate = unemploy / pop) %>% plot_ly(x = date, y = rate, name = "raw") %>% subset(rate == max(rate)) %>% layout(annotations = list(x = date, y = rate, text = "Peak", showarrow = T), title = "The U.S. Unemployment Rate")
https://github.com/cpsievert/tourbrush. Inspired by http://ggobi.org/demos.
Pipeline | Pros | Cons |
---|---|---|
Server | Flexible | Not (necessarily) fast |
Client | Fast | Not (necessarily) flexible |