Skip to contents

Introduction

manimR is designed to work seamlessly with the tidyverse ecosystem. This vignette shows how to create data-driven animations, integrate with ggplot2, and use tidy data workflows. ## Pipe-Friendly Design

manimR uses the magrittr pipe (%>%) throughout:

library(manimR)
library(dplyr)

scene("pipeline") %>%
  add_text("Data Pipeline Demo") %>%
  write_in() %>%
  pause(1) %>%
  fade_out() %>%
  render()

Data-Driven Animations

Animated Scatter Plots

Create scatter plots that animate from your data:

library(manimR)
library(dplyr)

mtcars %>%
  animate_scatter(
    x = mpg,
    y = wt,
    color = cyl,
    duration = 3
  ) %>%
  render()

Animated Bar Charts

# Create sample data
sales_data <- tibble(
  product = c("A", "B", "C", "D"),
  revenue = c(120, 85, 200, 150)
)

sales_data %>%
  animate_bar_chart(
    x = product,
    y = revenue,
    duration = 2
  ) %>%
  render()

Animated Line Charts

# Time series data
time_data <- tibble(
  month = 1:12,
  sales = c(100, 120, 115, 140, 160, 155, 170, 185, 180, 200, 210, 230)
)

time_data %>%
  animate_line_chart(
    x = month,
    y = sales,
    color = "#4CAF50",
    duration = 3
  ) %>%
  render()

ggplot2 Integration

Convert existing ggplot2 visualizations to manimR scenes:

library(ggplot2)

# Create a ggplot
p <- ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
  geom_point(size = 3) +
  theme_minimal() +
  labs(title = "MPG vs Weight", color = "Cylinders")

# Convert to manimR
convert_ggplot_to_manim(p, animate_build = TRUE) %>%
  render()

Working with Grouped Data

Animate data that changes over groups or time:

library(dplyr)

# Sample grouped data
group_data <- tibble(
  category = rep(c("A", "B", "C"), each = 4),
  quarter = rep(1:4, 3),
  value = c(10, 15, 20, 25,   # A
            8, 12, 18, 22,    # B
            12, 14, 16, 20)   # C
)

# Create animation showing each group
scene("grouped_animation") %>%
  add_text("Sales by Category", position = c(0, 3.5, 0), size = 48) %>%
  write_in() %>%
  pause(0.5)

# Add each group's data
for (cat in c("A", "B", "C")) {
  cat_data <- filter(group_data, category == cat)

  scene %>%
    add_text(paste("Category:", cat), position = c(-4, 2.5, 0)) %>%
    fade_in() %>%
    # Add visualization for this category
    pause(2) %>%
    fade_out()
}

Animating Data Transformations

Show data pipeline steps:

scene("data_pipeline") %>%
  # Show original data
  add_text("Raw Data", position = c(0, 3, 0), size = 48) %>%
  write_in() %>%
  pause(1) %>%

  # Show filtering
  shift(c(0, -1, 0)) %>%
  add_text("filter(year == 2020)", position = c(0, 2, 0),
           color = "#4CAF50", size = 36) %>%
  fade_in() %>%
  pause(1) %>%

  # Show grouping
  add_text("group_by(region)", position = c(0, 1, 0),
           color = "#2196F3", size = 36) %>%
  fade_in() %>%
  pause(1) %>%

  # Show summarizing
  add_text("summarise(total = sum(sales))", position = c(0, 0, 0),
           color = "#FF9800", size = 36) %>%
  fade_in() %>%
  pause(1) %>%

  render()

Using purrr for Multiple Animations

Generate multiple related animations:

library(purrr)

# Create animations for different variables
variables <- c("mpg", "hp", "wt")

animations <- map(variables, function(var) {
  scene(paste0(var, "_distribution")) %>%
    add_text(paste("Distribution of", toupper(var)),
             position = c(0, 3, 0), size = 48) %>%
    write_in() %>%
    # Add histogram animation here
    pause(2) %>%
    render(output = paste0(var, "_dist.mp4"))
})

Combining with dplyr

Use dplyr to prepare data for animation:

library(dplyr)

# Prepare summary statistics
summary_stats <- mtcars %>%
  group_by(cyl) %>%
  summarise(
    mean_mpg = mean(mpg),
    mean_hp = mean(hp),
    count = n(),
    .groups = "drop"
  )

# Animate the summary
summary_stats %>%
  animate_bar_chart(
    x = cyl,
    y = mean_mpg,
    duration = 2
  ) %>%
  render()

Best Practices

1. Prepare Data First

# Good: Prepare data, then animate
clean_data <- raw_data %>%
  filter(!is.na(value)) %>%
  mutate(category = as_factor(category))

clean_data %>%
  animate_scatter(x, y) %>%
  render()

2. Use Meaningful Names

# Good: Descriptive scene names
scene("sales_by_region_2024") %>%
  # ...

# Avoid: Generic names
scene("scene1") %>%
  # ...

3. Chain Operations Logically

# Group related operations
scene("demo") %>%
  # Setup
  add_text("Title") %>%
  write_in() %>%

  # Main content
  add_latex("f(x) = x^2") %>%
  write_in() %>%
  highlight_term("x^2") %>%

  # Cleanup
  pause(1) %>%
  fade_out() %>%

  render()

Next Steps

  • vignette("ggplot2-conversion"): Detailed ggplot2 conversion guide
  • vignette("performance-optimization"): Optimizing large data animations