--- title: "Interactive and Dynamic Animation" author: "Prof. Eric A. Suess" format: html: self-contained: true --- In this R Notebook we will try out some interactive visualizations and some dynamic visualizations that are possible using R. ```{r} library(pacman) p_load(tidyverse, purrr, gapminder, plotly, trelliscopejs, echarts4r, gganimate) ``` Using the gapminder dataset. ```{r} gapminder ``` Plot by continent. ```{r} gapm <- gapminder %>% group_by(continent, country) %>% ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, colour = country )) + geom_point() + scale_x_log10() + facet_wrap(~continent) + theme(legend.position = "none") gapm ``` ```{r} ggplotly(gapm) ``` Using facet_trelliscope can be used to create an interactive collection of plots. Can you image making a million plots? ```{r} #| eval=FALSE gapminder %>% ggplot(aes(x = log10(gdpPercap), y = lifeExp, color = country, size = pop)) + geom_point() + theme_bw() + theme(legend.position = "none") + facet_trelliscope(~ continent, nrow = 2, ncol = 3) ``` ```{r} #| eval=FALSE gapm_trel <- gapminder %>% ggplot(aes(x = log10(gdpPercap), y = lifeExp, color = country, size = pop)) + geom_point() + theme_bw() + theme(legend.position = "none") + facet_trelliscope(~ country + continent, nrow = 2, ncol = 3) gapm_trel ``` Try echarts. ```{r} titles <- map(unique(gapminder$year), function(x){ list( text = "Gapminder", subtext = x, left = "center" ) }) gapminder %>% group_by(year) %>% e_charts(gdpPercap, timeline = TRUE) %>% e_scatter(lifeExp, pop, country) %>% e_y_axis(min = 20, max = 85) %>% e_x_axis(type = "log", min = 100, max = 100000) %>% e_timeline_serie(title = titles) %>% e_tooltip() %>% e_timeline_opts( show = TRUE, orient = "vertical", symbol = "none", right = 0, top = 20, bottom = 20, height = NULL, width = 45, inverse = TRUE, playInterval = 1000, autoPlay = TRUE, controlStyle = list( showNextBtn = FALSE, showPrevBtn = FALSE ), label = list( fontSize = 8 ) ) %>% e_theme("dark") ``` ```{r} gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, colour = country)) + geom_point(alpha = 0.7, show.legend = FALSE) + scale_colour_manual(values = country_colors) + scale_size(range = c(2, 12)) + scale_x_log10() + facet_wrap(~ continent) ``` Try gganimate. Takes a long to process before the animation is available. ```{r} #| eval=FALSE ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) + geom_point(alpha = 0.7, show.legend = FALSE) + scale_colour_manual(values = country_colors) + scale_size(range = c(2, 12)) + scale_x_log10() + facet_wrap(~continent) + # Here comes the gganimate specific bits labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') + transition_time(year) + ease_aes('linear') ``` ```{r} #| eval=FALSE goo <- gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp, size = pop, colour = country)) + geom_point(alpha = 0.7, show.legend = FALSE) + scale_colour_manual(values = country_colors) + scale_size(range = c(2, 12)) + scale_x_log10() + facet_wrap(~continent) + # Here comes the gganimate specific bits labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') + transition_time(year) + ease_aes('cubic-in-out') anim_save("goo.gif", goo) ``` ![](goo.gif) Try echarts. [Globe](https://echarts4r.john-coene.com/articles/globe.html?q=airport#scatter-3d) Run the following code once to install the *echarts4r.assets$ package. ```{r} #| eval=FALSE # install.packages("remotes") # remotes::install_github("JohnCoene/echarts4r.assets") ``` ```{r} #| eval=FALSE library(echarts4r.assets) airports <- read.csv( paste0("https://raw.githubusercontent.com/plotly/datasets/", "master/2011_february_us_airport_traffic.csv") ) airports |> e_charts(long) |> e_globe( environment = ea_asset("starfield"), base_texture = ea_asset("world"), globeOuterRadius = 100 ) |> e_scatter_3d(lat, cnt, coord_system = "globe", blendMode = 'lighter') |> e_visual_map(inRange = list(symbolSize = c(1, 10))) ``` ```{r} #| eval=FALSE flights <- read.csv( paste0("https://raw.githubusercontent.com/plotly/datasets/", "master/2011_february_aa_flight_paths.csv") ) flights |> e_charts() |> e_globe( environment = ea_asset("starfield"), base_texture = ea_asset("world topo"), height_texture = ea_asset("world topo"), displacementScale = 0.05 ) |> e_lines_3d( start_lon, start_lat, end_lon, end_lat, name = "flights", effect = list(show = TRUE) ) |> e_legend(FALSE) ```