class: middle, center, title-slide # Animated and Interactive Visualizations ## STAE04: Data Visualization ### Johan Larsson ### The Department of Statistics, Lund University --- ## Animated Visualizations offers an extra aesthetic that we can map variables to very useful in **presentations** Animating works best when mapping to time or similar process. -- ### gganimate .pull-left-60[ [gganimate](https://gganimate.com/) is a ggplot-based solution for producing animations in R. You may need to download and install a **renderer**; we recommend [gifski](https://gif.ski/). ] .pull-right-40[ <img src="images/gganimate.svg" width="50%" style="display: block; margin: auto;" /> ] --- ### Example: Housing Sales in Texas Start with some data. ```r # aggregate over years tx <- select(txhousing, city, year, sales:inventory) %>% group_by(city, year) %>% summarize_all(mean, na.rm = TRUE) ``` .pull-left[ Then build a basic ggplot. ```r p <- tx %>% ggplot(aes(median, listings)) + geom_point(alpha = 0.5) p ``` ] .pull-right[ <div class="figure" style="text-align: center"> <img src="15-animated-and-interactive-visualizations_files/figure-html/unnamed-chunk-3-1.png" alt="Median sales price and numbers of listings." width="100%" /> <p class="caption">Median sales price and numbers of listings.</p> </div> ] --- ### Animating the Plot Adding `ease_aes()` and `transition_*()` to the plot sets up the animation. Calling the object invokes the `animate()` function, which does all the heavy-lifting. .pull-left[ ```r library(gganimate) anim <- p + * transition_time(year) + * ease_aes() anim # same as animate(anim) ``` ] .pull-right[ <img src="15-animated-and-interactive-visualizations_files/figure-html/unnamed-chunk-4-1.gif" width="100%" style="display: block; margin: auto;" /> ] --- ### Labeling uses special **glue** syntax inside `labs()` ```r anim2 <- anim + * labs(title = "Year: {frame_time}") anim2 ``` <img src="15-animated-and-interactive-visualizations_files/figure-html/unnamed-chunk-5-1.gif" width="60%" style="display: block; margin: auto;" /> --- ### Fine-tuning Use `animate()` directly for more control over the animation. ```r *animate(anim2, duration = 20, nframes = 150) ``` <img src="15-animated-and-interactive-visualizations_files/figure-html/unnamed-chunk-6-1.gif" width="60%" style="display: block; margin: auto;" /> --- ## Interactive Visualizations great for most datasets, particularly complicated datasets, but can only really be used on webpages often useful in tandem with carefully designed static visualizations .pull-left[ ### plotly Simple interactive visualizations can be achieved easily using [plotly](https://plotly.com/). two alternatives: - call `plot_ly()` directly - use `ggplotly()` on a plot built in ggplot2 We focus on the second alternative. ] .pull-right[ <img src="images/plotly.png" width="204" style="display: block; margin: auto;" /> ] --- ```r library(plotly) p <- ggplot(msleep, aes(brainwt, sleep_total, color = vore, genus = genus, conservation = conservation)) + geom_point() + scale_x_log10() *ggplotly(p) ``` <img src="videos/plotly-animation1.gif" width="83%" style="display: block; margin: auto;" /> --- ### Shiny For more advanced interactive visualizations (beyond the scope of this course), have a look at [shiny](https://shiny.rstudio.com/). <img src="videos/shiny-animation1.gif" width="80%" style="display: block; margin: auto;" /> <!-- --- --> <!-- ## References --> <!-- ```{r, results = "asis", echo=FALSE} --> <!-- PrintBibliography(bib) --> <!-- ``` -->