具有并行处理的动画线图
Posted
技术标签:
【中文标题】具有并行处理的动画线图【英文标题】:Animated line plot with parallel processing 【发布时间】:2019-04-08 18:06:39 【问题描述】:我想做什么:
我正在尝试在给定的时间范围内(以月和年为单位)构建动画线图。由于我有很多条目,我想通过并行处理来提高速度。我使用我的一个旧问题 (How to manage parallel processing with animated ggplot2-plot?) 的答案作为模板,并希望从那里构建。
我还查看了this post,了解如何通过单核处理为线图制作动画。
问题:
不幸的是,我不知道在哪里以及如何正确过滤我的数据(例如filter(x, date_input_in_loop <= date)
),以便它...
这是一个问题的例子:
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")
想要的结果:
我想要实现的应该看起来像this post 中的动画。
提前感谢您的建议。
【问题讨论】:
【参考方案1】:不知道你为什么要这么复杂。我会试试的
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了
anim_save("GIF.gif")
【讨论】:
这太棒了!但是,我无法在 Win10 上安装gganimate
,因为可以在这篇文章中找到问题:github.com/thomasp85/gganimate/issues/115。我需要一种解决方法才能让它工作,这可能比解决我的实际问题需要更长的时间来安装它。
不是Win10的问题。我也有麻烦,但让它实际工作。我会搜索我是如何管理它的。
尝试先使用install.packages('devtools'); devtools::install_github('thomasp85/tweenr')
安装tweenr
以上是关于具有并行处理的动画线图的主要内容,如果未能解决你的问题,请参考以下文章
异常消息: 查询处理器未能为执行并行查询启动必要的线程资源。