Jeff Henderson
Jeff Henderson

Reputation: 689

Limit data going into a heat map across transitions in gganimate

I'm newer to gganimate and trying to make a heatmap that only considers specific years into a frame and that shows only one frame per year with that year's data considered. I made a sample dput with just three years and code for the ggplot. Based on what I found in searching, I thought maybe view_step_manual() might be it but not sure how to write it if so.

foo <- structure(list(yearSeason = c(2021, 2021, 2021, 2021, 2021, 
2022, 2022, 2022, 2022, 2022, 2023, 2023, 2023, 2023, 2023), x = c(-1, 
14.4, -1.5, -13.3, 8.3, -2.9, -14.3, 23.9, 13.6, 1.1, 14.1, 16.3, 
3.3, -10.8, 0), y = c(8.95, 15.65, 7.55, 5.55, 18.75, 8.25, 25.55, 
3.75, 29.35, 7.45, 25.85, 27.75, 8.95, 29.45, 5.65)), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -15L), groups = structure(list(
yearSeason = c(2021, 2022, 2023), .rows = structure(list(
    1:5, 6:10, 11:15), ptype = integer(0), class = c("vctrs_list_of", 
"vctrs_vctr", "list"))), row.names = c(NA, -3L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))

Here's what I tried:

ggplot(foo) + 
  geom_density_2d_filled(mapping = aes(x=x,y=y,fill = ..level..,), 
                     contour_var = "ndensity", breaks = seq(0.01, 1, length.out = 10), 
alpha = .5) +
  labs(title = 'Year: {frame_time}') +
  transition_time(as.integer(yearSeason)) +
  ease_aes('linear')

So the output I'm looking for in this example would really just be three frames for each year (2021, 2022, 2023) that shows a heatmap for only data falling in that year.

Upvotes: 0

Views: 59

Answers (1)

Quinten
Quinten

Reputation: 41437

You get the following error:

Error in transform_polygon(all_frames, states[[i]], ease, nframes[i],  : 
  The transformr package is required to tween polygons

So you should load the transformr package to get this animation working like this:

library(ggplot2)
library(gganimate)
# devtools::install_github('thomasp85/transformr')
library(transformr)
ggplot(foo) + 
  geom_density_2d_filled(mapping = aes(x=x,y=y,fill = ..level..,), 
                         contour_var = "ndensity", breaks = seq(0.01, 1, length.out = 10), 
                         alpha = .5) +
  labs(title = 'Year: {frame_time}') +
  transition_time(as.integer(yearSeason)) +
  ease_aes('linear')
#> Warning: The dot-dot notation (`..level..`) was deprecated in ggplot2 3.4.0.
#> ℹ Please use `after_stat(level)` instead.

Created on 2023-01-20 with reprex v2.0.2

Upvotes: 0

Related Questions