Generate animated GIF showing Thinking Grid plots across different conditions or time points.
Usage
create_tg_animation(
survey_results,
dc_column = "Deliberate.Constraints",
ac_column = "Automatic.Constraints",
condition_column = NULL,
type = "depth",
proportion_type = "overall",
colorer = NULL,
palette = "RdYlBu",
zero_color = "#FFFFFF",
gradient_scaling = "linear",
enhanced_threshold_pct = 50,
enhanced_expansion_factor = 1.5,
x_label = "Directedness",
y_label = "Stickiness",
max_legend = NULL,
min_legend = NULL,
plot_title = NULL,
legend_title = NULL,
plot_subtitle = NULL,
filename = "tg_animation.gif",
duration = 1,
width = 800,
height = 800,
sorted_conditions = NULL,
subset_condition = NULL
)
Arguments
- survey_results
Data frame containing survey results with constraint response columns.
- dc_column
Name of deliberate constraints column (default: "Deliberate.Constraints").
- ac_column
Name of automatic constraints column (default: "Automatic.Constraints").
- condition_column
Column name containing conditions for animation frames. Each unique value becomes one frame.
- type
Type of visualization (default: "depth"). See plot_tg for options.
- proportion_type
Currently only "overall" is supported for animations.
- colorer
Custom colorer function. If NULL, uses default based on other parameters.
- palette
Color palette (default: "RdYlBu"). See plot_tg for options.
- zero_color
Color for zero values (default: "#FFFFFF").
- gradient_scaling
Scaling method: "linear" (default) or "enhanced".
- enhanced_threshold_pct
Enhanced scaling threshold percentage (default: 50).
- enhanced_expansion_factor
Enhanced scaling expansion factor (default: 1.5).
- x_label
X-axis label (default: "Directedness").
- y_label
Y-axis label (default: "Stickiness").
- max_legend
Maximum legend value. If NULL, calculated from all conditions.
- min_legend
Minimum legend value. If NULL, calculated from all conditions.
- plot_title
Main title appearing on all frames.
- legend_title
Legend title.
- plot_subtitle
Subtitle(s). Can be single string (same subtitle for all frames) or vector (one subtitle per condition in same order as sorted_conditions if provided).
- filename
Output GIF filename (default: "tg_animation.gif").
- duration
Duration of each frame in seconds (default: 1).
- width
GIF width in pixels (default: 800).
- height
GIF height in pixels (default: 800).
- sorted_conditions
Vector specifying frame order. Must contain all unique values from condition_column. If NULL, numeric conditions are sorted in ascending order and character/factor conditions are in random order (with warning).
- subset_condition
R expression string for subsetting data before analysis. Applied before splitting by conditions.
Details
The function creates one frame per unique value in condition_column. All frames use the same legend scale (calculated from all conditions) to ensure comparability across frames. Requires the 'gifski' package for GIF creation. Will prompt to install if missing.
Examples
## Create sample data with time points for animation
set.seed(123) ## For reproducible examples
survey_data <- data.frame(
Deliberate.Constraints = sample(1:6, 300, replace = TRUE),
Automatic.Constraints = sample(1:6, 300, replace = TRUE),
time_point = rep(1:3, each = 100),
week = rep(1:3, each = 100),
completed_training = sample(c(TRUE, FALSE), 300, replace = TRUE)
)
# \donttest{
## Basic animation across time points (creates temporary GIF file)
temp_gif1 <- tempfile(fileext = ".gif")
create_tg_animation(survey_data,
condition_column = "time_point",
filename = temp_gif1)
## Clean up temporary file
if (file.exists(temp_gif1)) file.remove(temp_gif1)
#> [1] TRUE
## Enhanced scaling for small differences
temp_gif2 <- tempfile(fileext = ".gif")
create_tg_animation(survey_data,
condition_column = "week",
gradient_scaling = "enhanced",
enhanced_threshold_pct = 40,
enhanced_expansion_factor = 2.0,
subset_condition = "completed_training == TRUE",
filename = temp_gif2)
#> Applying subset condition: completed_training == TRUE
#> Subset applied successfully. 154 rows remaining out of 300 original rows.
## Clean up temporary file
if (file.exists(temp_gif2)) file.remove(temp_gif2)
#> [1] TRUE
# }