class: center middle main-title section-title-6 # Animations<br>and interactivity .class-info[ <figure> <img src="img/00/ggplot-logo.png" alt="ggplot" title="ggplot" width="15%"> </figure> ] --- layout: true class: title title-6 --- # Animations .left-code[ .box-inv-6[Use [**gganimate**](https://gganimate.com/) to map variables to a time aesthetic] ```r ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, size = pop, color = country)) + geom_point(alpha = 0.7) + scale_size(range = c(2, 12)) + scale_x_log10(labels = dollar_format()) + guides(size = FALSE, color = FALSE) + facet_wrap(vars(continent)) + # Special gganimate stuff labs(title = 'Year: {frame_time}') + transition_time(year) + ease_aes('linear') ``` ] .right-plot[ ![](img/03/gapminder.gif) ] --- # Interactivity .box-inv-6.medium[Single plots with **plotly**] .box-6.small.sp-after[Easy!] -- .box-inv-6.medium[Dashboards with **flexdashboard**] .box-6.small.sp-after[Slightly more complicated] -- .box-inv-6.medium[Complete interactive apps with **Shiny**] .box-6.small[Super complicated!] --- # Single plots with plotly .box-inv-6[[Plotly](https://plotly.com/graphing-libraries/) is special software for<br>creating interactive plots with JavaScript] -- .box-inv-6.sp-after[No knowledge of JavaScript needed!] -- .box-6[`ggplotly()` in the **plotly** R package translates<br>between R and Javascript for you!] --- # Plotly .left-code[ ```r library(gapminder) library(plotly) gapminder_2007 <- filter(gapminder, year == 2007) my_plot <- ggplot( data = gapminder_2007, mapping = aes(x = gdpPercap, y = lifeExp, color = continent)) + geom_point() + scale_x_log10() + theme_minimal() ``` ```r ggplotly(my_plot) ``` ] .pull-right.small-code[
] --- # Plotly tooltips .left-code[ ```r my_plot <- ggplot( data = gapminder_2007, mapping = aes(x = gdpPercap, y = lifeExp, color = continent)) + * geom_point(aes(text = country)) + scale_x_log10() + theme_minimal() ``` ```r *interactive_plot <- ggplotly( * my_plot, tooltip = "text" *) *interactive_plot ``` ] .pull-right.small-code[
] --- # Works with most geoms! .left-code[ ```r car_hist <- ggplot(mpg, aes(x = hwy)) + geom_histogram(binwdith = 2, boundary = 0, color = "white") ``` ```r ggplotly(car_hist) ``` ] .pull-right.small-code[
] --- # Save as HTML .box-inv-6[Save a self-contained HTML version of it with<br>`saveWidget()` in the **htmlwidgets** R package] ```r # This is like ggsave, but for interactive HTML plots htmlwidgets::saveWidget(interactive_plot, "fancy_plot.html") ``` --- # Fully documented .box-inv-6[The [documentation](https://plotly.com/ggplot2/) for ggplot2 + plotly is full of<br>examples of how to customize everything] .box-inv-6[Rely on that ↑ + Google to make<br>really fancy (and easy!) interactive plots] --- # Interactivity .box-inv-6.medium[Single plots with **plotly**] .box-6.small.sp-after[Easy!] -- .box-inv-6.medium[Dashboards with **flexdashboard**] .box-6.small.sp-after[Slightly more complicated] --- # Dashboards with **flexdashboard** .box-inv-6[Use basic R Markdown to build a dashboard!] .center[ <figure> <img src="img/03/flexdashboard-simple.png" alt="flexdashboard simple layout" title="flexdashboard simple layout" width="80%"> </figure> ] --- # Dashboards with **flexdashboard** .box-inv-6[Make any kind of block arrangement] .center[ <figure> <img src="img/03/flexdashboard-complex.png" alt="flexdashboard complex layout" title="flexdashboard complex layout" width="75%"> </figure> ] --- # Dashboards with **flexdashboard** .box-inv-6[Add other elements like text and gauges] .center[ <figure> <img src="img/03/value-boxes.png" alt="flexdashboard value boxes" title="flexdashboard value boxes" width="80%"> </figure> <figure> <img src="img/03/gauges.png" alt="flexdashboard gauges" title="flexdashboard gauges" width="80%"> </figure> ] --- # Example dashboards .center[ <figure> <img src="img/03/flexdashboard-example.png" alt="flexdashboard simple example" title="flexdashboard simple example" width="80%"> <figcaption><a href="https://beta.rstudioconnect.com/jjallaire/htmlwidgets-ggplotly-geoms/" target="_blank">ggplot2 geoms</a></figcaption> </figure> ] --- # Example dashboards .center[ <figure> <img src="img/03/flexdashboard-heatmap.png" alt="flexdashboard heatmap example" title="flexdashboard heatmap example" width="80%"> <figcaption><a href="https://beta.rstudioconnect.com/jjallaire/htmlwidgets-d3heatmap/" target="_blank">NBA scoring</a></figcaption> </figure> ] --- # Example dashboards .center[ <figure> <img src="img/03/utah-covid.png" alt="Utah's COVID-19 dashboard" title="Utah's COVID-19 dashboard" width="90%"> <figcaption><a href="https://coronavirus-dashboard.utah.gov/" target="_blank">Utah's COVID-19 dashboard</a></figcaption> </figure> ] --- # Outstanding documentation .box-inv-6[The [documentation](https://rmarkdown.rstudio.com/flexdashboard/index.html) for **flexdashboard** is<br>full of examples and details of everything you can do] .box-inv-6[Rely on that ↑ + Google to make<br>really fancy (and easy!) dashboards!] --- # Interactivity .box-inv-6.medium[Single plots with **plotly**] .box-6.small.sp-after[Easy!] .box-inv-6.medium[Dashboards with **flexdashboard**] .box-6.small.sp-after[Slightly more complicated] -- .box-inv-6.medium[Complete interactive apps with **Shiny**] .box-6.small[Super complicated!] --- # Shiny .box-inv-6.medium[Shiny is a complete web application framework for interactive statistics] -- .box-inv-6.sp-after[It's super complex and hard for beginners] -- .box-6.medium[I've never made a standalone Shiny app!] .box-6.small[(And I don't plan on trying anytime soon)] --- # Lots of resources to help start .box-inv-6[RStudio has [a whole website for helping you get started](https://shiny.rstudio.com/tutorial/)] .center[ <figure> <img src="img/03/shiny-getting-started.png" alt="Getting started with Shiny" title="Getting started with Shiny" width="50%"> <figcaption><a href="https://shiny.rstudio.com/tutorial/" target="_blank">Getting started with Shiny</a></figcaption> </figure> ] --- # Really neat examples! .center[ <figure> <img src="img/03/isee-shiny.png" alt="iSEE" title="iSEE" width="65%"> <figcaption><a href="https://shiny.rstudio.com/gallery/isee.html" target="_blank">iSEE (interactive SummarizedExperiment Explorer)</a></figcaption> </figure> ] --- # Really neat examples! .center[ <figure> <img src="img/03/covid-shiny.png" alt="COVID-19 tracker" title="COVID-19 tracker" width="65%"> <figcaption><a href="https://shiny.rstudio.com/gallery/covid19-tracker.html" target="_blank">COVID-19 tracker</a></figcaption> </figure> ] --- # Really neat examples! .center[ <figure> <img src="img/03/lego-shiny.png" alt="Living in the LEGO world" title="Living in the LEGO world" width="65%"> <figcaption><a href="https://shiny.rstudio.com/gallery/lego-world.html" target="_blank">Living in the LEGO world</a></figcaption> </figure> ] --- # flexdashboard + Shiny .box-inv-6[You can [use reactive Shiny things in flexdashboards](https://rmarkdown.rstudio.com/flexdashboard/shiny.html)<br>without building a complete Shiny app!] .box-6.small[I *have* done this] .center[ <figure> <img src="img/03/why-donors-donate.png" alt="Why Donors Donate" title="Why Donors Donate" width="45%"> </figure> ]