class: center, middle, inverse, title-slide .title[ # Animate ] .author[ ### Visualizing and communicating data with R
The R Bootcamp @ Unibas
] .date[ ### November 2023 ] --- layout: true <div class="my-footer"> <span style="text-align:center"> <span> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/by-sa.png" height=14 style="vertical-align: middle"/> </span> <a href="https://therbootcamp.github.io/"> <span style="padding-left:82px"> <font color="#7E7E7E"> https://therbootcamp.github.io </font> </span> </a> <a href="https://therbootcamp.github.io/"> <font color="#7E7E7E"> The R Bootcamp | November 2023 </font> </a> </span> </div> --- layout: true <div class="my-footer"> <span style="text-align:center"> <span> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/by-sa.png" height=14 style="vertical-align: middle"/> </span> <a href="https://therbootcamp.github.io/"> <span style="padding-left:82px"> <font color="#7E7E7E"> https://therbootcamp.github.io </font> </span> </a> <a href="https://therbootcamp.github.io/"> <font color="#7E7E7E"> The R Bootcamp | June 2022 </font> </a> </span> </div> --- .pull-left3[ # Animations with <mono>gganimate</mono> <ul> <li class="m1"><span><mono>gganimate</mono> provides functions to <high>animate any <mono>ggplot</mono></high>.</span></li> <li class="m2"><span>Animations are useful to <high>highlight change</high> in data.</span></li> </ul> ] .pull-right6[ <img src="animate_files/figure-html/unnamed-chunk-2-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Transitions <ul> <li class="m1"><span><mono>gganimate</mono>'s workhorses are several <high>transition function</high>.</span></li> <ul class="level"> <li><span><mono>transition_states()</mono> iterates through distinct values</span> </li> <li><span><mono>transition_time()</mono> iterates through continuous values </span></li> <li><span><mono>transition_reveal()</mono> iterates through in a cumulative fashion</span></li> <li><span><mono>transition_filter()</mono> iterates through logicals</span></li> <li><span><mono>transition_layers()</mono> iterates through plot layers</span></li> </ul> </ul> ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-3-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # <mono>*_states</mono> <ul> <li class="m1"><span><mono>transition_states()</mono> iterates through distinct states.</span></li> </ul> ```r # map code quarters_map %>% ggplot() + geom_sf(aes(fill = income_mean), col = "white") + theme_void() + scale_fill_viridis_c(name = 'Income', option = "E") + # add transitions transition_states(year) ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-5-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Labels <ul> <li class="m1"><span><high>Annotation can be parameterized</high> using <mono>{closest_state}</mono> to reflect the changes in state.</span></li> </ul> ```r # map code quarters_map %>% ggplot(...) + # add transitions transition_states(year) + # add labels ggtitle(label = "Inequality in Basel", subtitle = "... in {closest_state}") + ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-7-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # `*_reveal` <ul> <li class="m1"><span><mono>transition_reveal()</mono> iterates through states in a cumulative fashion.</span></li> </ul> ```r # plot code basel %>% ggplot(...) + # transition reveal transition_reveal(year) ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-9-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Labels <ul> <li class="m1"><span><high>Annotation can be parameterized</high> using <mono>{frame_along}</mono> to reflect the changes in state.</span></li> </ul> ```r # plot code basel %>% ggplot(...) + # add transitions transition_reveal(year) + # add labels ggtitle(label = "Inequality in Basel", subtitle = "... in {frame_along}") + labs(x = "Year", y = "Income median") ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-11-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Types <ul> <li class="m1"><span><mono>transition_*</mono> functions are sensitive to the <high>type of the transiton variable</high>.</span></li> </ul> ```r basel %>% # change to integer mutate(year = as.integer(year)) + # plot ggplot(...) + # add transitions transition_reveal(year) + # add labels ... ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-13-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Length <ul> <li class="m1"><span>The <high>length of the transitions</high> can be manipulated using the <mono>transition_length</mono> and <state_length> arguments.</span></li> </ul> ```r # plot code basel %>% ggplot(...) + # transition length 1: Default transition_states(year, transition_length = 1) + # labels ggtitle(label = "Inequality in Basel", subtitle = "... to {closest_state}") ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-15-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Length <ul> <li class="m1"><span>The <high>length of the transitions</high> can be manipulated using the <mono>transition_length</mono> and <state_length> arguments.</span></li> </ul> ```r # plot code basel %>% ggplot(...) + # transition length 10 transition_states(year, transition_length = 10) + # labels ggtitle(label = "Inequality in Basel", subtitle = "... to {closest_state}") ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-17-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Length <ul> <li class="m1"><span>The <high>length of the transitions</high> can be manipulated using the <mono>transition_length</mono> and <state_length> arguments.</span></li> </ul> ```r # plot code basel %>% ggplot(...) + # transition length 10 transition_states(year, transition_length = 10, state_length = 0) + # labels ggtitle(label = "Inequality in Basel", subtitle = "... to {closest_state}") ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-19-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Length <ul> <li class="m1"><span>The <high>length of the transitions</high> can be manipulated using the <mono>transition_length</mono> and <state_length> arguments.</span></li> </ul> ```r # plot code basel %>% ggplot(...) + # transition length 10 transition_states(year, transition_length = 10, state_length = 0) + # labels ggtitle(label = "Inequality in Basel", subtitle = "... to {closest_state}") ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-21-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Shadows <ul> <li class="m1"><span>There are several <mono>shadow_*</mono> to help <high>emphasize changes</high> across states/time.</span></li> <ul class="level"> <li><span><mono>shadow_mark()</mono> shows past and/or future states</span></li> <li><span><mono>shadow_wake()</mono> shows wake of data</span></li> <li><span><mono>shadow_trail()</mono> shows past data</span></li> </ul> </ul> ```r # plot code basel %>% ggplot(...) + # transition length 10 transition_states(year, transition_length = 10) + # shadow shadow_mark(alpha = .1) ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-23-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Shadows <ul> <li class="m1"><span>There are several <mono>shadow_*</mono> to help <high>emphasize changes</high> across states/time.</span></li> <ul class="level"> <li><span><mono>shadow_mark()</mono> shows past and/or future states</span></li> <li><span><mono>shadow_wake()</mono> shows wake of data</span></li> <li><span><mono>shadow_trail()</mono> shows past data</span></li> </ul> </ul> ```r # plot code basel %>% ggplot(...) + # transition length 10 transition_states(year, transition_length = 10) + # shadow shadow_mark(alpha = .1, past = TRUE, future = TRUE) ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-25-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Shadows <ul> <li class="m1"><span>There are several <mono>shadow_*</mono> to help <high>emphasize changes</high> across states/time.</span></li> <ul class="level"> <li><span><mono>shadow_mark()</mono> shows past and/or future states</span></li> <li><span><mono>shadow_wake()</mono> shows wake of data</span></li> <li><span><mono>shadow_trail()</mono> shows past data</span></li> </ul> </ul> ```r # plot code basel %>% ggplot(...) + # transition length 10 transition_states(year, transition_length = 10) + # shadow shadow_wake(wake_length = .1) ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-27-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Shadows <ul> <li class="m1"><span>There are several <mono>shadow_*</mono> to help <high>emphasize changes</high> across states/time.</span></li> <ul class="level"> <li><span><mono>shadow_mark()</mono> shows past and/or future states</span></li> <li><span><mono>shadow_wake()</mono> shows wake of data</span></li> <li><span><mono>shadow_trail()</mono> shows past data</span></li> </ul> </ul> ```r # plot code basel %>% ggplot(...) + # transition length 10 transition_states(year, transition_length = 10) + # shadow shadow_wake(wake_length = .3) ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-29-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # <mono>animate()</mono> <ul> <li class="m1"><span><mono>animate()</mono> can be used to fine-tune the animation.</span></li> </ul> ```r # plot code anim <- basel %>% ggplot(...) + # transition length 10 transition_states(year, transition_length = 10) + # shadow shadow_wake(wake_length = .3) # fine-tune animate(anim, nframes = 3) ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-31-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left45[ # <mono>anim_save()</mono> <ul> <li class="m1"><span><mono>anim_save()</mono> can be used to <high>save the animation</high> as <mono>.gif</mono> files.</span></li> </ul> ```r # plot code anim <- basel %>% ggplot(...) + # transition length 10 transition_states(year, transition_length = 10) + # shadow shadow_wake(wake_length = .3) # fine-tune anim_save(filename = "anim.gif", animation = anim) ``` ] .pull-right45[ <br><br> <img src="animate_files/figure-html/unnamed-chunk-33-1.gif" style="display: block; margin: auto;" /> ] --- .pull-left3[ # Inteactive plots with <mono>plotly</mono> <ul> <li class="m1"><span><mono>plotly</mono> is a <high>framework for interactive visualizations</high> based on JavaScript.</span></li> <li class="m2"><span>Offers a basic translator or ggplot .</span></li> </ul> ] .pull-right6[ <br>
] --- .pull-left45[ # <mono>ggplotly</mono> <ul> <li class="m1"><span><mono>ggplotly</mono> <high>translates a ggplot</high> plot object into plotly.</span></li> </ul> ```r # map code p = quarters_map %>% ggplot(...) # turn into plotly p = ggplotly(p) # show p ``` ] .pull-right45[ <br><br>
] --- .pull-left45[ # <mono>ggplotly</mono> <ul> <li class="m1"><span><mono>ggplotly</mono> <high>translates a ggplot</high> plot object into plotly.</span></li> </ul> ```r # map code p = quarters_map %>% ggplot(...) # turn into plotly p = ggplotly(p) # show p ``` ] .pull-right45[ <br><br><br><br> ```r names(p) ``` ``` [1] "x" "width" [3] "height" "sizingPolicy" [5] "dependencies" "elementId" [7] "preRenderHook" "jsHooks" ``` ```r names(p$x) ``` ``` [1] "data" "layout" "config" "source" [5] "attrs" "cur_data" "visdat" ``` ```r names(p$x$data[[1]]) ``` ``` [1] "x" "y" "type" [4] "mode" "line" "hoveron" [7] "hoverinfo" "showlegend" "_isGraticule" [10] "xaxis" "yaxis" "name" ``` ] --- .pull-left45[ # <mono>ggplotly</mono> <ul> <li class="m1"><span><mono>ggplotly</mono> <high>translates a ggplot</high> plot object into plotly.</span></li> </ul> ```r # map code p = quarters_map %>% ggplot(...) # turn into plotly p = ggplotly(p) # change labels p$x$data = lapply(p$x$data, function(x){ x$text = str_replace(x$text, "TYPE", "Quarter"); x}) p$x$data = lapply(p$x$data, function(x){ x$text = str_replace(x$text, "income_mean", "Income"); x}) # show plot p ``` ] .pull-right45[ <br><br>
] --- .pull-left45[ # <mono>tooltip</mono> <ul> <li class="m1"><span><mono>tooltip</mono> permits the selection <high>self-created hover information</high>.</span></li> </ul> ```r # map code p = quarters_map %>% ggplot(aes( text = paste0( "Quarter: ", quarter, "<br>Income: ", income_mean) )) # turn into plotly p = ggplotly(p, tooltip = "text") # show plot p ``` ] .pull-right45[ <br><br>
] --- class: middle, center <h1><a href="">Practical</a></h1>