具有并行处理的动画线图 [英] Animated line plot with parallel processing

查看:96
本文介绍了具有并行处理的动画线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在给定的时间范围内(以月和年为单位)构建动画线图.因为我有很多条目,所以我想通过并行处理来提高速度.我使用了我的一个老问题(如何使用动画ggplot2-plot作为模板来管理并行处理?),并希望从那里进行构建.

I'm trying to build an animated lineplot over a given timeframe (in months and years). As I've got a lot of entries, I wanted to do it via parallel processing to increase speed. I used the answer to one of my old questions (How to manage parallel processing with animated ggplot2-plot?) as a template and wanted to build from there.

我还看了这篇文章看看如何通过单核处理为线图设置动画.

I also had a look at this post to see how animating a line plot via single-core processing works.

不幸的是,我无法弄清楚在何处以及如何正确过滤数据(例如filter(x, date_input_in_loop <= date)),以便对其进行过滤...

Unfortunately, I can't figure out where and how to filter my data properly (e.g. filter(x, date_input_in_loop <= date)) so that it...

  • 在x轴上显示整个比例尺
  • 从左到右显示一条增长中"的线

这是问题的一个例子:

library(doParallel)

# sample data
x <- structure(list(date = c("January 2013", "February 2013", "March 2013", 
                         "April 2013", "May 2013", "June 2013", "July 2013", "August 2013", 
                         "September 2013", "October 2013", "November 2013", "December 2013", 
                         "January 2014", "February 2014", "March 2014", "April 2014", 
                         "May 2014", "June 2014", "July 2014", "August 2014", "September 2014", 
                         "October 2014", "November 2014", "December 2014", "January 2015", 
                         "February 2015", "March 2015", "April 2015", "May 2015", "June 2015", 
                         "July 2015", "August 2015", "September 2015", "October 2015", 
                         "November 2015", "December 2015", "January 2016", "February 2016", 
                         "March 2016", "April 2016", "May 2016", "June 2016", "July 2016", 
                         "August 2016", "September 2016", "October 2016", "November 2016", 
                         "December 2016", "January 2017", "February 2017", "March 2017", 
                         "April 2017", "May 2017", "June 2017", "July 2017", "August 2017", 
                         "September 2017", "October 2017", "November 2017", "December 2017", 
                         "January 2018", "February 2018", "March 2018", "April 2018", 
                         "May 2018", "June 2018", "July 2018", "August 2018", "September 2018", 
                         "October 2018"),
                count = c(131, 17, 68, 79, 127, 168, 13, 0, 
                          11, 62, 99, 131, 168, 14, 100, 68, 147, 187, 10, 0, 7, 63, 122, 
                          116, 155, 20, 82, 101, 138, 215, 7, 0, 11, 75, 102, 121, 141, 
                          23, 87, 96, 154, 241, 16, 0, 9, 64, 130, 94, 179, 38, 112, 67, 
                          183, 206, 15, 1, 7, 80, 120, 125, 175, 39, 81, 104, 158, 214, 
                          15, 0, 10, 73)),
           row.names = c(NA, -70L),
           class = c("tbl_df", "tbl", "data.frame"))

# plot specifics
y_max <- round(max(x$count,na.rm=TRUE) * 1.25,0)
y_nstep <- 10
y_breaks <- round(y_max/10^(nchar(y_max)-2),0)*10^(nchar(y_max)-2) / y_nstep

# setup doParallel
cores <- detectCores()
ind_cluster <- sort(rep_len(1:cores, nrow(x)))
date_cluster <- split(x, ind_cluster)
registerDoParallel(cl <- makeCluster(cores,type="PSOCK"))

# create tempfile for images
tmp <- tempfile()

# loop
files <- foreach(ic = 1:cores, .packages = c("tidyverse", "magick", "ggplot2")) %dopar% {
  # Magick-device
  img <- image_graph(1200, 700, res = 96)
  # data
  x %>%
    filter(date %in% date_cluster[[ic]]) %>%
    group_by(date) %>%
    do(
      plot = ggplot(.) +
        geom_line(aes(date, count, group=1), size=2) +
        geom_line(aes(date, count, group=1), size=2, alpha=0) +
        scale_y_continuous(expand = c(0,0), 
                           breaks = c(seq(0, y_breaks*y_nstep,y_breaks)), 
                           limits = c(0, y_breaks*y_nstep))
    ) %>%
    pmap(function(date, plot) {
      print(plot + ggtitle(as.character(date))
      )
      NULL
    })

  # write image
  dev.off()
  image_write(image_animate(img, fps = 2), paste0(tmp, ic, ".gif"))
}

# stop cluster
closeAllConnections()

# save plot
plot <- do.call(c, lapply(files, image_read))
image_write(image_animate(plot, fps = 10), "test.gif")

所需结果:

我想要实现的目标应该类似于此动画发布.

预先感谢您的建议.

推荐答案

不确定为什么要这么复杂.我会尝试

Not sure why you want it that complicated. I would try

library(gganimate)
library(tidyverse)
Sys.setlocale("LC_TIME", "C")
x %>% 
  mutate(group=1) %>% 
  mutate(date=as.Date(paste0("01 ", date),format ="%d %B %Y")) %>% 
  ggplot(aes(date, count, group=group)) +
    geom_line()  + 
    scale_x_date(date_breaks = "year", date_labels = "%Y") + 
    transition_reveal(group, date) +
    ease_aes('linear')

然后,您可以将图形保存为gif

Then you can safe the figure as gif

anim_save("GIF.gif")  

这篇关于具有并行处理的动画线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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