knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(animbook)
The inspiration for this package comes from the NY Times animated plot. The challenge of reproducing the animation is the amount of code required. This package tries to solve that by reducing it to only three steps. This package also adds a new element of proportional shaded area.
In this stage, we first need to transform the data into the categorized format. This function will calculate the scaling based on the argument the user provided.
The data needs to contain the following variable for the function to work:
dbl_change
, A data frame that contained the numerical values.id
, needs to be a factor variable.values
, needs to be a numeric variable.time
, needs to be an integer variable.categorized_dbl <- anim_prep(data = dbl_change, id = id, values = values, time = time)
There are also additional options that allow the user to customize.
The function can calculate four different scales using these options.
# rank scaling rank_scaling <- anim_prep(data = dbl_change, id = id, values = values, time = time) # absolute scaling absolute_scaling <- anim_prep(data = dbl_change, id = id, values = values, time = time, scaling = "absolute") # rank scaling by group rank_group_scaling <- anim_prep(data = dbl_change, id = id, values = values, time = time, group_scaling = gp) # absolute scaling by group absolute_group_scaling <- anim_prep(data = dbl_change, id = id, values = values, time = time, group_scaling = gp, scaling = "absolute") rank_scaling
This function will return a categorized
data.
After preparing the data, we can now plot it. There are three plots available in this package:
kangaroo
, which plots the observation's movement over time.wallaby
, which subset the plot to either top
or bottom
and see which group they are in after the observational period.funnel_web_spider
, which is a faceted plot by time variable.dbl_categorized <- anim_prep(data = dbl_change, id = id, values = values, time = time, group = gp) # kangaroo plot kangaroo_plot(dbl_categorized) # wallaby plot wallaby_plot(dbl_categorized) # funnel web spider plot funnel_web_plot(dbl_categorized)
The kangaroo
and wallaby
plots can be animated using the function of the next stage. funnel_web_spider
only supported static plot. We can also choose whether we want to animate the plot using gganimate or plotly.
To animate the plot, we need to save the plot into an object, which then can be passed on to the function.
aeles_cat <- anim_prep_cat(data = aeles, id = id, values = party, time = year, group = gender) p <- wallaby_plot(aeles_cat) p2 <- anim_animate(p) gganimate::animate(p2)
knitr::include_graphics("figures/example.gif")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.