class: center, middle, inverse, title-slide # Generate interactive graphics ## Using the plotly library ### Thomas Källman ### 10 June, 2018 --- class: spaced ## Contents * Why interactive graphics and `plotly` * Creating plotly objects * `Plot_ly` and `ggplotly` * Combining plots * Animated plots * Additional information --- ## Interactive graphics Why interactive? * Adds a layer of information that help viewers understand data encourages explanatory data analysis * Make it possible to view complex data such as time series of multiple variables that is much harder to do with static views Why plotly in R? * Makes use of all the development and benefits of the javascript graphing library [plotly.js](www.plot.ly) (avoid reinventing the wheel) * Uses the ideas of grammar of graphics and is hence in many ways similar to ggplot2 * Can in a single line of code convert static ggplot2 graphs to interactive plots * Can generate useful 3d graphics * Allow for sharing the plot with associated data in json format so that they can be easily modified using other languages ??? Plotly is also easy to interact with from python and matlab making it suitable for collaborative efforts --- ## Support many types of basic plots .pull-left-50[ Scatterplots
Line plots
] .pull-right-50[ Bar plots
Bubble plots
] ??? These are just examples of the standard plots that are part of most graphing systems. Make sure to look at one in more detail to show the interaction that is part of the plots. --- ## As well as more complex types .pull-left-50[ 3d scatterplots
] .pull-right-50[ 2d histograms
] ??? Pane around the 3d plot to highlight that 3d plots can be fairly useful. --- ## And even animated plots
??? This is just showing a y = sin(x) for a set of pi times x values, no meaning just looks cool :) --- ## The plotly logics As ggplot2, plotly is a take on the idea of Grammar of Graphics. Creating graphics in plotly entails the following: 1. Prepare data 2. Create plotly object with the function `plot_ly()` 3. Specify trace type with the type argument. Trace types currently supported: 'scatter', 'box', 'bar', 'heatmap', 'histogram', 'histogram2d', 'histogram2dcontour', 'pie', 'contour', 'scatterternary', 'sankey', 'scatter3d', 'surface', 'mesh3d', 'scattergeo', 'choropleth', 'scattergl', 'pointcloud', 'heatmapgl', 'parcoords', 'scattermapbox', 'carpet', 'scattercarpet', 'contourcarpet', 'ohlc', 'candlestick', 'area' 4. Add traces to the plot with the functions `add_trace()`, `add_markers()`, `add_boxplot()`... (see plot.ly for complete list) 5. Finally use the `layout()` function to modify or create title, axis, annotation etc ??? unlike ggplot2 matrices is also okay, but it is easier to stick with data frames or tibbles --- ## Build a plot | Data Create a random data set ```r y1 <- rgamma(n = 300, shape = 3) + 5 y2 <- rnorm(300, mean = 0) y3 <- rnorm(300, mean = -5) - 3 data <- data.frame(y1, y2, y3) data$type <- sample(factor(c("A", "B")), replace = T, size = 300) head(data) ``` ``` ## y1 y2 y3 type ## 1 7.620308 0.9767587 -6.823413 B ## 2 6.777759 -0.3870562 -9.587509 B ## 3 10.128819 -1.0908251 -8.062486 A ## 4 8.800668 -0.3210605 -9.633460 B ## 5 7.115550 0.2449032 -6.593296 A ## 6 6.993369 -0.6715598 -7.252789 A ``` --- ## Build a plot | Scatter plot Scatter plot ```r ps <- plot_ly(data, y = ~y1, type = "scatter") %>% add_trace(y = ~y2, type = "scatter", mode = "line") tags$div(ps, align = "center") ```
??? Note that layout does not have to be called to produce nice axqis names etc --- ## Build a plot | Histogram ```r ph <- plot_ly(data) %>% add_histogram(x = ~y2) %>% layout(title = "HIST") tags$div(ph, align = "center") ```
--- ## Build a plot | Boxplot ```r pb <- plot_ly(data) %>% add_boxplot(x = ~ y1) %>% layout(xaxis = list(title = "test")) tags$div(pb, align = "center") ```
??? The binning is done by plotly and is slightly different to the default R histogram binning --- ## Two approaches to plotly objects Plotly takes two approaches to create plotly objects. The end-result from both of these are interactive web-based plots with the same base properties and behavior. 1. `plot_ly()` Transform data (dataframe, matrix) to an interactive plot 2. `ggplotly()` Transform static ggplot object to an interactive plot Even though the function differs they generate the same type of plots and objects --- ## Plot_ly vs ggplotly ```r p2 <- plot_ly(iris, x = ~Sepal.Length, y = ~Sepal.Width) %>% add_markers(color = ~Species, showlegend = TRUE) tags$div(p2, align = "center") ```
??? Explain the basic terminology eg. x, y and coloring by species. The tags$div is not part of the plotly package and is needed here to center the plot when capturing the plot in an html file. --- ## Plot_ly vs ggplotly ```r p3 <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color=Species)) + geom_point() p3.1 <- ggplotly(p3) tags$div(p3.1, align = "center") ```
--- ## Plot_ly vs ggplotly
??? As long as one stick to geoms bundled with ggplot2 the ggplotly function will generate useful output, but with custom geoms one might have to do more work as the ggplotly function only knows about the standard ones. --- ## Multiple plots | `subplot()` Multiple plotly objects can be combined using the subplot function
??? This is the mtcars data set --- ## Multiple plot | `subplot()`
--- ## Multiple plot | `subplot()` Complex layouts are easily created using `subplot()` ```r s <- subplot( plot_ly(data, x = y1, color = I("orange"), width = 300, height = 300), plotly_empty(), plot_ly(data, x = y1, y = y2, color = I("blue")), plot_ly(data, y = y2, color = I("orange")), nrows = 2, heights = c(0.2, 0.8), widths = c(0.8, 0.2), shareX = TRUE, shareY = TRUE, titleX = FALSE, titleY = FALSE) tags$div(layout(s, showlegend = FALSE), align = "center") ```
??? The plotly_empty just skips one "quadrant" and generates nothing. The shareX and shareY makes sure the same axis length is used for all data heights/widths settings is the fraction taken up by each plot in this case 80 and 20% --- ## Animated plots ```r p <- plot_ly(ut, x = ~month, y = ~temp, frame = ~year, width = 500, height = 500) tags$div(p, align = "center") ```
??? The data is temperature recordings from uppsala that will be used for the lab session and shows the montly mean temperature from 1722-2017 The frame creates a plot for every year and adds a slider at the bottom so one can see one year at a time --- ## Learn more Unlike many R packages the documentation of plotly from within R is not that complete and to obtain comprehensive information one has to go to online to get a more complete picture. - https://plot.ly/r/reference - complete reference - http://plotly-book.cpsievert.me - book with great examples - https://stackoverflow.com/questions/tagged/plotly --- name: end-slide class: end-slide # Thank you