R语言 限制跨gganimate中的转换进入热图的数据

z4bn682m  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(123)

我是gganimate的新手,我试图制作一个热图,它只考虑特定年份的帧,并且只显示一个帧。我制作了一个样本dput,只有三年的数据和ggplot的代码。基于我在搜索中找到的,我想可能是view_step_manual(),但不知道如果是这样的话该怎么写。

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))

以下是我的尝试:

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')

因此,我在本例中寻找的输出实际上只是每年的三个帧(2021年、2022年、2023年),仅显示该年数据的热图。

wribegjk

wribegjk1#

您会得到以下错误:

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

因此,您应该加载transformr包,以使此动画像这样工作:

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.

创建于2023年1月20日,使用reprex v2.0.2

相关问题