class: center, middle, inverse, title-slide # Interactive Graphics in R ##
with Plotly
### Morten Andersen & Andreas Hald Espensen ### 2019-04-23 (updated: 2019-05-09) --- # Basics Plotly in R website: https://plot.ly/r/ -- "Plotly is an R package for creating interactive web-based graphs via the open source JavaScript graphing library [plotly.js](https://plot.ly/javascript/)." -- Rendered locally through the [htmlwidgets](http://www.htmlwidgets.org/) framework. -- **Installation**: ```r if (!require("plotly")) install.packages("plotly") library(plotly) ``` -- **Getting Started**: https://plot.ly/r/getting-started/ -- *How did we come across the Plotly library?* ??? By no means exhaustive. Only an introduction to plotly. Working on shiny apps as a Student Assistant at Grundfos' Data Analytics department. --- # Creating a Plot `plot_ly`: Maps R objects to plotly.js. **Usage**: ```r plot_ly(data = data.frame(), ..., type = NULL, name, color, colors = NULL, alpha = NULL, stroke, strokes = NULL, alpha_stroke = 1, size, sizes = c(10, 100), span, spans = c(1, 20), symbol, symbols = NULL, linetype, linetypes = NULL, split, frame, width = NULL, height = NULL, source = "A") ``` `data`: Data frame (optional) `...`: Arguments passed along to the trace type. E.g. `x = 1:10`, `x = ~Time`, `y`, etc. ??? --- # Creating a Plot - Let's see it in action! ```r p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box") p ```
??? `Midwest` data set: Demographic information of midwest counties. `percollege`: Percent college educated. --- # Connection with ggplot2 Contains the function `ggplotly` which will convert `ggplot2` figures into a Plotly object. -- You have the option to manipulate the object with the `style` function. -- More information at: https://plot.ly/ggplot2/extending-ggplotly/ ??? --- # Connection with ggplot2 ```r p <- ggplot(fortify(forecast::gold), aes(x, y)) + geom_line() ``` ``` ## Registered S3 method overwritten by 'xts': ## method from ## as.zoo.xts zoo ``` ``` ## Registered S3 method overwritten by 'quantmod': ## method from ## as.zoo.data.frame zoo ``` ``` ## Registered S3 methods overwritten by 'forecast': ## method from ## fitted.fracdiff fracdiff ## residuals.fracdiff fracdiff ``` ```r p ``` <!-- --> ??? `gold`: Gold price time series - Daily morning gold prices in US dollars. 1 January 1985 - 31 March 1989. --- # Connection with ggplot2 ```r p <- ggplot(fortify(forecast::gold), aes(x, y)) + geom_line() gg <- ggplotly(p) gg ```
??? --- # Connection with ggplot2 ```r p <- ggplot(fortify(forecast::gold), aes(x, y)) + geom_line() gg <- ggplotly(p) gg <- style(gg, line = list(color = 'gold'), hoverinfo = "y", traces = 1) gg ```
??? --- # Cheatsheet + Cookbook Handy cheatsheet available at: https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf. -- Cookbook available at: https://plotly-r.com/index.html ??? --- # Creating a Plot ```r trace_0 <- rnorm(100, mean = 5) trace_1 <- rnorm(100, mean = 0) trace_2 <- rnorm(100, mean = -5) x <- c(1:100) data <- data.frame(x, trace_0, trace_1, trace_2) head(data) ``` ``` ## x trace_0 trace_1 trace_2 ## 1 1 7.504929 -0.179177707 -4.316720 ## 2 2 5.291646 -0.708512684 -4.686275 ## 3 3 4.422269 0.003913931 -4.455104 ## 4 4 4.276195 -1.452620827 -5.188447 ## 5 5 4.500436 -2.984828576 -5.417425 ## 6 6 5.854183 -2.122019810 -4.489960 ``` ??? --- # Creating a Plot ```r p <- plot_ly(data, x = ~x, y = ~trace_0, name = 'trace 0', type = 'scatter', mode = 'lines') %>% add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers') %>% add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers') #p <- p %>% layout(hovermode = 'compare') p ```
??? --- # Creating a Plot ```r p_hist <- data %>% plot_ly(alpha = 0.6) %>% add_histogram(x = ~trace_0) %>% add_histogram(x = ~trace_1) %>% add_histogram(x = ~trace_2) %>% layout(barmode = "overlay", xaxis = list(title = "My x-axis title")) p_hist ```
??? --- # Iris ```r pal <- c("red", "blue", "green") p <- iris %>% plot_ly(x = ~Sepal.Length, y = ~Petal.Length, color = ~Species, colors = pal, type = "scatter", mode = "markers", hoverinfo = 'text', text = ~paste('</br> Species: ', Species, '</br> Petal Lenght: ', Petal.Length, '</br> Petal Width: ', Petal.Width)) p ```
??? --- # dygraphs for R dygraphs in R website: https://rstudio.github.io/dygraphs/index.html "The dygraphs package is an R interface to the dygraphs JavaScript charting library. It provides rich facilities for charting time-series data in R" (https://rstudio.github.io/dygraphs/index.html) ```r if (!require("dygraphs")) install.packages("dygraphs") library(dygraphs) ``` ??? --- # dygraphs for R ```r lungDeaths <- cbind(mdeaths, fdeaths) dygraph(lungDeaths) %>% dyRangeSelector() ```
??? --- # Custom plots ```r lungDeaths <- cbind(mdeaths, fdeaths) dygraph(lungDeaths) %>% dyRangeSelector() %>% dyBarSeries('fdeaths') %>% dyFilledLine('mdeaths') ```
??? --- class: center, middle # Thank you! Slides created via the [**xaringan**](https://github.com/yihui/xaringan) R package.