library(pacman)
p_load(tidyverse, purrr, gapminder, plotly, trelliscopejs, echarts4r, gganimate)
Interactive and Dynamic Animation
In this R Notebook we will try out some interactive visualizations and some dynamic visualizations that are possible using R.
Using the gapminder dataset.
gapminder
# A tibble: 1,704 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 1952 28.8 8425333 779.
2 Afghanistan Asia 1957 30.3 9240934 821.
3 Afghanistan Asia 1962 32.0 10267083 853.
4 Afghanistan Asia 1967 34.0 11537966 836.
5 Afghanistan Asia 1972 36.1 13079460 740.
6 Afghanistan Asia 1977 38.4 14880372 786.
7 Afghanistan Asia 1982 39.9 12881816 978.
8 Afghanistan Asia 1987 40.8 13867957 852.
9 Afghanistan Asia 1992 41.7 16317921 649.
10 Afghanistan Asia 1997 41.8 22227415 635.
# ℹ 1,694 more rows
Plot by continent.
<- gapminder %>% group_by(continent, country) %>%
gapm ggplot(aes(x = gdpPercap, y = lifeExp,
size = pop, colour = country )) +
geom_point() +
scale_x_log10() +
facet_wrap(~continent) +
theme(legend.position = "none")
gapm
ggplotly(gapm)
Using facet_trelliscope can be used to create an interactive collection of plots. Can you image making a million plots?
%>% ggplot(aes(x = log10(gdpPercap), y = lifeExp,
gapminder color = country, size = pop)) +
geom_point() +
theme_bw() +
theme(legend.position = "none") +
facet_trelliscope(~ continent,
nrow = 2, ncol = 3)
<- gapminder %>%
gapm_trel 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.
<- map(unique(gapminder$year), function(x){
titles 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")
%>% ggplot(aes(x = gdpPercap, y = lifeExp,
gapminder 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.
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')
<- gapminder %>% ggplot(aes(x = gdpPercap, y = lifeExp,
goo 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)
Try echarts. Globe
Run the following code once to install the *echarts4r.assets$ package.
# install.packages("remotes")
# remotes::install_github("JohnCoene/echarts4r.assets")
library(echarts4r.assets)
<- read.csv(
airports 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)))
<- read.csv(
flights 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)