10705

Animated line plot with parallel processing

Question:

What I want to do:

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 (<a href="https://stackoverflow.com/questions/50697548/how-to-manage-parallel-processing-with-animated-ggplot2-plot/50702959#50702959" rel="nofollow">How to manage parallel processing with animated ggplot2-plot?</a>) as a template and wanted to build from there.

I also had a look at <a href="http://lenkiefer.com/2018/01/17/simple-animated-line-plot/" rel="nofollow">this post</a> to see how animating a line plot via single-core processing works.

The Problem:

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

<ul><li>displays the whole scale on the x-axis</li> <li>displays a "growing" line from the left to the right</li> </ul>

Here's an example of the problem:

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

The desired Result:

What I want to achieve should look like the animation in <a href="http://lenkiefer.com/2018/01/17/simple-animated-line-plot/" rel="nofollow">this post</a>.

Thank you in advance for your suggestions.

Answer1:

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

Then you can safe the figure as gif

anim_save("GIF.gif")

<a href="https://i.stack.imgur.com/REhG8.gif" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/REhG8.gif" data-original="https://i.stack.imgur.com/REhG8.gif" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Recommend

  • Where to run collectstatic when deploying django app to heroku using docker?
  • Convert a map into a keyword list in Elixir
  • Get the instance of a component provider out of its scope
  • Django Login Form Input Fields not showing up
  • Replace a specific line in a file using Ruby
  • Google CSE REST API : is v1 deprecated, is there a v2?
  • C# - System.OutOfMemoryException in Visual Studio
  • How can I restart the airflow server on Google Composer?
  • Hand Off Bot to Human using BotFramework (NodeJS)
  • Electron require() is not defined
  • Mocking Parametrized Constructor using Gmock
  • how to save a fitted R model for later use
  • List of Date + Hours between two dates
  • Storing an async function as a variable
  • How to access a bundled ES6 class in inline
  • .htaccess rule: multiple domains to one ssl domain
  • Installation failed for laravel/lumen-installer: guzzlehttp/guzzle locked at 6.3.0
  • MySql tables, error #1064 & error #1068 Multiple primary key defined [closed]
  • Newtonsoft.json serializing and deserializing base/inheirited where classes are from shared projects
  • Turn Inline OnClick to Click Jquery
  • JavaFX Embed Custom Font Not Working
  • what do lines starting with double-slash '//' mean in a .npmrc?
  • How to define a custom accuracy in Keras to ignore samples with a particular gold label?
  • What does “T extends Junk” mean in a generic class in Java?
  • xpath assertion failure with dynamic xpath
  • Rotating Towards Path in OpenGL
  • How convert html to BBcode in C#
  • Floating parent div grows to hypothetical width of floating child div
  • C++ STL stack pop operation giving segmentation fault
  • Change cell value based on cell color in google spreadsheet
  • Send array to next viewcontroller iOs xcode [duplicate]
  • Codeigniniter insert data through models and controller
  • XEP-0166: Jingle protocol implementation for voice/video chat in iOS
  • PHP Permalinks.. how to change?
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?