可以在R中使用gganimate为多边形填充制作动画吗? [英] Possible to animate polygon fill using gganimate in R?

查看:50
本文介绍了可以在R中使用gganimate为多边形填充制作动画吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有县级数据,该数据记录了2002年至2018年该县首次检测到入侵害虫的年份.我使用ggplot2创建了一张地图,该地图包根据害虫的年份用一种颜色填充了该县的多边形被检测到.

I have county level data recording the year an invasive insect pest was first detected in that county between 2002 and 2018. I created a map using ggplot2 and the maps package that fills the county polygons with a color according to the year the pest was detected.

**是否可以使用gganimate软件包对地图进行动画处理,第一帧仅填充检测日期为2002的多边形,第二帧填充检测日期为2003或更早的多边形(因此2002和2003),检测日期2004年或更早(2002年,2003年,2004年)等的第三帧?**澄清:我想让所有县多边形始终可见,并最初用白色填充,并且动画的每一帧都会根据检测年份在县中添加填充.

**Is there a way to use the gganimate package to animate this map with the first frame filling in only polygons with a detection date of 2002, the second frame filling polygons with a detection date of 2003 or earlier (so 2002 and 2003), a third frame for detection dates of 2004 or earlier (2002, 2003, 2004), etc.? ** Clarification: I'd like it so all the county polygons are always visible and filled in with white initially and each frame of the animation adds fills in counties based on the year of detection.

我尝试将 transition_reveal(data $ detect_year)与静态图一起使用,但是得到一个错误,数据必须是整数,数字,POSIXct,Date,difftime,orhms".

I've tried using the transition_reveal(data$detect_year) with the static plot but get an error that "along data must either be integer, numeric, POSIXct, Date, difftime, orhms".

以下是可重现示例的一些代码:

Here's some code for a reproducible example:

library(dplyr)
library(purrr)
library(maps)
library(ggplot2)
library(gganimate)
# Reproducible example
set.seed(42)
map_df <- map_data("county") %>% 
   filter(region == "minnesota")
map_df$detection_year <- NA
# Add random detection year to each county
years <- 2002:2006
map_list <- split(map_df, f = map_df$subregion)
map_list <- map(map_list, function(.x) {
   .x$detection_years <- mutate(.x, detection_years = sample(years, 1))
})
# collapse list back to data frame
map_df <- bind_rows(map_list)
map_df$detection_years <- as.factor(map_df$detection_years)

# Make plot
static_plot <- ggplot(map_df,
                      aes(x = long,
                          y = lat,
                          group = group)) +
   geom_polygon(data = map_df, color = "black", aes(fill = detection_years)) +
   scale_fill_manual(values = terrain.colors(n = length(unique(map_df$detection_years))),
                     name = "Year EAB First Detected") +
   theme_void() +
   coord_fixed(1.3)

animate_plot <- static_plot +
   transition_reveal(detection_years)

如果有可能用gganimate做到这一点,我愿意,但是如果有人有想法,我也愿意接受其他解决方案.

If it's possible to do this with gganimate, I'd like to but I'm also open to other solutions if anyone has ideas.

推荐答案

从@RLave那里得到一个答案,该答案几乎可以满足我的要求,并花了一点时间在文档上,所以我能够找到一种方法来做我想.看起来不太干净,但是可以.

After getting an answer from @RLave that almost did what I wanted and spending a little time with the documentation, I was able to figure out a way to do what I want. It doesn't seem very clean, but it works.

从本质上讲,我每年都需要在动画中创建框架以复制数据框架.然后,对于要设置动画效果的每一检测年,我都在数据框的副本中编辑了 detection_year 变量,以便在感兴趣年份或更早的年份进行检测的任何县都保留其值,以及尚未检测到的县被转换为我绘制为白色的值.这确保了所有县总是被标绘.然后,我需要使用 transition_manual 以及我赋予原始数据帧的每个副本的唯一ID来确定动画的顺序.

Essentially, I created a copy of my data frame for each year that needed a frame in the animation. Then for each year of detection I wanted to animate, I edited the detection_year variable in that copy of the data frame so that any county that had a detection in the year of interest or earlier retained their values and any county that had no detection yet was converted to the value I plotted as white. This made sure all the counties were always plotted. Then I needed to use transition_manual along with a unique ID I gave to each copy of the original data frame to determine the order of the animation.

library(dplyr)
library(purrr)
library(maps)
library(ggplot2)
library(gganimate)
# Reproducible example
set.seed(42)
years <- 2002:2006

map_df <- map_data("county") %>% 
   filter(region == "minnesota")

map_df <- map_df %>% 
   group_by(subregion) %>% 
   mutate(detection_year = sample(years,1))

animate_data <- data.frame()
for(i in 2002:2006){
   temp_dat <- map_df %>% 
      mutate(detection_year = as.numeric(as.character(detection_year))) %>% 
      mutate(detection_year = case_when(
         detection_year <= i ~ detection_year,
         detection_year > i ~ 2001
      ),
      animate_id = i - 2001
      )
   animate_data <- bind_rows(animate_data, temp_dat)
}

animate_data$detection_year <- as.factor(as.character(animate_data$detection_year))

# Make plot
static_plot <- ggplot(animate_data,
                      aes(x = long,
                          y = lat,
                          group = group)) +
   geom_polygon(data = animate_data, color = "black", aes(fill = detection_year)) +
   scale_fill_manual(values = c("white",
                                terrain.colors(n = 5)),
                     name = "Year First Detected") +
   theme_void() +
   coord_fixed(1.3) #+
   facet_wrap(~animate_id)

animate_plot <- static_plot +
   transition_manual(frames = animate_id)
animate_plot

这篇关于可以在R中使用gganimate为多边形填充制作动画吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆