具有并行处理的动画线图
Animated line plot with parallel processing
我想做的事情:
我正在尝试在给定的时间范围内(以月和年为单位)构建动画线图。由于我有很多条目,我想通过并行处理来提高速度。我使用我的一个老问题 () 的答案作为模板,并想从那里构建。
我还查看了 this post 以了解如何通过单核处理对线图进行动画处理。
问题:
不幸的是,我不知道在哪里以及如何正确地过滤我的数据(例如 filter(x, date_input_in_loop <= date)
)以便它...
- 在 x 轴上显示整个刻度
- 从左到右显示一条"growing"线
这是一个问题示例:
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中的动画效果。
提前感谢您的建议。
不确定为什么要这么复杂。我会试试
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")
我想做的事情:
我正在尝试在给定的时间范围内(以月和年为单位)构建动画线图。由于我有很多条目,我想通过并行处理来提高速度。我使用我的一个老问题 (
我还查看了 this post 以了解如何通过单核处理对线图进行动画处理。
问题:
不幸的是,我不知道在哪里以及如何正确地过滤我的数据(例如 filter(x, date_input_in_loop <= date)
)以便它...
- 在 x 轴上显示整个刻度
- 从左到右显示一条"growing"线
这是一个问题示例:
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中的动画效果。
提前感谢您的建议。
不确定为什么要这么复杂。我会试试
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")