Learn R Programming

animint (version 2017.01.04)

animint2dir: Compile and render an animint in a local directory

Description

An animint is a list of ggplots and options that defines an interactive animation and can be viewed in a web browser. Several new aesthetics control interactivity. The most important two are

  • aes(showSelected=variable) means that only the subset of the data that corresponds to the selected value of variable will be shown.

  • aes(clickSelects=variable) means that clicking this geom will change the currently selected value of variable.

The others are described on https://github.com/tdhock/animint/wiki/Advanced-features-present-animint-but-not-in-ggplot2

Usage

animint2dir(plot.list, out.dir = tempfile(), json.file = "plot.json",
  open.browser = interactive(), css.file = "")

Arguments

plot.list

a named list of ggplots and option lists.

out.dir

directory to store html/js/csv files.

json.file

character string that names the JSON file with metadata associated with the plot.

open.browser

Should R open a browser? If yes, be sure to configure your browser to allow access to local files, as some browsers block this by default (e.g. chrome).

css.file

character string for non-empty css file to include. Provided file will be copied to the output directory as styles.css

Value

invisible list of ggplots in list format.

Details

Supported ggplot2 geoms:

  • point

  • jitter

  • line

  • rect

  • tallrect (new with this package)

  • segment

  • hline

  • vline

  • bar

  • text

  • tile

  • raster

  • ribbon

  • abline

  • density

  • path

  • polygon

  • histogram

  • violin

  • linerange

  • step

  • contour

  • density2d

  • area

  • freqpoly

  • hex

Unsupported geoms:

  • rug

  • dotplot

  • quantile - should *theoretically* work but in practice does not work

  • smooth - can be created using geom_line and geom_ribbon

  • boxplot - can be created using geom_rect and geom_segment

  • crossbar - can be created using geom_rect and geom_segment

  • pointrange - can be created using geom_linerange and geom_point

  • bin2d - bin using ddply() and then use geom_tile()

  • map - can be created using geom_polygon or geom_path

Supported scales:

  • alpha,

  • fill/colour (brewer, gradient, identity, manual)

  • linetype

  • x and y axis scales, manual break specification, label formatting

  • x and y axis theme elements: axis.line, axis.ticks, axis.text, axis.title can be set to element_blank(); other theme modifications not supported at this time, but would be possible with custom css files.

  • area

  • size

Unsupported scales:

  • shape. Open and closed circles can be represented by manipulating fill and colour scales and using default (circle) points, but d3 does not support many R shape types, so mapping between the two is difficult.

See Also

ggplot2

Examples

Run this code
# NOT RUN {
## Make a Gapminder plot (aka Google motion chart), which is actually
## just a scatterplot with size and color that moves over time.
library(animint)
data(WorldBank)
gapminder <-
  list(title="Linked scatterplot and time series",
       ts=ggplot()+
       make_tallrect(WorldBank, "year")+
       geom_line(aes(year, life.expectancy, group=country, color=region,
                     clickSelects=country),
                 data=WorldBank, size=4, alpha=3/5),
       time=list(variable="year",ms=3000),
       duration=list(year=1000),
       scatter=ggplot()+
       geom_point(aes(fertility.rate, life.expectancy, clickSelects=country,
                      key=country,
                      showSelected=year, colour=region, size=population),
                  data=WorldBank)+
       geom_text(aes(fertility.rate, life.expectancy, label=country,
                     showSelected=country, showSelected2=year),
                 data=WorldBank)+
       make_text(WorldBank, 5, 80, "year")+
       scale_size_animint(pixel.range=c(2,20), breaks=10^(4:9)))
animint2dir(gapminder, "WorldBank-viz")

data(worldPop)
## Linked bar and line plots of world population by subcontinent,
## inspired by polychartjs.
popPlots <-
  list(bars=ggplot()+
       geom_bar(aes(x=subcontinent, y=population,
                    clickSelects=subcontinent, showSelected=year),
                data=worldPop, stat="identity", position="identity")+
       ## This make_text creates a geom_text that shows the current
       ## selected value of the year variable.
       make_text(worldPop, 1, 3e6, "year")+
       coord_flip(),
       lines=ggplot()+
       ## This make_tallrect tiles the background of the lineplot with
       ## rects that can be clicked to select the year variable.
       make_tallrect(worldPop, "year")+
       ## This geom_point does not have aes(clickSelects) so its alpha
       ## transparency behaves normally: all points have alpha=1/4.
       geom_point(aes(year, population, colour=type),
                  data=worldPop, size=4, alpha=1/4)+
       ## This geom_line DOES have aes(clickSelects) so only the
       ## selected line has the specified alpha=3/4. The other
       ## unselected lines have 0.5 less (alpha=1/4).
       geom_line(aes(year, population, group=subcontinent,
                     clickSelects=subcontinent),
                 data=worldPop, size=4, alpha=3/4))
animint2dir(popPlots, "WorldPop-interactive")
## Make it animated by specifying year as the variable to animate and
## an interval of 2000 milliseconds between animation frames.
popAnim <- c(popPlots, list(time=list(variable="year",ms=2000)))
animint2dir(popAnim, "WorldPop-animated")
## Make the animation smooth by specifying a duration of 1000 ms for
## geoms with aes(showSelected=year).
popSmooth <- c(popAnim, list(duration=list(year=1000)))
animint2dir(popSmooth, "WorldPop-smooth")

# }

Run the code above in your browser using DataLab