library(animint2)
data(WorldBank)
animint(
timeSeries=ggplot()+
theme_animint(width=1000, last_in_row=TRUE)+
theme(legend.position="none")+
make_tallrect(WorldBank, "year")+
geom_label_aligned(aes(
year, life.expectancy, label=country, color=region,
hjust=ifelse(year==1960, 1, 0)),
alignment="vertical",
showSelected=c("region","country"),
clickSelects="country",
data=subset(WorldBank, year %in% c(1960, 2011)))+
scale_x_continuous(
limits=c(1950, 2020),
breaks=seq(1960, 2010, by=10))+
scale_y_continuous(breaks=seq(10, 100, by=10))+
geom_line(aes(
year, life.expectancy, group=country, color=region),
clickSelects="country",
showSelected="region",
alpha=1, # opacity when selected
alpha_off=0.1, # opacity when not selected
data=WorldBank),
scatter=ggplot()+
geom_point(aes(
life.expectancy, fertility.rate,
key=country, color=region, size=population),
showSelected="year",
clickSelects="country",
chunk_vars=character(),
data=WorldBank)+
geom_text(aes(
life.expectancy, fertility.rate, label=country, key=country),
showSelected=c("region","year","country"),
clickSelects="country",
chunk_vars=character(),
data=WorldBank)+
make_text(WorldBank, 55, 9, "year")+
scale_x_continuous(breaks=seq(10, 100, by=10))+
scale_y_continuous(breaks=seq(1, 10))+
scale_size_animint(breaks=10^seq(5, 9)),
selector.types=list(country="multiple"),
first=list(country=c("Canada","Japan","France")),
duration=list(year=500),
time=list(variable="year", ms=1000)
)This is the first post in our new blog about the animint project. Welcome!
The purpose of the animint project is to maintain the animint2 R package, for creating animated interactive data visualizations using R ggplot code. To learn how to create animated interactive data visualizations, please read
- The online manual, Animated interactive data visualization using animint2 in R, recently ported to quarto for rendering and Netlify for deployment (Video, Slides from my webinar last week about how to set that up).
- Or its French translation, Visualisation interactive de données dans R avec animint2
Below is a demo of an animint of data from the World Bank. Since animint2 is a fork of ggplot2 (thanks ggplot2 developers!), the code below should look familiar to you if you have some experience with ggplot2. There are some new code elements for interactive features:
clickSelectsandshowSelectedare used to specify interactivity for each geom.theme_animint()is used to define ggplotwidthin pixels, and layout of ggplots in a table (last_in_row).- New geoms
geom_tallrect(), rectangle spanning the entire vertical range (no need to specify ymin and ymax).geom_label_aligned(), readable text labels which avoid vertical or horizontal collisions.
make_tallrect()andmake_text()are functions for quickly making interactive geoms (tallrect for selecting year, text for showing selected year).scale_size_animint()draws a size scale with units in pixels.selector.typesoption list specifies single or multiple selection.firstoption list specifies the selection when the data viz is first rendered.durationoption list andaes(key)specify smooth transitions.timeoption list specifies animation.
The animint above has two ggplots, with several kinds of direct manipulation interactions:
- the top ggplot shows time series of life expectancy, one curve per country.
- click curves or text labels to select or de-select a country.
- click grey bars in background to select a year.
- the bottom ggplot is a scatterplot showing the relationship of life expectancy and fertility rate, for the currently selected year.
- click a dots or text to select or de-select a country.
- click the legend to select or de-select a region.
There are also indirect manipulation interactions possible, by clicking the buttons, Show selection menus, etc.
Currently, the quarto rendering of animints requires a custom render.R script, which copies the rendered animint directories to the quarto _site directory. For future posts on this blog, make sure to use one animint() per code chunk, and make sure that each animint code chunk has a unique name, as documented in Section 5.2 of the Animint manual.